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
});
4
  • 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 Commented Sep 23, 2016 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") Commented Sep 23, 2016 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 Commented Sep 23, 2016 at 16:13
  • I see, that makes sense. This answer suggests you might be able to parse the jqXHR object to reach the exception message. Commented Sep 23, 2016 at 16:36

1 Answer 1

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");
            }
        }
    });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.