title: Catching errors from calling ASP.NET WebMethod with malformed Json
body: <p>We have an older ASP.NET WebForms application which performs AJAX request by using jQuery <code>$.ajax()</code> calls on the client side, calling static methods in the page code-behind decorated with <code>[WebMethod]</code> attributes.</p>
  
  <p>If an unhandled exception occurs within the WebMethod, it does not fire the <code>Application_Error</code> event and is thus not picked up by our error logger (<a href="https://code.google.com/p/elmah/">ELMAH</a>). This is well known and not a problem - we have all WebMethod code wrapped in try-catch blocks with exceptions being manually logged to ELMAH.</p>
  
  <p>However, there is one case that has me stumped. If malformed Json is posted to the WebMethod URL, it throws an exception before entering our code, and I can't find any way to trap this. </p>
  
  <p>e.g. this WebMethod signature</p>
  
  <pre><code>[WebMethod]
  public static string LeWebMethod(string stringParam, int intParam)
  </code></pre>
  
  <p>Normally called with a Json payload like:</p>
  
  <pre><code>{"stringParam":"oh hai","intParam":37}
  </code></pre>
  
  <p>I tried a test using Fiddler to edit the payload to the malformed Json:</p>
  
  <pre><code>{"stringParam":"oh hai","intPara
  </code></pre>
  
  <p>And got the following <code>ArgumentException</code> error response from <code>JavaScriptObjectDeserializer</code> sent to the client (this is in a simple test app running locally with no custom errors):</p>
  
  <pre><code>{"Message":"Unterminated string passed in. (32): {\"stringParam\":\"oh hai\",\"intPara","StackTrace":"   at
  System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeString()\r\n   at
  System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeMemberName()\r\n   at
  System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeDictionary(Int32 depth)\r\n   at 
  System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n   at 
  System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\r\n   at 
  System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n   at 
  System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n   at 
  System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)\r\n   at 
  System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n   at 
  System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"}
  </code></pre>
  
  <p>It's still not firing the <code>Application_Error</code> event, and it never enters our code so we can't log the error ourselves.</p>
  
  <p>I found a similar question which got a pointer to the blog post "<a href="http://geekswithblogs.net/pavelka/archive/2005/09/05/HowToCreateAGlobalExceptionHandlerForAWebService.aspx">How to create a global exception handler for a Web Service</a>" but that appears to only be valid for SOAP webservices, not AJAX GETs/POSTs.</p>
  
  <p>Is there some similar way to attach a custom handler in my situation?</p>