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

Throw an error from a service to client #1006


User avatar
0
ofir1234 created

Hi. I'm working with your Dynamic Web API Layer and it's very useful, but I want to understand how can I throw an error to the web client.

I have a client request, defined this way :

abp.services.app.trends.deleteSavedQuery($scope.loadQuerySelectedId)
   .done(function (result) {
      abp.message.info("Saved query deleted successfully");
   });
   //here I want to catch an error (you can't delete a query that wasn't written by you)

The service code in the server is something like this :

[HttpGet]
public void DeleteSavedQuery(int queryId)
{
if (_userManager.AbpSession.UserId == getQueryId(queryId))
   {
      _savedQueriesRepo.Delete(queryId);
   }
   else
   {
      //Here I want to throw an unauthorized error (403) with a message : 'you can't delete a query that wasn't written by you'
   }
}

How can I throw this error from the server and catch it nicely in the client, doing it the best and conventional 'ASP.NET Boilerplate' way ?

Thanks.


4 Answer(s)
  • User Avatar
    0
    finallyfreeguy created
  • User Avatar
    0
    hikalkan created
    Support Team

    just throw UserFriendlyException and ABP will handle the rest.

  • User Avatar
    0
    ofir1234 created

    I understand. Nice. One question - how can I tell the client it's a 500 or 400 or 404 server exception this way?

  • User Avatar
    0
    hikalkan created
    Support Team

    No way for now. Because, application layer is web independent, but these error codes are HTTP related. But we will work on this to find an elegant way.