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
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(