Skip to content

Commit 6cddb3a

Browse files
authored
Wrap subscription exceptions passed along via OnError (#754)
1 parent a85774f commit 6cddb3a

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

src/Transports.Subscriptions.Abstractions/Subscription.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Linq;
55
using System.Reactive.Linq;
66
using System.Threading.Tasks;
7+
using GraphQL.Execution;
78
using GraphQL.Subscription;
89
using GraphQL.Transport;
910
using Microsoft.Extensions.Logging;
@@ -56,7 +57,31 @@ public void OnCompleted()
5657
_unsubscribe = null;
5758
}
5859

59-
public void OnError(Exception error) => throw new NotImplementedException();
60+
/// <summary>
61+
/// Handles errors that are raised from the source, wrapping the error
62+
/// in an <see cref="ExecutionResult"/> instance and sending it to the client.
63+
/// </summary>
64+
public void OnError(Exception error)
65+
{
66+
_logger.LogDebug("Subscription: {subscriptionId} got error", Id);
67+
68+
// exceptions should already be wrapped by the GraphQL engine
69+
if (error is not ExecutionError executionError)
70+
{
71+
// but in the unlikely event that an unhandled exception delegate throws an exception,
72+
// or for any other reason it is not an ExecutionError instance, wrap the error
73+
executionError = new UnhandledError($"Unhandled error of type {error?.GetType().Name}", error!);
74+
}
75+
76+
// pass along the error as an execution result instance
77+
OnNext(new ExecutionResult { Errors = new ExecutionErrors { executionError } });
78+
79+
// Optionally we can disconnect the client by calling OnComplete() here
80+
//
81+
// https://docs.microsoft.com/en-us/dotnet/standard/events/observer-design-pattern-best-practices
82+
// > Once the provider calls the OnError or IObserver<T>.OnCompleted method, there should be
83+
// > no further notifications, and the provider can unsubscribe its observers.
84+
}
6085

6186
public void OnNext(ExecutionResult value)
6287
{

0 commit comments

Comments
 (0)