Skip to content

Commit 3258f47

Browse files
committed
fixing ws subprot. impl: sending "Error" msg type only for "Subscribe"
"Error" is only intended as a response for "Subscribe" and includes of course any exception thrown during processing of "Subscribe".
1 parent 95e30b5 commit 3258f47

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

src/FSharp.Data.GraphQL.Server.AspNetCore/GraphQLWebsocketMiddleware.fs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -259,22 +259,26 @@ type GraphQLWebSocketMiddleware<'Root>
259259
| ValueNone -> do! ServerPong p |> sendMsg
260260
| ClientPong p -> nameof ClientPong |> logMsgReceivedWithOptionalPayload p
261261
| Subscribe (id, query) ->
262-
nameof Subscribe |> logMsgWithIdReceived id
263-
if subscriptions |> GraphQLSubscriptionsManagement.isIdTaken id then
264-
do!
265-
let warningMsg : FormattableString = $"Subscriber for Id = '{id}' already exists"
266-
logger.LogWarning (String.Format (warningMsg.Format, "id"), id)
267-
socket.CloseAsync (
268-
enum CustomWebSocketStatus.SubscriberAlreadyExists,
269-
warningMsg.ToString (),
270-
CancellationToken.None
271-
)
272-
else
273-
let variables = query.Variables |> Skippable.toOption
274-
let! planExecutionResult =
275-
let root = options.RootFactory httpContext
276-
options.SchemaExecutor.AsyncExecute (query.Query, root, ?variables = variables)
277-
do! planExecutionResult |> applyPlanExecutionResult id socket
262+
try
263+
nameof Subscribe |> logMsgWithIdReceived id
264+
if subscriptions |> GraphQLSubscriptionsManagement.isIdTaken id then
265+
do!
266+
let warningMsg : FormattableString = $"Subscriber for Id = '{id}' already exists"
267+
logger.LogWarning (String.Format (warningMsg.Format, "id"), id)
268+
socket.CloseAsync (
269+
enum CustomWebSocketStatus.SubscriberAlreadyExists,
270+
warningMsg.ToString (),
271+
CancellationToken.None
272+
)
273+
else
274+
let variables = query.Variables |> Skippable.toOption
275+
let! planExecutionResult =
276+
let root = options.RootFactory httpContext
277+
options.SchemaExecutor.AsyncExecute (query.Query, root, ?variables = variables)
278+
do! planExecutionResult |> applyPlanExecutionResult id socket
279+
with ex ->
280+
logger.LogError (ex, "Unexpected error during subscription with id '{id}'", id)
281+
do! sendMsg (Error (id, [new Shared.NameValueLookup ([ ("subscription", "Unexpected error during subscription" :> obj) ])]))
278282
| ClientComplete id ->
279283
"ClientComplete" |> logMsgWithIdReceived id
280284
subscriptions

0 commit comments

Comments
 (0)