Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

How to get business error message #106


User avatar
0
lcyhjx created

Hi

Please see following scenario: I have an application service, in the business logic, need to do some business rule validation. If validation failed, then I throw an AbpException as following” throw new Abp.AbpException("Available balance is not enough.");

But when I invoked the service, I get following response:

{
   "success": false,
   "result": null,
   "error":    {
      "code": 0,
      "message": "An internal error occured during your request!",
      "details": null,
      "validationErrors": null
   },
   "unAuthorizedRequest": false
}

You can see the error message is a common message ‘An internal error occured during your request!’, but the message I want is ‘Available balance is not enough’. The common error message is fine for unexpected exception, but for the business error, I need to get specific business error message in front-end application. Do you have any suggestion?

BTW, there is spelling error: An internal error occured during your request! An internal error occurred during your request!


2 Answer(s)
  • User Avatar
    0
    lcyhjx created

    I have addressed it by changing the code as following :

    throw new UserFriendlyException("Available balance is not enough.");
    

    then the response is:

    <Response xmlns="http://localhost/api/services/app/orderpublish/publishorder">
       <error>
          <code>0</code>
          <details null="true"/>
          <message>Available balance is not enough.</message>
          <validationErrors null="true"/>
       </error>
       <result null="true"/>
       <success>false</success>
       <unAuthorizedRequest>false</unAuthorizedRequest>
    </Response>
    

    Not sure if it is the best way, if not, could you please let me know the best practice?

  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    UserFriendlyException is built exactly for this reason. So, you've done it right.

    I fixed the spelling error: <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/commit/36bf6687f442ac32ff22694742f9049c8dfe724b">https://github.com/aspnetboilerplate/as ... 9c8dfe724b</a>

    Thanks.