From 332b4293365569377e67a5a4eb51bc5e709d3785 Mon Sep 17 00:00:00 2001 From: "Alexey I.m2strng4dtwrld" Date: Sun, 11 Oct 2020 12:31:37 +0300 Subject: [PATCH] feat: provide exception messages for WebSocketException --- src/Fleck/WebSocketException.cs | 10 ++++++--- src/Fleck/WebSocketStatusCodes.cs | 36 +++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/Fleck/WebSocketException.cs b/src/Fleck/WebSocketException.cs index 2554aeea..5fb7f277 100644 --- a/src/Fleck/WebSocketException.cs +++ b/src/Fleck/WebSocketException.cs @@ -3,11 +3,10 @@ namespace Fleck { public class WebSocketException : Exception { - public WebSocketException(ushort statusCode) : base() + public WebSocketException(ushort statusCode) : this(statusCode, PrepareExceptionMessage(statusCode)) { - StatusCode = statusCode; } - + public WebSocketException(ushort statusCode, string message) : base(message) { StatusCode = statusCode; @@ -19,5 +18,10 @@ public WebSocketException(ushort statusCode, string message, Exception innerExce } public ushort StatusCode { get; private set;} + + private static string PrepareExceptionMessage(ushort statusCode) + { + return $"Exception with status code {statusCode}: {WebSocketStatusCodes.ConvertStatusCodeToString(statusCode)}"; + } } } diff --git a/src/Fleck/WebSocketStatusCodes.cs b/src/Fleck/WebSocketStatusCodes.cs index ca111224..50c18461 100644 --- a/src/Fleck/WebSocketStatusCodes.cs +++ b/src/Fleck/WebSocketStatusCodes.cs @@ -24,6 +24,42 @@ public static class WebSocketStatusCodes InvalidFramePayloadData, PolicyViolation, MessageTooBig, MandatoryExt, InternalServerError }; + + public static string ConvertStatusCodeToString(ushort statusCode) + { + switch (statusCode) + { + case 1000: + return nameof(NormalClosure); + case 1001: + return nameof(GoingAway); + case 1002: + return nameof(ProtocolError); + case 1003: + return nameof(UnsupportedDataType); + case 1005: + return nameof(NoStatusReceived); + case 1006: + return nameof(AbnormalClosure); + case 1007: + return nameof(InvalidFramePayloadData); + case 1008: + return nameof(PolicyViolation); + case 1009: + return nameof(MessageTooBig); + case 1010: + return nameof(MandatoryExt); + case 1011: + return nameof(InternalServerError); + case 1015: + return nameof(TLSHandshake); + case 3000: + return nameof(ApplicationError); + + default: + throw new ArgumentException($"Unexpected status status code: {statusCode}", nameof(statusCode)); + } + } } }