Skip to content

Commit 16c8d51

Browse files
author
Eilon Lipton
committed
Fixes #76 - improve error messages in Hosting
Removed the TODOs from messages, and generally made them more readable.
1 parent 6bf5eab commit 16c8d51

File tree

5 files changed

+65
-25
lines changed

5 files changed

+65
-25
lines changed

src/Microsoft.AspNet.Hosting/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void Main(string[] args)
6767
catch (Exception ex)
6868
{
6969
var logger = loggerFactory.Create<Program>();
70-
logger.WriteError("TODO: Dispose threw an exception", ex);
70+
logger.WriteError("Dispose threw an exception.", ex);
7171
}
7272
shutdownHandle.Set();
7373
});

src/Microsoft.AspNet.Hosting/Server/ServerLoader.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public IServerFactory LoadServerFactory(string serverFactoryIdentifier)
3232
var assembly = Assembly.Load(new AssemblyName(assemblyName));
3333
if (assembly == null)
3434
{
35-
throw new Exception(String.Format("TODO: assembly {0} failed to load message", assemblyName));
35+
throw new Exception(string.Format("The assembly {0} failed to load.", assemblyName));
3636
}
3737

3838
Type type = null;
@@ -50,7 +50,7 @@ public IServerFactory LoadServerFactory(string serverFactoryIdentifier)
5050

5151
if (type == null)
5252
{
53-
throw new Exception(String.Format("TODO: type {0} failed to load message", typeName ?? "<null>"));
53+
throw new Exception(string.Format("The type {0} failed to load.", typeName ?? "<null>"));
5454
}
5555
}
5656
else
@@ -59,14 +59,14 @@ public IServerFactory LoadServerFactory(string serverFactoryIdentifier)
5959

6060
if (type == null)
6161
{
62-
throw new Exception(String.Format("TODO: type {0} failed to load message", typeName ?? "<null>"));
62+
throw new Exception(String.Format("The type {0} failed to load.", typeName ?? "<null>"));
6363
}
6464

6565
interfaceInfo = type.GetTypeInfo().ImplementedInterfaces.FirstOrDefault(interf => interf.Equals(typeof(IServerFactory)));
6666

6767
if (interfaceInfo == null)
6868
{
69-
throw new Exception("TODO: IServerFactory interface not found");
69+
throw new Exception(string.Format("The type '{0}' does not implement the '{1}' interface.", type, typeof(IServerFactory).FullName));
7070
}
7171
}
7272

src/Microsoft.AspNet.Hosting/Startup/StartupLoader.cs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ private MethodInfo FindMethod(Type startupType, string methodName, string enviro
3131
{
3232
if (required)
3333
{
34-
throw new Exception(string.Format("TODO: {0} or {1} method not found",
34+
throw new Exception(string.Format("A method named '{0}' or '{1}' in the type '{2}' could not be found.",
3535
methodNameWithEnv,
36-
methodNameWithNoEnv));
36+
methodNameWithNoEnv,
37+
startupType.FullName));
3738

3839
}
3940
return null;
@@ -42,8 +43,10 @@ private MethodInfo FindMethod(Type startupType, string methodName, string enviro
4243
{
4344
if (required)
4445
{
45-
throw new Exception(string.Format("TODO: {0} method does not return " + returnType.Name,
46-
methodInfo.Name));
46+
throw new Exception(string.Format("The '{0}' method in the type '{1}' must have a return type of '{2}'.",
47+
methodInfo.Name,
48+
startupType.FullName,
49+
returnType.Name));
4750
}
4851
return null;
4952
}
@@ -75,10 +78,11 @@ private object Invoke(MethodInfo methodInfo, object instance, IApplicationBuilde
7578
catch (Exception)
7679
{
7780
throw new Exception(string.Format(
78-
"TODO: Unable to resolve service for {0} method {1} {2}",
79-
methodInfo.Name,
81+
"Could not resolve a service of type '{0}' for the parameter '{1}' of method '{2}' on type '{3}'.",
82+
parameterInfo.ParameterType.FullName,
8083
parameterInfo.Name,
81-
parameterInfo.ParameterType.FullName));
84+
methodInfo.Name,
85+
methodInfo.DeclaringType.FullName));
8286
}
8387
}
8488
}
@@ -98,26 +102,26 @@ public Action<IApplicationBuilder> LoadStartup(
98102
var assembly = Assembly.Load(new AssemblyName(applicationName));
99103
if (assembly == null)
100104
{
101-
throw new Exception(String.Format("TODO: assembly {0} failed to load message", applicationName));
105+
throw new Exception(String.Format("The assembly '{0}' failed to load.", applicationName));
102106
}
103107

104-
var startupName1 = "Startup" + environmentName;
105-
var startupName2 = "Startup";
108+
var startupNameWithEnv = "Startup" + environmentName;
109+
var startupNameWithoutEnv = "Startup";
106110

107111
// Check the most likely places first
108112
var type =
109-
assembly.GetType(startupName1) ??
110-
assembly.GetType(applicationName + "." + startupName1) ??
111-
assembly.GetType(startupName2) ??
112-
assembly.GetType(applicationName + "." + startupName2);
113+
assembly.GetType(startupNameWithEnv) ??
114+
assembly.GetType(applicationName + "." + startupNameWithEnv) ??
115+
assembly.GetType(startupNameWithoutEnv) ??
116+
assembly.GetType(applicationName + "." + startupNameWithoutEnv);
113117

114118
if (type == null)
115119
{
116120
// Full scan
117121
var definedTypes = assembly.DefinedTypes.ToList();
118122

119-
var startupType1 = definedTypes.Where(info => info.Name.Equals(startupName1, StringComparison.Ordinal));
120-
var startupType2 = definedTypes.Where(info => info.Name.Equals(startupName2, StringComparison.Ordinal));
123+
var startupType1 = definedTypes.Where(info => info.Name.Equals(startupNameWithEnv, StringComparison.Ordinal));
124+
var startupType2 = definedTypes.Where(info => info.Name.Equals(startupNameWithoutEnv, StringComparison.Ordinal));
121125

122126
var typeInfo = startupType1.Concat(startupType2).FirstOrDefault();
123127
if (typeInfo != null)
@@ -128,9 +132,9 @@ public Action<IApplicationBuilder> LoadStartup(
128132

129133
if (type == null)
130134
{
131-
throw new Exception(String.Format("TODO: {0} or {1} class not found in assembly {2}",
132-
startupName1,
133-
startupName2,
135+
throw new Exception(String.Format("A type named '{0}' or '{1}' could not be found in assembly '{2}'.",
136+
startupNameWithEnv,
137+
startupNameWithoutEnv,
134138
applicationName));
135139
}
136140

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using Microsoft.AspNet.Builder;
5+
6+
namespace Microsoft.AspNet.Hosting.Fakes
7+
{
8+
public class StartupWithConfigureServicesNotResolved
9+
{
10+
public StartupWithConfigureServicesNotResolved()
11+
{
12+
}
13+
14+
public void Configure(IApplicationBuilder builder, int notAService)
15+
{
16+
}
17+
}
18+
}

test/Microsoft.AspNet.Hosting.Tests/StartupManagerTests.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,25 @@ public void StartupWithNoConfigureThrows()
114114
var diagnosticMessages = new List<string>();
115115

116116
var ex = Assert.Throws<Exception>(() => loader.LoadStartup("Microsoft.AspNet.Hosting.Tests", "Boom", diagnosticMessages));
117-
Assert.True(ex.Message.Contains("ConfigureBoom or Configure method not found"));
117+
Assert.Equal("A method named 'ConfigureBoom' or 'Configure' in the type 'Microsoft.AspNet.Hosting.Fakes.StartupBoom' could not be found.", ex.Message);
118+
}
119+
120+
[Fact]
121+
public void StartupWithConfigureServicesNotResolvedThrows()
122+
{
123+
var serviceCollection = HostingServices.Create();
124+
var services = serviceCollection.BuildServiceProvider();
125+
var loader = services.GetRequiredService<IStartupLoader>();
126+
var diagnosticMessages = new List<string>();
127+
128+
var startup = loader.LoadStartup("Microsoft.AspNet.Hosting.Tests", "WithConfigureServicesNotResolved", diagnosticMessages);
129+
130+
131+
var app = new ApplicationBuilder(services);
132+
133+
var ex = Assert.Throws<Exception>(() => startup.Invoke(app));
134+
135+
Assert.Equal("Could not resolve a service of type 'System.Int32' for the parameter 'notAService' of method 'Configure' on type 'Microsoft.AspNet.Hosting.Fakes.StartupWithConfigureServicesNotResolved'.", ex.Message);
118136
}
119137

120138
[Fact]

0 commit comments

Comments
 (0)