Jason
Jason

Reputation: 3040

SignalR and 500 Errors

I've been playing around with SignalR very successfully so far, but I've found a situation where I have not really been able to find any documented reference or solution.

I have a server side function with a class parameter that has a Double property

public bool AddThing(Thing thing)
{
    // add and notify client
}

public class Thing {
    public Double Foo { get; set; }
}

The server rightfully returns a 500 error if I send a Thing object with text instead of a number for the property Foo

{"hub":"ThingHub","method":"AddThing","args":[{"Foo":"bar"}],"state":{},"id":1}

Since this happens before the SignalR context kicks in, how can deal with the error at the client side? Does the Hub have any special callbacks or properties to check? Is there anything special I need to do to the Hub on the server side?

Thanks!

Upvotes: 5

Views: 5270

Answers (1)

Lasse Christiansen
Lasse Christiansen

Reputation: 10325

SignalR have some logic on the client side you can use to check if a server side hub method call from the client succeeded or not.

First of all, to handle connection failures you can use the error handler on the hub connection ( https://github.com/SignalR/SignalR/issues/404#issuecomment-6754425 , http://www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-javascript-client) like this:

$.connection.hub.error(function() {
    console.log('An error occurred...');
});

So when I recreate your scenario by implementing this on the server side:

public bool AddThing(Thing thing)
{
    return true;
}

public class Thing
{
    public Double Foo { get; set; }
}

.. and then calls this from the client side:

myHub.addThing({"Foo":"Bar"});

The error handling function is invoked, and the text An error occurred... is printed to the console.

Another thing you can do - because invoking a method on the server returns a jQuery deferred object ( https://github.com/SignalR/SignalR/wiki/SignalR-JS-Client-Hubs ) , you can chain a couple of callbacks on the returning object from the invoke. For example the documentation gives this sample:

myHub.someMethod()
     .done(function(result) {
     })
     .fail(function(error) {
     });

It should be noted though that fail is only called when there is an error during hub invocation ( e.g. an exception is thrown inside the server side method ) - https://github.com/SignalR/SignalR/issues/404#issuecomment-6754425.

A final note - I think this is an interesting question because, as I see it, it is the JSON Serializer which throws the exception - e.g., in your case:

Newtonsoft.Json.JsonSerializationException: Error converting value "Bar" to type 'System.Double'. Line 1, position 54. ---> System.FormatException:

... but for sure, it could be interesting if there were any better method than what I described above to handle this scenario - like being able to tell which exact hub invocation caused the 500 Internal Server Error.

Upvotes: 11

Related Questions