11

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.

Community
  • 1
  • 1
Artyom
  • 3,507
  • 2
  • 34
  • 67

2 Answers2

15

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 image description here

Quentin Roger
  • 6,410
  • 2
  • 23
  • 36
  • Interesting way. Didn't know about `HubPipelineModule.OnIncomingError`. Tried the little chat project. It works there. But that didn't work at my project. Researching this. In my project I have signalr groups, autofac, owin and other. And at client... That seems complicated. – Artyom Mar 22 '16 at 12:24
  • And that seems like some hack. Why not get the error at the first place and not to make this second call to clients? – Artyom Mar 22 '16 at 12:26
  • I know it's ugly but I think its the only way to handle all exceptions for a hub :( – Quentin Roger Mar 22 '16 at 13:50
  • I think this solution is for ASP.NET SignalR and not ASP.NET Core SingnalR because HubPipelineModule is in namespace Microsoft.AspNet.SignalR.Hubs; Any solution for ASP.NET Core SignalR? – Simon Nov 07 '18 at 13:12
  • This is no longer an option in new SignalR versions for .NET Core. – korulis Jun 03 '21 at 07:53
9

I've used the error handling on the client like this:

It's for SignalR version 2

More detail: SignalR documentation - How to handle errors in the Hub class

Hub - AppHub.cs:

public class AppHub : Hub
    {
        public void Send(string message)
        {
            throw new HubException("Unauthorized", new { status = "401" });
        }
    }

Client-side:

$(function () {

            var appHub = $.connection.appHub;

            $.connection.hub.start().done(function () {

                appHub.server.send("test message")
                    .fail(function (e) {

                        if (e.source === 'HubException') {
                            console.error("Error message: ", e.message);
                            console.error("Error status: ", e.data.status);
                        }

                    });
            });

        });
Jenan
  • 3,408
  • 13
  • 62
  • 105