{"record":{"id":"72d1507e0e8ad02e","repo":"nopSolutions/nopCommerce","slug":"discount-could-not-be-loaded","errorCode":null,"errorMessage":"Discount could not be loaded","messagePattern":"Discount could not be loaded","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Plugins/Nop.Plugin.DiscountRules.CustomerRoles/Controllers/DiscountRulesCustomerRolesController.cs","lineNumber":70,"sourceCode":"    /// Get errors message from model state\r\n    /// </summary>\r\n    /// <param name=\"modelState\">Model state</param>\r\n    /// <returns>Errors message</returns>\r\n    protected IEnumerable<string> GetErrorsFromModelState(ModelStateDictionary modelState)\r\n    {\r\n        return ModelState.Values.SelectMany(v => v.Errors.Select(e => e.ErrorMessage));\r\n    }\r\n\r\n    #endregion\r\n\r\n    #region Methods\r\n\r\n    [CheckPermission(StandardPermission.Promotions.DISCOUNTS_VIEW)]\r\n    public async Task<IActionResult> Configure(int discountId, int? discountRequirementId)\r\n    {\r\n        //load the discount\r\n        var discount = await _discountService.GetDiscountByIdAsync(discountId)\r\n                       ?? throw new ArgumentException(\"Discount could not be loaded\");\r\n\r\n        //check whether the discount requirement exists\r\n        if (discountRequirementId.HasValue && await _discountService.GetDiscountRequirementByIdAsync(discountRequirementId.Value) is null)\r\n            return Content(\"Failed to load requirement.\");\r\n\r\n        //try to get previously saved restricted customer role identifier\r\n        var restrictedRoleId = await _settingService.GetSettingByKeyAsync<int>(string.Format(DiscountRequirementDefaults.SettingsKey, discountRequirementId ?? 0));\r\n\r\n        var model = new RequirementModel\r\n        {\r\n            RequirementId = discountRequirementId ?? 0,\r\n            DiscountId = discountId,\r\n            CustomerRoleId = restrictedRoleId,\r\n            //set available customer roles\r\n            AvailableCustomerRoles = (await _customerService.GetAllCustomerRolesAsync(true)).Select(role => new SelectListItem\r\n            {\r\n                Text = role.Name,\r\n                Value = role.Id.ToString(),\r","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/nopSolutions/nopCommerce/blob/64bdf2ff08c8b39e65717bcf974fb43dc2ef68f2/src/Plugins/Nop.Plugin.DiscountRules.CustomerRoles/Controllers/DiscountRulesCustomerRolesController.cs#L52-L88","documentation":"Thrown by DiscountRulesCustomerRolesController.Configure as an ArgumentException when _discountService.GetDiscountByIdAsync(discountId) returns null — i.e. no discount exists with the supplied id. The action is guarded by DISCOUNTS_VIEW permission.","triggerScenarios":"A request to the discount-rules customer-roles configuration endpoint with a discountId that does not exist in the Discount table (deleted, never existed, or wrong id). Commonly hit when the admin UI loads a stale link to a deleted discount.","commonSituations":"A discount was deleted but a browser tab/bookmark still points at its configuration URL; a malformed or hand-crafted request with an arbitrary id; concurrency where the discount is removed between page load and the configure POST.","solutions":["Navigate back to the discounts list and open a valid discount.","If linking programmatically, verify the discount id exists before building the URL.","Handle the 500/exception gracefully in the calling UI (show 'discount not found') instead of relying on the raw throw."],"exampleFix":"// before\nvar discount = await _discountService.GetDiscountByIdAsync(discountId)\n               ?? throw new ArgumentException(\"Discount could not be loaded\");\n// after (caller-side guard)\nvar discount = await _discountService.GetDiscountByIdAsync(discountId);\nif (discount is null) return NotFound($\"Discount {discountId} not found.\");","handlingStrategy":"validation","validationCode":"var discount = await _discountService.GetDiscountByIdAsync(discountId);\nif (discount is null) return NotFound(\"Discount not found.\");\n// proceed to Configure logic","typeGuard":"static bool DiscountExists(IDiscountService svc, int id)\n    => svc.GetDiscountByIdAsync(id).GetAwaiter().GetResult() is not null;","tryCatchPattern":"try { return await Configure(discountId, reqId); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Discount could not be loaded\"))\n{ return NotFound(ex.Message); }","preventionTips":["Verify discount id exists before navigating/linking.","Refresh stale admin tabs after deleting a discount.","Return NotFound instead of throwing for missing entities."],"tags":["discounts","plugins","controller","validation","not-found"],"backgroundTag":null,"analyzedSha":"64bdf2ff08c8b39e65717bcf974fb43dc2ef68f2","analyzedAt":"2026-08-13T21:19:38.062Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}