Provide a better error message when invoking a non-existant hub method (#2768)

This commit is contained in:
BrennanConroy 2018-08-08 15:55:33 -07:00 committed by GitHub
parent c9104d4932
commit a550ae6cc3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 14 deletions

View File

@ -136,7 +136,7 @@ namespace Microsoft.AspNetCore.SignalR.Internal
{ {
if (!_methods.TryGetValue(methodName, out var descriptor)) if (!_methods.TryGetValue(methodName, out var descriptor))
{ {
return Type.EmptyTypes; throw new HubException("Method does not exist.");
} }
return descriptor.ParameterTypes; return descriptor.ParameterTypes;
} }

View File

@ -157,10 +157,10 @@ namespace Microsoft.AspNetCore.SignalR.Protocol
} }
var target = ReadString(input, ref offset, "target"); var target = ReadString(input, ref offset, "target");
var parameterTypes = binder.GetParameterTypes(target);
try try
{ {
var parameterTypes = binder.GetParameterTypes(target);
var arguments = BindArguments(input, ref offset, parameterTypes, resolver); var arguments = BindArguments(input, ref offset, parameterTypes, resolver);
return ApplyHeaders(headers, new InvocationMessage(invocationId, target, arguments)); return ApplyHeaders(headers, new InvocationMessage(invocationId, target, arguments));
} }
@ -175,10 +175,10 @@ namespace Microsoft.AspNetCore.SignalR.Protocol
var headers = ReadHeaders(input, ref offset); var headers = ReadHeaders(input, ref offset);
var invocationId = ReadInvocationId(input, ref offset); var invocationId = ReadInvocationId(input, ref offset);
var target = ReadString(input, ref offset, "target"); var target = ReadString(input, ref offset, "target");
var parameterTypes = binder.GetParameterTypes(target);
try try
{ {
var parameterTypes = binder.GetParameterTypes(target);
var arguments = BindArguments(input, ref offset, parameterTypes, resolver); var arguments = BindArguments(input, ref offset, parameterTypes, resolver);
return ApplyHeaders(headers, new StreamInvocationMessage(invocationId, target, arguments)); return ApplyHeaders(headers, new StreamInvocationMessage(invocationId, target, arguments));
} }

View File

@ -464,7 +464,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests
bool ExpectedErrors(WriteContext writeContext) bool ExpectedErrors(WriteContext writeContext)
{ {
return writeContext.LoggerName == DefaultHubDispatcherLoggerName && return writeContext.LoggerName == DefaultHubDispatcherLoggerName &&
writeContext.EventId.Name == "UnknownHubMethod"; writeContext.EventId.Name == "FailedInvokingHubMethod";
} }
var hubProtocol = HubProtocols[hubProtocolName]; var hubProtocol = HubProtocols[hubProtocolName];
@ -476,7 +476,40 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests
await connection.StartAsync().OrTimeout(); await connection.StartAsync().OrTimeout();
var ex = await Assert.ThrowsAsync<HubException>(() => connection.InvokeAsync("!@#$%")).OrTimeout(); var ex = await Assert.ThrowsAsync<HubException>(() => connection.InvokeAsync("!@#$%")).OrTimeout();
Assert.Equal("Unknown hub method '!@#$%'", ex.Message); Assert.Equal("Failed to invoke '!@#$%' due to an error on the server. HubException: Method does not exist.", ex.Message);
}
catch (Exception ex)
{
loggerFactory.CreateLogger<HubConnectionTests>().LogError(ex, "{ExceptionType} from test", ex.GetType().FullName);
throw;
}
finally
{
await connection.DisposeAsync().OrTimeout();
}
}
}
[Theory]
[MemberData(nameof(HubProtocolsAndTransportsAndHubPaths))]
public async Task ServerThrowsHubExceptionIfHubMethodCannotBeResolvedAndArgumentsPassedIn(string hubProtocolName, HttpTransportType transportType, string hubPath)
{
bool ExpectedErrors(WriteContext writeContext)
{
return writeContext.LoggerName == DefaultHubDispatcherLoggerName &&
writeContext.EventId.Name == "FailedInvokingHubMethod";
}
var hubProtocol = HubProtocols[hubProtocolName];
using (StartVerifiableLog(out var loggerFactory, $"{nameof(ServerThrowsHubExceptionIfHubMethodCannotBeResolvedAndArgumentsPassedIn)}_{hubProtocol.Name}_{transportType}_{hubPath.TrimStart('/')}", expectedErrorsFilter: ExpectedErrors))
{
var connection = CreateHubConnection(hubPath, transportType, hubProtocol, loggerFactory);
try
{
await connection.StartAsync().OrTimeout();
var ex = await Assert.ThrowsAsync<HubException>(() => connection.InvokeAsync("!@#$%", 10, "test")).OrTimeout();
Assert.Equal("Failed to invoke '!@#$%' due to an error on the server. HubException: Method does not exist.", ex.Message);
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -563,7 +596,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests
bool ExpectedErrors(WriteContext writeContext) bool ExpectedErrors(WriteContext writeContext)
{ {
return writeContext.LoggerName == DefaultHubDispatcherLoggerName && return writeContext.LoggerName == DefaultHubDispatcherLoggerName &&
writeContext.EventId.Name == "UnknownHubMethod"; writeContext.EventId.Name == "FailedInvokingHubMethod";
} }
var hubProtocol = HubProtocols[hubProtocolName]; var hubProtocol = HubProtocols[hubProtocolName];
@ -576,7 +609,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests
var channel = await connection.StreamAsChannelAsync<int>("!@#$%"); var channel = await connection.StreamAsChannelAsync<int>("!@#$%");
var ex = await Assert.ThrowsAsync<HubException>(() => channel.ReadAllAsync().OrTimeout()); var ex = await Assert.ThrowsAsync<HubException>(() => channel.ReadAllAsync().OrTimeout());
Assert.Equal("Unknown hub method '!@#$%'", ex.Message); Assert.Equal("Failed to invoke '!@#$%' due to an error on the server. HubException: Method does not exist.", ex.Message);
} }
catch (Exception ex) catch (Exception ex)
{ {

View File

@ -799,7 +799,7 @@ namespace Microsoft.AspNetCore.SignalR.Tests
var result = await client.InvokeAsync(nameof(MethodHub.OnDisconnectedAsync)).OrTimeout(); var result = await client.InvokeAsync(nameof(MethodHub.OnDisconnectedAsync)).OrTimeout();
Assert.Equal("Unknown hub method 'OnDisconnectedAsync'", result.Error); Assert.Equal("Failed to invoke 'OnDisconnectedAsync' due to an error on the server. HubException: Method does not exist.", result.Error);
// kill the connection // kill the connection
client.Dispose(); client.Dispose();
@ -837,7 +837,7 @@ namespace Microsoft.AspNetCore.SignalR.Tests
var result = await client.InvokeAsync(nameof(MethodHub.StaticMethod)).OrTimeout(); var result = await client.InvokeAsync(nameof(MethodHub.StaticMethod)).OrTimeout();
Assert.Equal("Unknown hub method 'StaticMethod'", result.Error); Assert.Equal("Failed to invoke 'StaticMethod' due to an error on the server. HubException: Method does not exist.", result.Error);
// kill the connection // kill the connection
client.Dispose(); client.Dispose();
@ -858,16 +858,16 @@ namespace Microsoft.AspNetCore.SignalR.Tests
var connectionHandlerTask = await client.ConnectAsync(connectionHandler); var connectionHandlerTask = await client.ConnectAsync(connectionHandler);
var result = await client.InvokeAsync(nameof(MethodHub.ToString)).OrTimeout(); var result = await client.InvokeAsync(nameof(MethodHub.ToString)).OrTimeout();
Assert.Equal("Unknown hub method 'ToString'", result.Error); Assert.Equal("Failed to invoke 'ToString' due to an error on the server. HubException: Method does not exist.", result.Error);
result = await client.InvokeAsync(nameof(MethodHub.GetHashCode)).OrTimeout(); result = await client.InvokeAsync(nameof(MethodHub.GetHashCode)).OrTimeout();
Assert.Equal("Unknown hub method 'GetHashCode'", result.Error); Assert.Equal("Failed to invoke 'GetHashCode' due to an error on the server. HubException: Method does not exist.", result.Error);
result = await client.InvokeAsync(nameof(MethodHub.Equals)).OrTimeout(); result = await client.InvokeAsync(nameof(MethodHub.Equals)).OrTimeout();
Assert.Equal("Unknown hub method 'Equals'", result.Error); Assert.Equal("Failed to invoke 'Equals' due to an error on the server. HubException: Method does not exist.", result.Error);
result = await client.InvokeAsync(nameof(MethodHub.ReferenceEquals)).OrTimeout(); result = await client.InvokeAsync(nameof(MethodHub.ReferenceEquals)).OrTimeout();
Assert.Equal("Unknown hub method 'ReferenceEquals'", result.Error); Assert.Equal("Failed to invoke 'ReferenceEquals' due to an error on the server. HubException: Method does not exist.", result.Error);
// kill the connection // kill the connection
client.Dispose(); client.Dispose();
@ -889,7 +889,7 @@ namespace Microsoft.AspNetCore.SignalR.Tests
var result = await client.InvokeAsync(nameof(MethodHub.Dispose)).OrTimeout(); var result = await client.InvokeAsync(nameof(MethodHub.Dispose)).OrTimeout();
Assert.Equal("Unknown hub method 'Dispose'", result.Error); Assert.Equal("Failed to invoke 'Dispose' due to an error on the server. HubException: Method does not exist.", result.Error);
// kill the connection // kill the connection
client.Dispose(); client.Dispose();