Skip to content
This repository has been archived by the owner on Dec 19, 2018. It is now read-only.

Catch startup exceptions and show them in the browser. #370

Merged
merged 1 commit into from
Sep 25, 2015

Conversation

Tratcher
Copy link
Member

#77 @davidfowl @DamianEdwards @muratg @Eilon

Catch any application startup related exceptions and display them in the browser rather than on the console. Note this only applies to Hosting.Program.Main scenarios, not to self-hosted WebHostBuilder or to TestHost
Handled exceptions:

  • App compilation failures
  • Missing Startup class
  • Startup's constructor or static constructor throw, or fail to resolve dependencies
  • Startup does not have the expected methods.
  • ConfigureServices or Configure throws

image

The error details are only displayed if your in Development. Otherwise you get:
image

There is one open question:
Should we attempt any content-negotiation to avoid displaying HTML to non-browser clients?

@DamianEdwards
Copy link
Member

So pretty

@DamianEdwards
Copy link
Member

RE the content-negotiation question: That sounds really nice actually. In the case that the client doesn't send an text/html accept, what would we return in the body? Nothing at all?

@davidfowl
Copy link
Member

RE content-negotiation: no 😄

@DamianEdwards
Copy link
Member

What did we decide to do RE making this easier to debug in non-development environment? Where does this message go in that case?

@DamianEdwards
Copy link
Member

Spoke to @Tratcher, decided we should also print this message (sans-HTML) to stdout. With the HttpPlatformHandler config this will go to the file so you can see the error easily in Azure, etc.

@davidfowl
Copy link
Member

Sweet! We need to figure out if we want to use development as the only switch. We wouldn't want people to change the env to Development do we?

@DamianEdwards
Copy link
Member

@davidfowl we would not. Are you saying there should be a knob to allow show detailed errors that overrides the development check? Or are you referring to the stdout printing?

@davidfowl
Copy link
Member

Yep. That horrible one we had before ASPNET_DETAILED_ERRORS=true

@DamianEdwards
Copy link
Member

Yep sounds good. Can you add that pls @Tratcher?

}


#if !DNXCORE50
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They added StackFrame to CoreCLR https://www.nuget.org/packages/System.Diagnostics.StackTrace/4.0.1-beta-23225 though you;ll have to see if it's usable 😄

@Tratcher
Copy link
Member Author

Updated

public FailSafeStartup(bool showDetails, IRuntimeEnvironment env, Exception ex)
{
// Write errors to standard out so they can be retrieved when not in development mode.
Console.Out.WriteLine("Application startup exception: " + ex.ToString());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Log as well. One thing we said was that logging is always good to do. Console.WriteLine is the thing to question.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can't log, logging is app configured and probably wasn't set up yet.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can log. Hosting creates the logger factory.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the logs don't go anywhere until the app configures them, and the app failed to start.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't assume that, we should just log. You can imagine hosting registering an EventLogProvider on windows so that critical exceptions like this show up there.

@Tratcher
Copy link
Member Author

Updated.

_runtimeEnv = runtimeEnv;
_loggerFactory = loggerFactory;
ShowDetailedErrors = hostingEnv.IsDevelopment()
|| string.Equals("true", Environment.GetEnvironmentVariable("ASPNET_DETAILED_ERRORS"), StringComparison.OrdinalIgnoreCase);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true || 1 ?

@Tratcher
Copy link
Member Author

Redone.


// Write errors to standard out so they can be retrieved when not in development mode.
Console.Out.WriteLine("Application startup exception: " + ex.ToString());
var logger = _applicationServices.GetRequiredService<ILogger<HostingEngine>>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dont use Logger<T> it's hella inefficient (unfortunately) 😄. Get the LoggerFactory

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we just delete that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its like LINQ, nice to use in some cases but mostly easy to do something in efficient. It's worth trying to see why it's so slow. TBH tho, this code path is error path so it doesn't matter that much.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Plus you'll make @DamianEdwards cry, but thats ok

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This executes once per app and only in a catastrophic failure scenario, forget perf.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ya

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happened to my beloved ILogger<T>?!?! 😭

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I say keep ILogger<HostingEngine> since perf is not that important in the failure case.

@davidfowl
Copy link
Member

minor changes, this change looks much better

@Tratcher
Copy link
Member Author

Updated.

@Tratcher
Copy link
Member Author

ping @davidfowl

@davidfowl
Copy link
Member

:shipit:

@davidfowl
Copy link
Member

Thought you already merged this change

@davidfowl
Copy link
Member

😄

@Tratcher Tratcher merged commit a9e7948 into dev Sep 25, 2015
@Tratcher Tratcher deleted the tratcher/errorpage branch September 25, 2015 04:46
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants