eddy
eddy

Reputation: 4413

How to catch every exception thrown in a webmethod but without those exceptions interrupting the execution of the program

Is there any way I can make that every exception thrown inside a webmethod go straight to the jQuery Ajax error callback function?

    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "MantenimientoNotasCapacidades.aspx/SaveViaWebService",
        data: JSON.stringify(params),
        dataType: "json",
        async: true,
        success: function (data) {               


        },
        error: function (request, status, error) {
            var response = JSON.parse(request.responseText).d;
            var error = JSON.parse(response);
            alert(JSON.parse(request.responseText).error.message);

        }
    });

I know that using JSON.parse(request.responseText).Message should be enough to show the information for that error but all I've got now is that every time an exception is raised the code stops right there , being necessary to keep pressing F10 or F5 to finally be able to see the alert.

I already tried enclosing my code in a 'try' block but I don't see much point in doing that since I can't do much in the 'catch' block as I'd do in a visual basic application where I could use the 'catch' block to show the exception message in a MsgBox.

Is there any way to catch in the error callback function all the exceptions thrown in a webmethod but without them stopping the execution of the code?

Any help would be much appreciated.

P.S. Happy new year!!

Upvotes: 1

Views: 788

Answers (1)

eddy
eddy

Reputation: 4413

Following @cmd.promt's advice, I changed the Response.StatusCode to 500 and created and object(myError) containing the description for the exception that was thrown, then the only thing left to do was to serialize the object and send it back to the client (Here I tried to use Response..Write("{""message"": ""action failed!""}") but for some reason I always ended up getting the same error : "JSON.parse: unexpected non-whitespace character after JSON data" so in the end I decided to go with json.net and forget all about response.Write)

<WebMethod()> _
        Public Shared Function SaveViaWebService(lst As List(Of Students)) As String
          Try

          Catch ex As Exception


                Dim httpResponse = HttpContext.Current.Response
                httpResponse.Clear()
                httpResponse.StatusCode = 500

                'I thought that .StatusDescription would be useful but it turned out it didn't
                'httpResponse.StatusDescription = ex.Message

                Dim myError= New With {.message = ex.Message, .source = ex.Source}
                Return JsonConvert.SerializeObject(myError)

            End Try
        End Function

And with that, the error is correctly sent to the error callback , where the only thing I had to do was to play with firebug and see how the error has been sent enter image description here

$.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "MantenimientoNotasCapacidades.aspx/SaveViaWebService",
        data: JSON.stringify(params),
        dataType: "json",
        async: true,
        success: function (data) {               


        },
        error: function (request, status, error) {
            var response = JSON.parse(request.responseText).d;
            var error = JSON.parse(response);
            alert(JSON.parse(request.responseText).error.message);

        }
    });

Thus far, everything is as I as wanted it , except for the exceptions that keep on interrupting the execution of the program. Is that their normal behaviour ?? Aren't they all supposed to go directly to the 'catch' block??

Upvotes: 1

Related Questions