1

I have a C# web method that sometimes throws an exception when the call shown below times out (>30 seconds). This is fine I expect this behavior, but the problem is, when the ajax call hits the .fail callback, the error message states "Internal server error". I want to be able to catch the exception and report that the database timed out. How can I do this?

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string GetSPResults(string reportId, string sproc, Dictionary<string, string> parameters, string[] ensureArrays, string[] encryptArrays, string[] dateFields)
{
    ...
        XElement result = avdata.ExecuteSPXmlXElement(sproc, parameters, null);
    ...
}

$.ajax({
    type: "POST",
    url: "/Patrol/Report.aspx/GetSPResults",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: JSON.stringify(postData)
}).done(function (result) {
    ...
}).fail(function (jqXHR, textStatus, err) {
    alert("An error has occurred: " + err); //rerpots "Internal server error" no matter what problem occurs server-side
});
James Wierzba
  • 16,176
  • 14
  • 79
  • 120
  • You can't do this. Internal server error is the correct response from the server in this case. What you can do is catch exceptions on server-side and return response with error message, later on you .done function, you will have to idicate if it's proper response or error – MajkeloDev Sep 23 '16 at 15:18
  • Have you checked the contents of the textStatus string? It looks like it can return a `"timeout"` value (as well as `"error"`, `"abort"`, and `"parsererror"`) – dumbchemistry Sep 23 '16 at 15:19
  • 1
    @pinhead textStatus has a value of `error`, this is a database connection timeout from server->db, not a timeout from client->server which I think would result in the `timeout` return value – James Wierzba Sep 23 '16 at 16:13
  • I see, that makes sense. [This answer](http://stackoverflow.com/a/26003200/3850567) suggests you might be able to parse the `jqXHR` object to reach the exception message. – dumbchemistry Sep 23 '16 at 16:36

1 Answers1

0

i use this

       $.ajax({
          type: "POST",
          url: "/Patrol/Report.aspx/GetSPResults",
         contentType: "application/json; charset=utf-8",
         dataType: "json",
         data: JSON.stringify(postData)

        error: function (jqXHR, textStatus, errorThrown) {
            mensaje = false;
            if (jqXHR.status === 0) {
                alert("Not connect: Verify Network.");
            } else if (jqXHR.status == 404) {
                alert("Requested page not found [404]");
            } else if (jqXHR.status == 500) {
               alert("Internal Server Error [500].");
            } else if (textStatus === 'parsererror') {
                alert("Requested JSON parse failed.");
            } else if (textStatus === 'timeout') {
                alert("Time out error.");
            } else if (textStatus === 'abort') {
                alert("Ajax request aborted.");
            } else {
                toastr.error("Uncaught Error:", "Mensaje Servidor");
            }
        }
    });
Steve Fox
  • 46
  • 3