SignalR and 500 errors

I played very well with SignalR, but I found a situation where I really could not find the document link or solution.

I have a server side function with a class parameter with 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 Foo property

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

Since this happens before the SignalR context starts, how to handle the client-side error? Does the hub have any special callbacks or properties to check? Is there anything special I need to do with a server-side hub?

Thanks!

+4
source share
1 answer

SignalR has some client-side logic that you can use to check if the call to the node method on the server side succeeded from the client or not.

First of all, to handle connection failures, you can use the error handler in 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 ) as follows:

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

Therefore, when I recreate your script by doing this on the server side:

 public bool AddThing(Thing thing) { return true; } public class Thing { public Double Foo { get; set; } } 

.. and then calls it on the client side:

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

The error handling function is called, and the text An error occurred... is printed on the console.

Another thing you can do is because calling a method on the server returns a pending jQuery object ( https://github.com/SignalR/SignalR/wiki/SignalR-JS-Client-Hubs ), you can link a couple of callbacks to the returned object out of a call. For example, the documentation shows an example:

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

It should be noted that fail is called only when an error occurs during the call of the hub (for example, an exception is thrown inside the method on the server side) - https://github.com/SignalR/SignalR/issues/404#issuecomment-6754425 .

Last note - I think this is an interesting question, because, as I see it, it is JSON Serializer that throws an exception - for example, in your case:

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

... but for sure, it would be interesting if there was some better method than described above for processing this scenario - for example, the ability to determine which particular hub call caused 500 Internal Server Error .

+10
source

All Articles