I sometime need to response an error message and assign an error code to reject a HTTP request. For example, there is an AddPhoneNumber method in the phonebook example. If the input phone number in HTTP POST request is in our blocked list, I need to reject the POST request with Error code 401 and a message of "Blocked Phone Number". How do I implement it? Do you have a similar example?
Thanks,
4 Answer(s)
-
0
hi,
You can unwrap abp response with the DontWrapResult attribute. <a class="postlink" href="https://aspnetboilerplate.com/Pages/Documents/Javascript-API/AJAX#wrapresult-and-dontwrapresult-attributes">https://aspnetboilerplate.com/Pages/Doc ... attributes</a>
You can define a custom page for each error code in your project. <customErrors mode="On"> <error statusCode="404" redirect="~/Error/E404" /> </customErrors>
-
0
Thank you for your suggestion. As a test, I tried to modify the PersonAppService.AddPhone() in Phonebook example:
public async Task<PhoneInPersonListDto> AddPhone(AddPhoneInput input) { throw new Abp.UI.UserFriendlyException(401,"Test","Invalid Code"); }
I tried to run it in Swagger UI, and got a correct response body: { "result": null, "targetUrl": null, "success": false, "error": { "code": 401, "message": "Test", "details": "Invalid Code", "validationErrors": null }, "unAuthorizedRequest": false, "__abp": true }
However, when I tried to run it by POSTMAN, I got a different error in response body: { "result": null, "targetUrl": null, "success": false, "error": { "code": 0, "message": "Your request is not valid!", "details": "The following errors were detected during validation.\r\n - \r\n", "validationErrors": [ { "message": "", "members": [ "" ] } ] }, "unAuthorizedRequest": false, "__abp": true }
Can you advise what I did wrong?
BTW, this is for a 3rd party client which I can't control. The client side just needs to receive the same error code and message as what I see in Swagger.
Thanks,
-
0
Please discard my last post. It's my fault. I forgot to add Content Type in Header while I test it by postman.
Sorry! :oops:
-
0
ok. no problem. happy coding ;)