Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
57
rated 0 times [  63] [ 6]  / answers: 1 / hits: 25567  / 8 Years ago, mon, march 21, 2016, 12:00:00

The error handler is added like this at client:



$.connection.hub.url = /signalr;
$.connection.hub.logging = true;
$.connection.hub.error(function(error) {
console.log('SignalrAdapter: ' + error);
});

$.connection.hub.start().done(function() { me.onStartDone(); });
// ...


At server it is:



hubConfiguration.EnableDetailedErrors = true;


Accordingly to the docs this should be enough.



At my exception throwing it just displays a log text for it and does not invoke the handler:



[18:18:19 GMT+0000()] SignalR: ... [[[my error description here]]] ... 


At my cshtml page:



<script src=~/Scripts/vendor/jquery.signalR-2.1.2.js></script>
<script src=~/signalr/hubs></script>


However if I attach an error handler to the method itself it is got called:



$.connection.operatorHub.server.myMethodName(someParam).fail(function(error) {
console.log(Error handler called: + error);
});


How to handle a general error?



UPDATE.
The answer is below. Also see these:





Hope it helps someone.


More From » asp.net

 Answers
16

I tested on a little chat project (downloaded here) it seems this method handle only connection errors.



$.connection.hub.error(function(error) {
console.log('SignalrAdapter: ' + error);
});


I was able to handle all exceptions with the HubPipelineModule class.



1) I created a SOHubPipelineModule



public class SOHubPipelineModule : HubPipelineModule
{
protected override void OnIncomingError(ExceptionContext exceptionContext,
IHubIncomingInvokerContext invokerContext)
{
dynamic caller = invokerContext.Hub.Clients.Caller;
caller.ExceptionHandler(exceptionContext.Error.Message);
}
}


2) I Added the module to GlobalHost.HubPipeline



  // Any connection or hub wire up and configuration should go here
GlobalHost.HubPipeline.AddModule(new SOHubPipelineModule());
var hubConfiguration = new HubConfiguration { EnableDetailedErrors = true };
app.MapSignalR(hubConfiguration);


3) My ChatHub class :



 public class ChatHub : Hub
{
public void Send(string name, string message)
{
throw new Exception(exception for Artyom);
Clients.All.broadcastMessage(name, message);
}

}


4) In the js I use this code to get my exception message :



$.connection.chatHub.client.exceptionHandler = function (error) {
console.log('SignalrAdapter: ' + error);
alert('SignalrAdapter: ' + error);
};


enter


[#62854] Saturday, March 19, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
triston

Total Points: 545
Total Questions: 88
Total Answers: 94

Location: Lebanon
Member since Sun, Aug 2, 2020
4 Years ago
;