C # Webservice: exception exception with extra properties in JSON

I have a web service written in C #. When errors occur on the server, I would like to inform the user of the client side. This works great by throwing exception servers, which are then sent to my client side error handler.

However, I would like, when I have selected the exception, to set a property that describes how seriously I think the error is at the moment. Thus, I can decide the client side how to display the error:

WebService.Method(some_value, handle_response, handle_error);

function handle_response (response) {
    //Do something...
}

function handle_error (error) {
    if(error.level === 'Critical') {
        //Show critical message.
    } else if(error.level === 'Warning') {
        //Show warning message.
    } else 
        ...
    }
}

My solution so far has been to create a custom exception that inherits from System.Exception. My web service is returning with a formatted result JSON.

My problem is how to pass my property to client side JSON response?

+3
1

-:

     public Response zzz()
        {
          Response result;
           try
        {
         ...
        }
        catch(MyException)
        {
          result.HasError = true;
          result.Error.Level = Normal;
          result.Error.Message = "It OK.";
        }
        catch(Exception)
        {
result.HasError = true;
          result.Error.Level = Critical;
          result.Error.Message = "!!!!";
        }
        }

Response.HasError

+3

Source: https://habr.com/ru/post/1734541/


All Articles