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)
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, ex.
private string throwsException2(string s)
throw new JsonRpcException(-27000, "This exception was thrown using: throw new JsonRpcException()", null);
Setting the exception without throwing one.
- 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(). Ex:
private string throwsException3(string s)
JsonRpcContext.SetException(new JsonRpcException(-27000, "This exception was thrown using: JsonRpcContext.SetException()", null));
It is up to the developer to stop execution of the current method when using this approach.