Skip to content

Commit 6c2d22d

Browse files
committed
Fixes #141
[R] Typos
1 parent 017582b commit 6c2d22d

8 files changed

+72
-22
lines changed

src/Simplify.Web/Diagnostics/DetailedExceptionInfoBuilder.cs

+16-8
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public static class DetailedExceptionInfoBuilder
1313
/// Builds the detailed exception info.
1414
/// </summary>
1515
/// <param name="e">The exception.</param>
16-
/// <returns></returns>
17-
public static string? Build(Exception e)
16+
/// <param name="htmlFormatting">if set to <c>true</c> then HTML formatting will be added to text.</param>
17+
public static string? Build(Exception e, bool htmlFormatting)
1818
{
1919
var trace = new StackTrace(e, true);
2020

@@ -28,12 +28,18 @@ public static class DetailedExceptionInfoBuilder
2828
? ""
2929
: $"[{fileLineNumber}:{fileColumnNumber}]";
3030

31-
return
32-
$"<b>{positionPrefix} {e.GetType()} : {e.Message}</b>{Environment.NewLine}{trace}{BuildInnerExceptionData(1, e.InnerException)}"
33-
.Replace(Environment.NewLine, "<br />");
31+
var result = (htmlFormatting ? "<b>" : "")
32+
+ $"{positionPrefix} {e.GetType()} : {e.Message}"
33+
+ (htmlFormatting ? "</b>" : "")
34+
+ $"{Environment.NewLine}{trace}{BuildInnerExceptionData(1, e.InnerException, htmlFormatting)}";
35+
36+
if (htmlFormatting)
37+
result = result.Replace(Environment.NewLine, "<br />");
38+
39+
return result;
3440
}
3541

36-
private static string? BuildInnerExceptionData(int currentLevel, Exception? e)
42+
private static string? BuildInnerExceptionData(int currentLevel, Exception? e, bool htmlFormatting)
3743
{
3844
if (e == null)
3945
return null;
@@ -54,8 +60,10 @@ public static class DetailedExceptionInfoBuilder
5460
? " " + currentLevel.ToString(CultureInfo.InvariantCulture)
5561
: "";
5662

57-
return
58-
$"<br /><b>[Inner Exception{levelText}]{positionPrefix} {e.GetType()} : {e.Message}</b>{Environment.NewLine}{trace}{BuildInnerExceptionData(currentLevel + 1, e.InnerException)}";
63+
return (htmlFormatting ? "<br /><b>" : "")
64+
+ $"[Inner Exception{levelText}]{positionPrefix} {e.GetType()} : {e.Message}"
65+
+ (htmlFormatting ? "</b>" : "")
66+
+ $"{Environment.NewLine}{trace}{BuildInnerExceptionData(currentLevel + 1, e.InnerException, htmlFormatting)}";
5967
}
6068
}
6169
}

src/Simplify.Web/Diagnostics/ErrorPageGenerator.cs

+15-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace Simplify.Web.Diagnostics
55
{
66
/// <summary>
7-
/// Provides exception information HTML page generator
7+
/// Provides HTTP 500 error page generator
88
/// </summary>
99
public static class ErrorPageGenerator
1010
{
@@ -14,11 +14,19 @@ public static class ErrorPageGenerator
1414
/// <param name="e">Exception to get information from.</param>
1515
/// <param name="hideExceptionDetails">if set to <c>true</c> then exception details will not be shown.</param>
1616
/// <param name="darkStyle"><c>true</c> if page style should be in dark colors.</param>
17-
/// <returns></returns>
18-
public static string Generate(Exception e, bool hideExceptionDetails = false, bool darkStyle = false) =>
19-
Http500ErrorPageBuilder.Build(hideExceptionDetails
20-
? null
21-
: DetailedExceptionInfoBuilder.Build(e),
22-
darkStyle);
17+
/// <param name="minimalStyle">If set to true then no HTML page style will be added.</param>
18+
public static string Generate(Exception e,
19+
bool hideExceptionDetails = false,
20+
bool darkStyle = false,
21+
bool minimalStyle = false)
22+
{
23+
var exceptionText = hideExceptionDetails
24+
? null
25+
: DetailedExceptionInfoBuilder.Build(e, !minimalStyle);
26+
27+
return minimalStyle
28+
? Http500MinimalErrorPageBuilder.Build(exceptionText)
29+
: Http500ErrorPageBuilder.Build(exceptionText, darkStyle);
30+
}
2331
}
2432
}

src/Simplify.Web/Diagnostics/ExceptionEventHandler.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ namespace Simplify.Web.Diagnostics
55
/// <summary>
66
/// Catched exceptions delegate
77
/// </summary>
8-
/// <param name="e">The e.</param>
8+
/// <param name="e">The exception.</param>
99
public delegate void ExceptionEventHandler(Exception e);
1010
}

src/Simplify.Web/Diagnostics/Templates/Http500ErrorPageBuilder.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ namespace Simplify.Web.Diagnostics.Templates
1010
public static class Http500ErrorPageBuilder
1111
{
1212
/// <summary>
13-
/// Builds the specified hide exception details.
13+
/// Builds the error page.
1414
/// </summary>
1515
/// <param name="exceptionText">The exception text.</param>
1616
/// <param name="darkStyle"><c>true</c> if page style should be in dark colors.</param>
17-
/// <returns></returns>
1817
public static string Build(string? exceptionText = null, bool darkStyle = false) =>
1918
TemplateBuilder.FromCurrentAssembly("Diagnostics.Templates.Http500ErrorPage.html")
2019
.Build()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
HTTP 500 - Internal Server Error
2+
{ExceptionText}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Simplify.Templates;
2+
3+
namespace Simplify.Web.Diagnostics.Templates
4+
{
5+
/// <summary>
6+
/// Provides HTML HTTP 500 minimal error page builder
7+
/// </summary>
8+
9+
public class Http500MinimalErrorPageBuilder
10+
{
11+
/// <summary>
12+
/// Builds the error page.
13+
/// </summary>
14+
/// <param name="exceptionText">The exception text.</param>
15+
/// <returns></returns>
16+
public static string Build(string? exceptionText = null) =>
17+
TemplateBuilder.FromCurrentAssembly("Diagnostics.Templates.Http500MinimalErrorPage.html")
18+
.Build()
19+
.Set("ExceptionText", exceptionText)
20+
.Get();
21+
}
22+
}

src/Simplify.Web/RequestPipeline/SimplifyWebRequestMiddleware.cs

+14-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace Simplify.Web.RequestPipeline
1717
/// <summary>
1818
/// Simplify.Web request execution root
1919
/// </summary>
20-
public class SimplifyWebRequestMiddleware
20+
public static class SimplifyWebRequestMiddleware
2121
{
2222
/// <summary>
2323
/// Occurs when exception occurred and catched by framework.
@@ -58,7 +58,7 @@ public static async Task<RequestHandlingStatus> Invoke(HttpContext context)
5858
e = exception;
5959
}
6060

61-
await context.Response.WriteAsync(GenerateErrorPage(scope, e));
61+
await context.WriteErrorResponse(scope, e);
6262

6363
return RequestHandlingStatus.RequestWasHandled;
6464
}
@@ -73,11 +73,21 @@ internal static bool ProcessOnException(Exception e)
7373
return true;
7474
}
7575

76-
private static string GenerateErrorPage(ILifetimeScope scope, Exception e)
76+
private static async Task WriteErrorResponse(this HttpContext context, ILifetimeScope scope, Exception e)
77+
{
78+
var webContext = scope.Resolver.Resolve<IWebContextProvider>().Get();
79+
80+
if (webContext.IsAjax)
81+
context.Response.ContentType = "text/plain";
82+
83+
await context.Response.WriteAsync(scope.GenerateErrorResponse(e, webContext.IsAjax));
84+
}
85+
86+
private static string GenerateErrorResponse(this ILifetimeScope scope, Exception e, bool minimalStyle)
7787
{
7888
var settings = scope.Resolver.Resolve<ISimplifyWebSettings>();
7989

80-
return ErrorPageGenerator.Generate(e, settings.HideExceptionDetails, settings.ErrorPageDarkStyle);
90+
return ErrorPageGenerator.Generate(e, settings.HideExceptionDetails, settings.ErrorPageDarkStyle, minimalStyle);
8191
}
8292
}
8393
}

src/Simplify.Web/Simplify.Web.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<PackageTags>.NET web-framework MVC DI OWIN</PackageTags>
2929
</PropertyGroup>
3030
<ItemGroup>
31+
<EmbeddedResource Include="Diagnostics\Templates\Http500MinimalErrorPage.html" />
3132
<EmbeddedResource Include="Diagnostics\Templates\Http500ErrorPageExceptionInfo.html" />
3233
<EmbeddedResource Include="Diagnostics\Templates\Http500ErrorPage.html" />
3334
<EmbeddedResource Include="Diagnostics\Templates\Styles\Dark.css" />

0 commit comments

Comments
 (0)