1

I am calling web method using ajax call.Here is the example call.

    [WebMethod(true)]
    public static string Save(string customParams)
    {

        throw new ApplicationException("Example Exception");
     }
    $.ajax({
        url: url,
        type: "POST",
        data: data,
        contentType:"application/json; charset=utf-8",
        dataType: props.dataType ? props.dataType : "text",
        error: function (xhr, errorType, ex) {
            debugger;
            err(ex);
        }
    })

If that method throws an exception I only get 500 internal server error.Stacktrace is empty and I can't get the inner exception message.I decorated webmethod with try catch blocks and return HttpException and set text of it but it didn't work.

    try
    {
    throw new ApplicationException("Example Exception");

    }
    catch (Exception e)
    {
      throw new HttpException(500,e.Message,e);
    }

I also tried this solution again with no luck.

        catch (Exception e)
        {
            HttpContext.Current.Response.Write(e.Message.ToJsonString());
            HttpContext.Current.Response.StatusCode=500;
        }

By the way I also experimented that uncaught exception when request is ajax request can't be caught by Global.asax's Application_Error.Here is the issue. I switched custom error off.Now it's displaying error but still not a intented solution.

Any solution ? Thanks in advance.

erhan355
  • 806
  • 11
  • 23

1 Answers1

1

I found some way of achieving this.As you may notice I am changing 500 error responseText with the actual exception's message and stacktrace.

First Clear Response and Header.Then set TrySkipIisCustomErrors = true in order to not let asp.net to return 500 error page.After that write actual error message to the response,flush it and end processing page.I really don't know this is ideal way of doing but so far I only got this solution.

Here is the code.

 public static string ProcessAjaxException(Exception ex)
        {
            if (!HttpContext.Current.Request.IsAjaxRequest())
            {
                return null;
            }
            var page = (Page)HttpContext.Current.CurrentHandler;
            string url = page.AppRelativeVirtualPath;
            Framework.Core.Logging.LoggerFactory.Error(url, ex);
            var jsonExceptionDetails = new { ex.Message, ex.StackTrace, statusText = "500" };
            var serializedExcpDetails = JsonConvert.SerializeObject(jsonExceptionDetails);
            //Erases any buffered HTML output.
            HttpContext.Current.Response.Clear();
            //Erases header
            HttpContext.Current.Response.ClearHeaders();
            /*If the IHttpResponse::SetStatus method was called by using the fTrySkipCustomErrors flag, 
             * the existing response is passed through, 
             * and no detailed or custom error is shown.*/
            HttpContext.Current.Response.TrySkipIisCustomErrors = true;
            HttpContext.Current.Response.ContentType = "application/json; charset=utf-8";
            HttpContext.Current.Response.StatusCode = 500;
            //Send all buffered output to client 
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.Write(serializedExcpDetails);
            //Stop processing the page
            HttpContext.Current.Response.End();
            return null;
        }
erhan355
  • 806
  • 11
  • 23