From cd6e40b66c5c9b99c5f60f15b7536cbf3d37f1e4 Mon Sep 17 00:00:00 2001 From: Refringe Date: Mon, 5 Feb 2024 19:03:18 -0500 Subject: [PATCH] Error Message Propagation Issue Resolves an issue where the error message in `getItemsToListOnFleaFromInventory` did not propagate as expected due to the pass-by-value behaviour in JavaScript. The solution was to refactor the method to return an object that includes both the items and the error message. --- project/src/controllers/RagfairController.ts | 24 ++++++++------------ 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/project/src/controllers/RagfairController.ts b/project/src/controllers/RagfairController.ts index 4ca4ec02..4574134f 100644 --- a/project/src/controllers/RagfairController.ts +++ b/project/src/controllers/RagfairController.ts @@ -372,15 +372,11 @@ export class RagfairController } // Get an array of items from player inventory to list on flea - const getItemsFromInventoryErrorMessage = ""; - const itemsInInventoryToList = this.getItemsToListOnFleaFromInventory( - pmcData, - offerRequest.items, - getItemsFromInventoryErrorMessage, - ); - if (!itemsInInventoryToList) + const { items: itemsInInventoryToList, errorMessage: itemsInInventoryError } = this + .getItemsToListOnFleaFromInventory(pmcData, offerRequest.items); + if (!itemsInInventoryToList || itemsInInventoryError) { - this.httpResponse.appendErrorToOutput(output, getItemsFromInventoryErrorMessage); + this.httpResponse.appendErrorToOutput(output, itemsInInventoryError); } // Checks are done, create the offer @@ -554,16 +550,16 @@ export class RagfairController * Using item ids from flea offer request, find corresponding items from player inventory and return as array * @param pmcData Player profile * @param itemIdsFromFleaOfferRequest Ids from request - * @param errorMessage if item is not found, add error message to this parameter * @returns Array of items from player inventory */ protected getItemsToListOnFleaFromInventory( pmcData: IPmcData, itemIdsFromFleaOfferRequest: string[], - errorMessage: string, - ): Item[] + ): { items: Item[] | null; errorMessage: string | null; } { const itemsToReturn = []; + let errorMessage: string | null = null; + // Count how many items are being sold and multiply the requested amount accordingly for (const itemId of itemIdsFromFleaOfferRequest) { @@ -575,7 +571,7 @@ export class RagfairController }); this.logger.error(errorMessage); - return null; + return { items: null, errorMessage }; } item = this.itemHelper.fixItemStackCount(item); @@ -587,10 +583,10 @@ export class RagfairController errorMessage = this.localisationService.getText("ragfair-unable_to_find_requested_items_in_inventory"); this.logger.error(errorMessage); - return null; + return { items: null, errorMessage }; } - return itemsToReturn; + return { items: itemsToReturn, errorMessage }; } public createPlayerOffer(