There are multiple ways to control exceptions that are returned from your method.
  1. The default behavior is that any exception thrown in your method will be returned to the client in its entirety (stack trace, everything)
  2. 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.
  1. 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);
            return s;

Setting the exception without throwing one.

  1. 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));
            return null;
It is up to the developer to stop execution of the current method when using this approach.

Last edited Jan 14, 2013 at 7:16 PM by AustinHarris, version 2


No comments yet.