1

Trying to figure out how to debug generic 500 errors from my .ASMX

JS:

function TestError() {
    return $.ajax({
        type: "POST",
        url: 'Order.asmx/TestError',
        contentType: "application/json; charset=utf-8"
    });
}

$.when(TestError()).then(function (err) {
    console.log(err);
});

C#

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public object TestError()
{
    try
    {
        throw new Exception("Testing error");
    }
    catch (Exception ex)
    {
        return ex;
    }
}

Result:

enter image description here

It's not even logging the err argument. Any ideas?

Edit

adding the error to the original Ajax call is getting me logging info, but still generic & useless.

http://i42.tinypic.com/2v27fgo.jpg

enter image description here

Edit 2

Answered my own question and have asked a follow-up:

Removing properties of an inherited object in C#

Community
  • 1
  • 1
Terry
  • 14,099
  • 9
  • 56
  • 84

2 Answers2

1

After a kick in the right direction and further digging I actually found this problem was twofold.

First, Dave Ward actually pointed me in the right direction in suggesting I turn off customErrors in the web.config. It was actually not that, but very close.

The actual culprit is the Elmah error handling module. After disabling it I was able to relay custom errors regardless of whether customErrors was on or off.

The second issue was that although I could now handle the errors, I still couldn't pass System.Exception objects. I discovered this is because the Exception.TargetSite property is not serializable.

I got around the problem by creating my own JsonException class and only including the serializable properties of the System.Exception class (abbreviated here for simplicity).

public class JsonException
{
    public string Source { get; set; }
    public string Message { get; set; }
    public string StackTrace { get; set; }

    public JsonException(Exception ex)
    {
        this.Source = ex.Source;
        this.Message = ex.Message;
        this.StackTrace = ex.StackTrace;
    }
}

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public object TestError()
{
    try
    {
        throw new Exception("Testing error");
    }
    catch (Exception ex)
    {
        return new JsonException(ex);
    }
}

And now I finally get the data I want:

enter image description here

Terry
  • 14,099
  • 9
  • 56
  • 84
0

I would change to this:

$.ajax({
        type: "POST",
        url: 'Order.asmx/TestError',
        contentType: "application/json; charset=utf-8",
        error: function (XMLHttpRequest,textStatus,errorThrown){ console.log(errorThrown); }
    });
Jake Feasel
  • 16,785
  • 5
  • 53
  • 66
  • That got the object logging to the console, still a generic "Internal Server Error", any idea how to get my custom message in there? – Terry Dec 22 '11 at 17:55
  • Try logging the other arguments - textStatus and XMLHttpRequest. – Jake Feasel Dec 22 '11 at 18:10
  • I did, doesn't help. From jQuery.com docs - *When an HTTP error occurs, errorThrown receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error."* – Terry Dec 22 '11 at 18:15
  • Are you sure your custom message is ever even being returned to the browser? Can you see the full response (in, for example, the Network tab in Chrome developer tools, or Firebug) – Jake Feasel Dec 22 '11 at 18:18
  • No it is definitely not - I'm using Chrome dev tools. See edit w/ screenshot. – Terry Dec 22 '11 at 18:23
  • Oh and in case you were going to ask, if I stop throwing the exception and just return something successfully it **does** work. – Terry Dec 22 '11 at 18:25
  • Well, in that case it sounds like you need to change your .net code to ensure your message does get sent back to the browser. I can't help with that part, though - I'm no .Net developer. – Jake Feasel Dec 22 '11 at 18:27