VAR.WebFormsCore: Change error handling

This commit is contained in:
2021-07-08 02:32:37 +02:00
parent 25dca639e6
commit a20ff88c51
4 changed files with 84 additions and 72 deletions

View File

@@ -1,5 +1,6 @@
using System; using System;
using System.Text; using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using VAR.WebFormsCore.Pages; using VAR.WebFormsCore.Pages;
@@ -9,42 +10,45 @@ namespace VAR.WebFormsCore.Code
{ {
#region Private methods #region Private methods
private static void ShowInternalError(HttpContext context, Exception ex) private static async Task ShowInternalErrorAsync(HttpContext context, Exception ex)
{ {
context.Response.StatusCode = 500; try
//context.Response.Clear();
StringBuilder sbOutput = new StringBuilder();
sbOutput.Append("<h2>Internal error</h2>");
Exception exAux = ex;
//if (exAux is HttpUnhandledException && exAux.InnerException != null) { exAux = exAux.InnerException; }
while (exAux != null)
{ {
sbOutput.AppendFormat("<p><b>Message:</b> {0}</p>", exAux.Message); context.Response.StatusCode = 500;
sbOutput.AppendFormat("<p><b>StackTrace:</b></p> <pre><code>{0}</code></pre>", exAux.StackTrace);
exAux = exAux.InnerException;
}
// Fill response to 512 bytes to avoid browser "beauty" response of errors. StringBuilder sbOutput = new StringBuilder();
long fillResponse = 512 - sbOutput.Length; sbOutput.Append("<h2>Internal error</h2>");
if (fillResponse > 0) Exception exAux = ex;
{ while (exAux != null)
sbOutput.Append("<!--");
for (int i = 0; i < fillResponse; i++)
{ {
sbOutput.Append("A"); sbOutput.AppendFormat("<p><b>Message:</b> {0}</p>", exAux.Message);
sbOutput.AppendFormat("<p><b>StackTrace:</b></p> <pre><code>{0}</code></pre>", exAux.StackTrace);
exAux = exAux.InnerException;
} }
sbOutput.Append("-->");
}
context.Response.WriteAsync(sbOutput.ToString()); // Fill response to 512 bytes to avoid browser "beauty" response of errors.
long fillResponse = 512 - sbOutput.Length;
if (fillResponse > 0)
{
sbOutput.Append("<!--");
for (int i = 0; i < fillResponse; i++)
{
sbOutput.Append("A");
}
sbOutput.Append("-->");
}
await context.Response.WriteAsync(sbOutput.ToString());
await context.Response.Body.FlushAsync();
}
catch { /* Nom nom nom */ }
} }
#endregion Private methods #endregion Private methods
#region Public methods #region Public methods
public static void HandleError(HttpContext context, Exception ex) public static async Task HandleErrorAsync(HttpContext context, Exception ex)
{ {
try try
{ {
@@ -52,10 +56,11 @@ namespace VAR.WebFormsCore.Code
//context.Response.Clear(); //context.Response.Clear();
//context.Handler = frmError; //context.Handler = frmError;
frmError.ProcessRequest(context); frmError.ProcessRequest(context);
await context.Response.Body.FlushAsync();
} }
catch catch
{ {
ShowInternalError(context, ex); await ShowInternalErrorAsync(context, ex);
} }
} }

View File

@@ -39,16 +39,11 @@ namespace VAR.WebFormsCore.Code
{ {
if (IsIgnoreException(ex) == false) if (IsIgnoreException(ex) == false)
{ {
try // TODO: Implement better error logging
{ Console.WriteLine("!!!!!!!!!!");
// TODO: Implement better error logging Console.Write("Message: {0}\nStacktrace: {1}\n", ex.Message, ex.StackTrace);
Console.WriteLine("!!!!!!!!!!");
Console.Write("Message: {0}\nStacktrace: {1}\n", ex.Message, ex.StackTrace);
GlobalErrorHandler.HandleError(httpContext, ex); await GlobalErrorHandler.HandleErrorAsync(httpContext, ex);
await httpContext.Response.Body.FlushAsync();
}
catch (Exception) { /* Nom nom nom */}
} }
} }

View File

@@ -8,11 +8,11 @@ namespace VAR.WebFormsCore.Pages
{ {
#region IHttpHandler #region IHttpHandler
public void ProcessRequest(HttpContext context) public async void ProcessRequest(HttpContext context)
{ {
context.Response.WriteAsync("<pre><code>"); await context.Response.WriteAsync("<pre><code>");
context.Response.WriteAsync(JsonWriter.WriteObject(context.Request, indent: true)); await context.Response.WriteAsync(JsonWriter.WriteObject(context.Request, indent: true));
context.Response.WriteAsync("</code></pre>"); await context.Response.WriteAsync("</code></pre>");
} }
#endregion IHttpHandler #endregion IHttpHandler

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Text; using System.Text;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
@@ -17,48 +18,59 @@ namespace VAR.WebFormsCore.Pages
public async void ProcessRequest(HttpContext context) public async void ProcessRequest(HttpContext context)
{ {
StringWriter stringWriter = new(); try
Context = context;
Page = this;
if (context.Request.Method == "POST")
{ {
_isPostBack = true; StringWriter stringWriter = new();
}
OnPreInit(); Context = context;
if (context.Response.HasStarted) { return; } Page = this;
OnInit(); if (context.Request.Method == "POST")
if (context.Response.HasStarted) { return; }
OnLoad();
if (context.Response.HasStarted) { return; }
if (_isPostBack)
{
List<Control> controls = ChildsOfType<IReceivePostbackEvent>();
foreach (Control control in controls)
{ {
string clientID = control.ClientID; _isPostBack = true;
if (context.Request.Form.ContainsKey(clientID)) }
OnPreInit();
if (context.Response.HasStarted) { return; }
OnInit();
if (context.Response.HasStarted) { return; }
OnLoad();
if (context.Response.HasStarted) { return; }
if (_isPostBack)
{
List<Control> controls = ChildsOfType<IReceivePostbackEvent>();
foreach (Control control in controls)
{ {
(control as IReceivePostbackEvent).ReceivePostBack(); string clientID = control.ClientID;
if (context.Response.HasStarted) { return; } if (context.Request.Form.ContainsKey(clientID))
{
(control as IReceivePostbackEvent).ReceivePostBack();
if (context.Response.HasStarted) { return; }
}
} }
} }
OnPreRender();
if (context.Response.HasStarted) { return; }
Render(stringWriter);
if (context.Response.HasStarted) { return; }
context.Response.Headers.Add("Content-Type", "text/html");
byte[] byteObject = _utf8Econding.GetBytes(stringWriter.ToString());
await context.Response.Body.WriteAsync(byteObject);
} }
catch (Exception ex)
{
// TODO: Implement better error logging
Console.WriteLine("!!!!!!!!!!");
Console.Write("Message: {0}\nStacktrace: {1}\n", ex.Message, ex.StackTrace);
OnPreRender(); await GlobalErrorHandler.HandleErrorAsync(context, ex);
if (context.Response.HasStarted) { return; } }
Render(stringWriter);
if (context.Response.HasStarted) { return; }
context.Response.Headers.Add("Content-Type", "text/html");
byte[] byteObject = _utf8Econding.GetBytes(stringWriter.ToString());
await context.Response.Body.WriteAsync(byteObject);
} }
private bool _isPostBack = false; private bool _isPostBack = false;