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.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using VAR.WebFormsCore.Pages;
@@ -9,42 +10,45 @@ namespace VAR.WebFormsCore.Code
{
#region Private methods
private static void ShowInternalError(HttpContext context, Exception ex)
private static async Task ShowInternalErrorAsync(HttpContext context, Exception ex)
{
context.Response.StatusCode = 500;
//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)
try
{
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;
}
context.Response.StatusCode = 500;
// 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++)
StringBuilder sbOutput = new StringBuilder();
sbOutput.Append("<h2>Internal error</h2>");
Exception exAux = ex;
while (exAux != null)
{
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
#region Public methods
public static void HandleError(HttpContext context, Exception ex)
public static async Task HandleErrorAsync(HttpContext context, Exception ex)
{
try
{
@@ -52,10 +56,11 @@ namespace VAR.WebFormsCore.Code
//context.Response.Clear();
//context.Handler = frmError;
frmError.ProcessRequest(context);
await context.Response.Body.FlushAsync();
}
catch
{
ShowInternalError(context, ex);
await ShowInternalErrorAsync(context, ex);
}
}

View File

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

View File

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

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Microsoft.AspNetCore.Http;
@@ -17,48 +18,59 @@ namespace VAR.WebFormsCore.Pages
public async void ProcessRequest(HttpContext context)
{
StringWriter stringWriter = new();
Context = context;
Page = this;
if (context.Request.Method == "POST")
try
{
_isPostBack = true;
}
StringWriter stringWriter = new();
OnPreInit();
if (context.Response.HasStarted) { return; }
Context = context;
Page = this;
OnInit();
if (context.Response.HasStarted) { return; }
OnLoad();
if (context.Response.HasStarted) { return; }
if (_isPostBack)
{
List<Control> controls = ChildsOfType<IReceivePostbackEvent>();
foreach (Control control in controls)
if (context.Request.Method == "POST")
{
string clientID = control.ClientID;
if (context.Request.Form.ContainsKey(clientID))
_isPostBack = true;
}
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();
if (context.Response.HasStarted) { return; }
string clientID = control.ClientID;
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();
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);
await GlobalErrorHandler.HandleErrorAsync(context, ex);
}
}
private bool _isPostBack = false;