christiansr85
christiansr85

Reputation: 877

How to handle a exception thrown in c# by a javascript web app?

I'm developing a web application with a client side build with html5 and javascript using, overall, dojo framework (I'm using xhr.post function to communicate with server). The server side is an asmx service in c#.

Here comes the problem: when a webmethod of my service throws an exception:

        try
        {
            throw new Exception("TEST");
        }
        catch (Exception ex)
        {
            throw ex;
            // TODO: send some info to the client about the error
        }

the client is realized than an error has occured and fires the error callback function:

        deferred = xhr.post({
            url: that.config.urlService,
            handleAs: that.config.handleAs,
            contentType: that.config.contentType,
            postData: that.config.parameters
        });

        deferred.then(function (res) {
            that.success(res);
        },
            function (err) {
                 if (that.config.callbackFail) {
                     that.config.callbackFail(err, that.config.parameters);
                 }
            }
        );

But in the 'err' paramater I don't have the exception info, I have the message:

'NetworkError: 500 Internal Server Error - http:/.../Services/Applications/Metrawa/ServiceManagement/WSJob.asmx/Filter'

and inspecting the call to server with firebug, in the response tab I can see strange characters, like if the exception hasn't been serialized.

What I want is to get the exception info in javascript. I've searched some info about this, like to create an own serializable exception class, but it doesn´t work neither. Any solution?

Thanks in advance.

Upvotes: 2

Views: 3267

Answers (2)

Mark Redman
Mark Redman

Reputation: 24515

I would catch the exception server side and always return a valid object with a "Result" property:

eg res.Result (as "OK" or "ERR" etc and handle that on the client)

Upvotes: 1

Alex R.
Alex R.

Reputation: 4754

You may opt to handle the exception in your server-side code and send it back as JSON, XML or even String--i.e. something your client can understand.

Upvotes: 2

Related Questions