开发者

C# Webservice: Throw exception with extra properties in JSON

I have a webservice written in C#. When errors occur on the server, I would like to inform the user client side. This works fine by throwing exceptions server side, which is then sent to my error handler client side.

However, I would like to, when I throw the exception, to set a property开发者_如何学编程 describing how serious I think the error is at this point. Thus I can decide 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 inheriting from System.Exception. My webservice returns with a JSON formatted result.

My problem is how to get my property through to the client side JSON response?


Web service:

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

Then check Response.HasError

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜