Skip to content

HubException message is always "wrapped", contrary to documentation #64714

@milesizzo

Description

@milesizzo

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

According to the SignalR documentation, when throwing a HubException from a hub method, SignalR should "[send] the entire exception message to the client, unmodified." However, the actual behavior wraps the message:

// Server
throw new HubException("{\"code\":0}");

// Client receives
"An unexpected error occurred invoking 'MethodName' on the server. HubException: {\"code\":0}"

This is caused by ErrorMessageHelper.BuildErrorMessage() which - in the case of HubException - formats the message as:

if (exception is HubException || includeExceptionDetails)
{
return $"{message} {exception.GetType().Name}: {exception.Message}";
}

This occurs when throwing the exception from a hub method and from a filter.

Describe the solution you'd like

The HubException.Message should be passed through unmodified to the client, as documented.

Alternatively, update the documentation to clarify this behaviour.

Additional context

Here is a sample use-case:

Passing JSON error payloads to clients:

// Server
throw new HubException("{\"code\":\"USER_NOT_FOUND\"}");

// Client receives
"An unexpected error occurred invoking 'GetUser' on the server. HubException: {\"code\":\"USER_NOT_FOUND\"}"

The client cannot directly deserialize this as JSON without first stripping the prefix.

This is similar to the following issues:

However I feel it is different enough to warrant a separate report.

If this behaviour is not desired, what is the recommended method for handling/parsing errors on the client?

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-signalrIncludes: SignalR clients and servers

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions