dotnet / dotnet/aspnetcore

`HubException` message is always "wrapped", contrary to documentation

Open
#64,714 2 comments 0 reactions 0 assignees View on GitHub
area-signalr
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

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

According to the [SignalR documentation](https://learn.microsoft.com/en-us/aspnet/core/signalr/hubs?view=aspnetcore-9.0#handle-errors), 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:
https://github.com/dotnet/aspnetcore/blob/a9aaa320f1c4c771b2dee8c000409a5f04397339/src/SignalR/server/Core/src/Internal/ErrorMessageHelper.cs#L10-L13

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:
- #7722
- #12633
- #11891

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?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.