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.
This commit is contained in:
Refringe 2024-02-05 19:03:18 -05:00
parent 3a2b24b9b8
commit cd6e40b66c
No known key found for this signature in database
GPG Key ID: 64E03E5F892C6F9E

View File

@ -372,15 +372,11 @@ export class RagfairController
} }
// Get an array of items from player inventory to list on flea // Get an array of items from player inventory to list on flea
const getItemsFromInventoryErrorMessage = ""; const { items: itemsInInventoryToList, errorMessage: itemsInInventoryError } = this
const itemsInInventoryToList = this.getItemsToListOnFleaFromInventory( .getItemsToListOnFleaFromInventory(pmcData, offerRequest.items);
pmcData, if (!itemsInInventoryToList || itemsInInventoryError)
offerRequest.items,
getItemsFromInventoryErrorMessage,
);
if (!itemsInInventoryToList)
{ {
this.httpResponse.appendErrorToOutput(output, getItemsFromInventoryErrorMessage); this.httpResponse.appendErrorToOutput(output, itemsInInventoryError);
} }
// Checks are done, create the offer // 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 * Using item ids from flea offer request, find corresponding items from player inventory and return as array
* @param pmcData Player profile * @param pmcData Player profile
* @param itemIdsFromFleaOfferRequest Ids from request * @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 * @returns Array of items from player inventory
*/ */
protected getItemsToListOnFleaFromInventory( protected getItemsToListOnFleaFromInventory(
pmcData: IPmcData, pmcData: IPmcData,
itemIdsFromFleaOfferRequest: string[], itemIdsFromFleaOfferRequest: string[],
errorMessage: string, ): { items: Item[] | null; errorMessage: string | null; }
): Item[]
{ {
const itemsToReturn = []; const itemsToReturn = [];
let errorMessage: string | null = null;
// Count how many items are being sold and multiply the requested amount accordingly // Count how many items are being sold and multiply the requested amount accordingly
for (const itemId of itemIdsFromFleaOfferRequest) for (const itemId of itemIdsFromFleaOfferRequest)
{ {
@ -575,7 +571,7 @@ export class RagfairController
}); });
this.logger.error(errorMessage); this.logger.error(errorMessage);
return null; return { items: null, errorMessage };
} }
item = this.itemHelper.fixItemStackCount(item); item = this.itemHelper.fixItemStackCount(item);
@ -587,10 +583,10 @@ export class RagfairController
errorMessage = this.localisationService.getText("ragfair-unable_to_find_requested_items_in_inventory"); errorMessage = this.localisationService.getText("ragfair-unable_to_find_requested_items_in_inventory");
this.logger.error(errorMessage); this.logger.error(errorMessage);
return null; return { items: null, errorMessage };
} }
return itemsToReturn; return { items: itemsToReturn, errorMessage };
} }
public createPlayerOffer( public createPlayerOffer(