Skip to content

Commit 9a171e4

Browse files
authored
Log prematurely closed connections as info, not as error (#291)
This is a common thing to happen and is unavoidable, so this should not be logged as an error IMO.
1 parent 9593e90 commit 9a171e4

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/Saturn/Channels.fs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ open System
99
open System.Collections.Concurrent
1010
open System.Collections.Generic
1111
open System.Net.WebSockets
12+
open System.Runtime.ExceptionServices
1213
open System.Threading
1314
open System.Threading.Tasks
1415

@@ -94,6 +95,14 @@ module Channels =
9495
| _ -> ()
9596
}
9697

98+
let (|WebSocketError|_|) (e: ExceptionDispatchInfo) =
99+
match e.SourceException with
100+
| sourceExn when sourceExn.InnerException |> isNull |> not ->
101+
match sourceExn.InnerException with
102+
| :? WebSocketException as webSocketExn -> Some webSocketExn
103+
| _ -> None
104+
| _ -> None
105+
97106
type SocketMiddleware(next : RequestDelegate, serializer: Giraffe.Json.ISerializer, path: string, channel: IChannel, sockets: SocketHub, logger: ILogger<SocketMiddleware>) =
98107
do sockets.NewPath path
99108

@@ -128,7 +137,10 @@ module Channels =
128137
logger.LogTrace(ex, "got message that was unable to be deserialized into a 'Message'")
129138
()
130139
| Error exn ->
131-
logger.LogError(exn.SourceException, "Error while receiving message")
140+
match exn with
141+
| WebSocketError socketError when socketError.WebSocketErrorCode = WebSocketError.ConnectionClosedPrematurely ->
142+
logger.LogInformation("WebSocket error: Connection closed prematurely")
143+
| _ -> logger.LogError(exn.SourceException, "Error while receiving message")
132144
() // TODO: ?
133145

134146
do! channel.Terminate (ctx, socketInfo)

0 commit comments

Comments
 (0)