-
Notifications
You must be signed in to change notification settings - Fork 89
Exceptions
Hüseyin Uslu edited this page Jun 3, 2014
·
1 revision
There are multiple ways to control exceptions that are returned from your method.
- The default behavior is that any exception thrown in your method will be returned to the client in its entirety (stack trace, everything)
- There is a global hook you can use to manage exceptions before they are returned to the client. As early as possible you will want to call AustinHarris.JsonRpc.Config.SetErrorHandler(). In asp.net you should do this in Application_Start. The signature for this method is
private JsonRpcException OnJsonRpcException(JsonRequest rpc, JsonRpcException ex)
{
return ex;
}
Here is where you would add your global error logging. Here you can clean up the exception, maybe you don't want to return the stack trace, or watson buckets.
You can also explicitly throw the exception how you want to in the first place. If you throw a JsonRpcException from within your JsonRpcMethod then it will be returned to the client exactly as you specify it;
[JsonRpcMethod("error3")]
private string throwsException2(string s)
{
throw new JsonRpcException(-27000, "This exception was thrown using: throw new JsonRpcException()", null);
return s;
}
If you don't want to throw an exception at all but still want to control the error response to the client you can make a call to JsonRpcContext.SetException().
[JsonRpcMethod("error4")]
private string throwsException3(string s)
{
JsonRpcContext.SetException(new JsonRpcException(-27000, "This exception was thrown using: JsonRpcContext.SetException()", null));
return null;
}
It is up to the developer to stop execution of the current method when using this approach.