diff --git a/src/Microsoft.AspNetCore.SignalR.Common/Internal/Protocol/JsonHubProtocol.cs b/src/Microsoft.AspNetCore.SignalR.Common/Internal/Protocol/JsonHubProtocol.cs index 63f02fc235..a4ac61e899 100644 --- a/src/Microsoft.AspNetCore.SignalR.Common/Internal/Protocol/JsonHubProtocol.cs +++ b/src/Microsoft.AspNetCore.SignalR.Common/Internal/Protocol/JsonHubProtocol.cs @@ -80,7 +80,7 @@ namespace Microsoft.AspNetCore.SignalR.Internal.Protocol if (token == null || token.Type != JTokenType.Object) { - throw new FormatException($"Unexpected JSON Token Type '{token?.Type}'. Expected a JSON Object."); + throw new InvalidDataException($"Unexpected JSON Token Type '{token?.Type}'. Expected a JSON Object."); } var json = (JObject)token; @@ -102,12 +102,12 @@ namespace Microsoft.AspNetCore.SignalR.Internal.Protocol case HubProtocolConstants.PingMessageType: return PingMessage.Instance; default: - throw new FormatException($"Unknown message type: {type}"); + throw new InvalidDataException($"Unknown message type: {type}"); } } catch (JsonReaderException jrex) { - throw new FormatException("Error reading JSON.", jrex); + throw new InvalidDataException("Error reading JSON.", jrex); } } } @@ -278,7 +278,7 @@ namespace Microsoft.AspNetCore.SignalR.Internal.Protocol var arguments = new object[args.Count]; if (paramTypes.Length != arguments.Length) { - throw new FormatException($"Invocation provides {arguments.Length} argument(s) but target expects {paramTypes.Length}."); + throw new InvalidDataException($"Invocation provides {arguments.Length} argument(s) but target expects {paramTypes.Length}."); } try @@ -293,7 +293,7 @@ namespace Microsoft.AspNetCore.SignalR.Internal.Protocol } catch (Exception ex) { - throw new FormatException("Error binding arguments. Make sure that the types of the provided values match the types of the hub method being invoked.", ex); + throw new InvalidDataException("Error binding arguments. Make sure that the types of the provided values match the types of the hub method being invoked.", ex); } } @@ -314,7 +314,7 @@ namespace Microsoft.AspNetCore.SignalR.Internal.Protocol if (error != null && resultProp != null) { - throw new FormatException("The 'error' and 'result' properties are mutually exclusive."); + throw new InvalidDataException("The 'error' and 'result' properties are mutually exclusive."); } if (resultProp == null) diff --git a/src/Microsoft.AspNetCore.SignalR.Common/Internal/Protocol/JsonUtils.cs b/src/Microsoft.AspNetCore.SignalR.Common/Internal/Protocol/JsonUtils.cs index 2086d027d3..bbcd3d1461 100644 --- a/src/Microsoft.AspNetCore.SignalR.Common/Internal/Protocol/JsonUtils.cs +++ b/src/Microsoft.AspNetCore.SignalR.Common/Internal/Protocol/JsonUtils.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.IO; using Newtonsoft.Json.Linq; namespace Microsoft.AspNetCore.SignalR.Internal.Protocol @@ -26,7 +27,7 @@ namespace Microsoft.AspNetCore.SignalR.Internal.Protocol if (prop == null) { - throw new FormatException($"Missing required property '{property}'."); + throw new InvalidDataException($"Missing required property '{property}'."); } return GetValue(property, expectedType, prop); @@ -36,7 +37,7 @@ namespace Microsoft.AspNetCore.SignalR.Internal.Protocol { if (expectedType != JTokenType.None && prop.Type != expectedType) { - throw new FormatException($"Expected '{property}' to be of type {expectedType}."); + throw new InvalidDataException($"Expected '{property}' to be of type {expectedType}."); } return prop.Value(); } diff --git a/src/Microsoft.AspNetCore.SignalR.Common/Internal/Protocol/NegotiationProtocol.cs b/src/Microsoft.AspNetCore.SignalR.Common/Internal/Protocol/NegotiationProtocol.cs index 8c2ead483d..11a2ee81fe 100644 --- a/src/Microsoft.AspNetCore.SignalR.Common/Internal/Protocol/NegotiationProtocol.cs +++ b/src/Microsoft.AspNetCore.SignalR.Common/Internal/Protocol/NegotiationProtocol.cs @@ -33,7 +33,7 @@ namespace Microsoft.AspNetCore.SignalR.Internal.Protocol { if (!TextMessageParser.TryParseMessage(ref input, out var payload)) { - throw new FormatException("Unable to parse payload as a negotiation message."); + throw new InvalidDataException("Unable to parse payload as a negotiation message."); } using (var memoryStream = new MemoryStream(payload.ToArray())) @@ -43,7 +43,7 @@ namespace Microsoft.AspNetCore.SignalR.Internal.Protocol var token = JToken.ReadFrom(reader); if (token == null || token.Type != JTokenType.Object) { - throw new FormatException($"Unexpected JSON Token Type '{token?.Type}'. Expected a JSON Object."); + throw new InvalidDataException($"Unexpected JSON Token Type '{token?.Type}'. Expected a JSON Object."); } var negotiationJObject = (JObject)token; diff --git a/test/Microsoft.AspNetCore.SignalR.Common.Tests/Internal/Protocol/JsonHubProtocolTests.cs b/test/Microsoft.AspNetCore.SignalR.Common.Tests/Internal/Protocol/JsonHubProtocolTests.cs index 1b07927a71..3d421cc710 100644 --- a/test/Microsoft.AspNetCore.SignalR.Common.Tests/Internal/Protocol/JsonHubProtocolTests.cs +++ b/test/Microsoft.AspNetCore.SignalR.Common.Tests/Internal/Protocol/JsonHubProtocolTests.cs @@ -149,7 +149,7 @@ namespace Microsoft.AspNetCore.SignalR.Common.Tests.Internal.Protocol var binder = new TestBinder(); var protocol = new JsonHubProtocol(); - var ex = Assert.Throws(() => protocol.TryParseMessages(Encoding.UTF8.GetBytes(input), binder, out var messages)); + var ex = Assert.Throws(() => protocol.TryParseMessages(Encoding.UTF8.GetBytes(input), binder, out var messages)); Assert.Equal(expectedMessage, ex.Message); } @@ -165,7 +165,7 @@ namespace Microsoft.AspNetCore.SignalR.Common.Tests.Internal.Protocol var binder = new TestBinder(paramTypes: new[] { typeof(int), typeof(string) }, returnType: typeof(bool)); var protocol = new JsonHubProtocol(); protocol.TryParseMessages(Encoding.UTF8.GetBytes(input), binder, out var messages); - var ex = Assert.Throws(() => ((HubMethodInvocationMessage)messages[0]).Arguments); + var ex = Assert.Throws(() => ((HubMethodInvocationMessage)messages[0]).Arguments); Assert.Equal(expectedMessage, ex.Message); } diff --git a/test/Microsoft.AspNetCore.SignalR.Common.Tests/Internal/Protocol/NegotiationProtocolTests.cs b/test/Microsoft.AspNetCore.SignalR.Common.Tests/Internal/Protocol/NegotiationProtocolTests.cs index bff7c60462..6f2985a497 100644 --- a/test/Microsoft.AspNetCore.SignalR.Common.Tests/Internal/Protocol/NegotiationProtocolTests.cs +++ b/test/Microsoft.AspNetCore.SignalR.Common.Tests/Internal/Protocol/NegotiationProtocolTests.cs @@ -36,7 +36,7 @@ namespace Microsoft.AspNetCore.SignalR.Common.Tests.Internal.Protocol { var message = Encoding.UTF8.GetBytes(payload); - var exception = Assert.Throws(() => + var exception = Assert.Throws(() => Assert.True(NegotiationProtocol.TryParseMessage(message, out var deserializedMessage))); Assert.Equal(expectedMessage, exception.Message);