Skip to content
This repository has been archived by the owner on Jan 24, 2021. It is now read-only.

Commit

Permalink
Merge pull request #2458 from thecodejunkie/update-error-page
Browse files Browse the repository at this point in the history
  • Loading branch information
jchannon committed May 26, 2016
2 parents 9005a97 + 2c001e3 commit c5af3dc
Show file tree
Hide file tree
Showing 10 changed files with 459 additions and 517 deletions.
26 changes: 15 additions & 11 deletions src/Nancy.ViewEngines.DotLiquid/DotLiquidViewEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public IEnumerable<string> Extensions
}

/// <summary>
/// Initialise the view engine (if necessary)
/// Initialize the view engine (if necessary)
/// </summary>
/// <param name="viewEngineStartupContext">Startup context</param>
public void Initialize(ViewEngineStartupContext viewEngineStartupContext)
Expand Down Expand Up @@ -100,13 +100,15 @@ public Response RenderView(ViewLocationResult viewLocationResult, dynamic model,
status = HttpStatusCode.InternalServerError;

// Build the error message
String errorMessage = String.Format("Syntax error in liquid view '{0}':\r\n\r\n{1}",
String.Format("{0}/{1}.{2}", viewLocationResult.Location, viewLocationResult.Name, viewLocationResult.Extension),
var errorMessage = string.Format("Syntax error in liquid view <strong>'{0}'</strong><br/><br/>{1}",
string.Format("{0}/{1}.{2}", viewLocationResult.Location, viewLocationResult.Name, viewLocationResult.Extension),
syntaxException.Message);

// Create the error model with a Nancy DynamicDictionary because i can ;)
DynamicDictionary errorModel = new DynamicDictionary();
errorModel.Add(new KeyValuePair<string, dynamic>("ErrorMessage", errorMessage));
var errorModel = new DynamicDictionary
{
new KeyValuePair<string, dynamic>("ErrorMessage", errorMessage)
};

// Hash up the Error model so DotLiquid will understand it
hashedModel =
Expand All @@ -116,18 +118,20 @@ public Response RenderView(ViewLocationResult viewLocationResult, dynamic model,
});

// Grab the error HTML from the embedded resource and build up the DotLiquid template.
String errorHtml = LoadResource(@"500.liquid");
var errorHtml = LoadResource(@"500.liquid");
parsed = Template.Parse(errorHtml);
}
catch (Exception ex)
{
status = HttpStatusCode.InternalServerError;
// Build the error message
String errorMessage = String.Format("Error: {0}", ex.Message);
var errorMessage = string.Format("Error: {0}", ex.Message);

// Create the error model with a Nancy DynamicDictionary because i can ;)
DynamicDictionary errorModel = new DynamicDictionary();
errorModel.Add(new KeyValuePair<string, dynamic>("ErrorMessage", errorMessage));
var errorModel = new DynamicDictionary
{
new KeyValuePair<string, dynamic>("ErrorMessage", errorMessage)
};

// Hash up the Error model so DotLiquid will understand it
hashedModel =
Expand All @@ -137,7 +141,7 @@ public Response RenderView(ViewLocationResult viewLocationResult, dynamic model,
});

// Grab the error HTML from the embedded resource
String errorHtml = LoadResource(@"500.liquid");
var errorHtml = LoadResource(@"500.liquid");
parsed = Template.Parse(errorHtml);
}

Expand All @@ -154,7 +158,7 @@ public Response RenderView(ViewLocationResult viewLocationResult, dynamic model,

private static string LoadResource(string filename)
{
var resourceStream = typeof(DotLiquidViewEngine).Assembly.GetManifestResourceStream(String.Format("Nancy.ViewEngines.DotLiquid.Resources.{0}", filename));
var resourceStream = typeof(DotLiquidViewEngine).Assembly.GetManifestResourceStream(string.Format("Nancy.ViewEngines.DotLiquid.Resources.{0}", filename));

if (resourceStream == null)
{
Expand Down
235 changes: 108 additions & 127 deletions src/Nancy.ViewEngines.DotLiquid/Resources/500.liquid

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Nancy.ViewEngines.Razor/NancyRazorErrorView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class NancyRazorErrorView : NancyRazorViewBase
{
private readonly TraceConfiguration traceConfiguration;

private const string DisplayErrorTracesFalseMessage = "Error details are currently disabled.<br />To enable it, please set <code>TraceConfiguration.DisplayErrorTraces</code> to true.<br />For example by overriding your Bootstrapper's <code>Configure</code> method and calling <code>environment.Tracing(enabled: false, displayErrorTraces: true);</code>.";
private const string DisplayErrorTracesFalseMessage = "Error details are currently disabled.<br />To enable it, please set <strong>TraceConfiguration.DisplayErrorTraces</strong> to <strong>true</strong>.<br />For example by overriding your Bootstrapper's <strong>Configure</strong> method and calling<br/> <strong>environment.Tracing(enabled: false, displayErrorTraces: true);</strong>.";

private static string template;

Expand Down
10 changes: 5 additions & 5 deletions src/Nancy.ViewEngines.Razor/RazorViewEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,11 @@ private static string BuildErrorMessage(EmitResult result, ViewLocationResult vi
var lineNumber = 1;

var errorDetails = string.Format(
"Error compiling template: <strong>{0}</strong><br/><br/>Errors:<br/>{1}<br/><br/>Details:<br/>{2}<br/><br/>Compilation Source:<br/><pre><code>{3}</code></pre>",
"Template: <strong>{0}</strong><br/><br/>Errors:<ul>{1}</ul>Details:<br/><pre>{2}</pre><br/>Compilation Source:<br/><pre><code>{3}</code></pre>",
fullTemplateName,
errorMessages,
templateLines.Aggregate((s1, s2) => s1 + "<br/>" + s2),
compilationSource.Aggregate((s1, s2) => s1 + "<br/>Line " + lineNumber++ + ":\t" + s2));
compilationSource.Aggregate((s1, s2) => s1 + "<br/><span class='lineNumber'>Line " + lineNumber++ + ":</span>\t" + s2.Replace("<", "&gt;").Replace(">", "&lt;")));

return errorDetails;
}
Expand Down Expand Up @@ -333,10 +333,10 @@ private static string BuildErrorMessages(IEnumerable<Diagnostic> errors, IList<s

if (lineNumber > 0)
{
templateLines[lineNumber - 1] = string.Format("<span class='error'><a name='{0}' />{1}</span>", lineNumber, templateLines[lineNumber - 1]);
templateLines[lineNumber - 1] = string.Format("<a name='{0}'><span class='error'>{1}</span></a>", lineNumber, templateLines[lineNumber - 1]);

return string.Format(
"[{0}] Line: {1} Column: {2} - {3} (<a class='LineLink' href='#{1}'>show</a>)",
"<li>[{0}] Line: {1} Column: {2} - {3} (<a href='#{1}'>show</a>)</li>",
error.Id,
lineNumber,
error.Location.GetLineSpan().StartLinePosition.Character,
Expand All @@ -348,7 +348,7 @@ private static string BuildErrorMessages(IEnumerable<Diagnostic> errors, IList<s
}).Where(x => x != null).ToArray();

return (messages.Any())
? messages.Aggregate((s1, s2) => s1 + "<br/>" + s2)
? string.Join(string.Empty, messages)
: string.Empty;
}

Expand Down
266 changes: 124 additions & 142 deletions src/Nancy.ViewEngines.Razor/Resources/CompilationError.html

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions src/Nancy/ErrorHandling/DefaultStatusCodeHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Nancy.ErrorHandling
/// </summary>
public class DefaultStatusCodeHandler : IStatusCodeHandler
{
private const string DisplayErrorTracesFalseMessage = "Error details are currently disabled.<br />To enable it, please set <code>TraceConfiguration.DisplayErrorTraces</code> to true.<br />For example by overriding your Bootstrapper's <code>Configure</code> method and calling <code>environment.Tracing(enabled: false, displayErrorTraces: true);</code>.";
private const string DisplayErrorTracesFalseMessage = "Error details are currently disabled.<br />To enable it, please set <strong>TraceConfiguration.DisplayErrorTraces</strong> to <strong>true</strong>.<br />For example by overriding your Bootstrapper's <strong>Configure</strong> method and calling<br/> <strong>environment.Tracing(enabled: false, displayErrorTraces: true);</strong>.";

private readonly IDictionary<HttpStatusCode, string> errorMessages;
private readonly IDictionary<HttpStatusCode, string> errorPages;
Expand Down Expand Up @@ -86,7 +86,11 @@ public void Handle(HttpStatusCode statusCode, NancyContext context)
// from swapping a view model with a `DefaultStatusCodeHandlerResult`
context.NegotiationContext = new NegotiationContext();

var result = new DefaultStatusCodeHandlerResult(statusCode, this.errorMessages[statusCode], !this.configuration.DisplayErrorTraces ? DisplayErrorTracesFalseMessage : context.GetExceptionDetails());
var details = !this.configuration.DisplayErrorTraces
? DisplayErrorTracesFalseMessage
: string.Concat("<pre>", context.GetExceptionDetails().Replace("<", "&gt;").Replace(">", "&lt;"), "</pre>");

var result = new DefaultStatusCodeHandlerResult(statusCode, this.errorMessages[statusCode], details);
try
{
context.Response = this.responseNegotiator.NegotiateResponse(result, context);
Expand Down
165 changes: 81 additions & 84 deletions src/Nancy/ErrorHandling/Resources/404.html

Large diffs are not rendered by default.

243 changes: 108 additions & 135 deletions src/Nancy/ErrorHandling/Resources/500.html

Large diffs are not rendered by default.

17 changes: 9 additions & 8 deletions src/Nancy/ViewEngines/ViewNotFoundException.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Nancy.ViewEngines
{
using System;
using System.Linq;

/// <summary>
/// Exception that is thrown when a view could not be located.
Expand Down Expand Up @@ -29,14 +30,14 @@ public ViewNotFoundException(string viewName, string[] availableViewEngineExtens
this.AvailableViewEngineExtensions = availableViewEngineExtensions;
this.InspectedLocations = inspectedLocations;

this.message = String.Format(
"Unable to locate view '{0}'{4}Currently available view engine extensions: {1}{4}Locations inspected: {2}{4}Root path: {3}{4}" +
"If you were expecting raw data back, make sure you set the 'Accept'-header of the request to correct format, for example 'application/json'",
this.ViewName,
string.Join(",", this.AvailableViewEngineExtensions),
string.Join(",", this.InspectedLocations),
this.rootPathProvider.GetRootPath(),
Environment.NewLine);
this.message = string.Format(
"{4}Unable to locate requested view{4}{4} \u2022 Name: {0}{4} \u2022 Root path: {3}{4} \u2022 Supported extensions: {4}{1} \u2022 Inspected locations: {4}{2}{4}" +
"If you were expecting raw data back, make sure you set the 'Accept'-header of the request to correct format, for example 'application/json'{4}",
this.ViewName,
string.Join(string.Empty, this.AvailableViewEngineExtensions.Select(x => string.Concat(" - ", x, Environment.NewLine))),
string.Join(string.Empty, this.InspectedLocations.Select(x => string.Concat(" - ", x, Environment.NewLine))),
this.rootPathProvider.GetRootPath(),
Environment.NewLine);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ public async Task Should_throw_exception_if_view_location_fails()

// Then
Assert.NotNull(result);
Assert.Contains("Unable to locate view", result.ToString());
Assert.Contains("Unable to locate requested view", result.ToString());
}

[Fact]
Expand Down Expand Up @@ -653,7 +653,7 @@ public async Task Should_not_try_and_serve_view_with_invalid_name()
var result = await RecordAsync.Exception(() => browser.Get("/invalid-view-name"));

// Then
Assert.True(result.ToString().Contains("Unable to locate view"));
Assert.True(result.ToString().Contains("Unable to locate requested view"));
}

[Fact]
Expand Down

0 comments on commit c5af3dc

Please sign in to comment.