From a20ff88c51109b0e7472294fae17603d191dccbc Mon Sep 17 00:00:00 2001 From: "Valeriano A.R" Date: Thu, 8 Jul 2021 02:32:37 +0200 Subject: [PATCH] VAR.WebFormsCore: Change error handling --- VAR.WebFormsCore/Code/GlobalErrorHandler.cs | 55 +++++++------ .../Code/GlobalRouterMiddleware.cs | 13 +-- VAR.WebFormsCore/Pages/FrmEcho.cs | 8 +- VAR.WebFormsCore/Pages/Page.cs | 80 +++++++++++-------- 4 files changed, 84 insertions(+), 72 deletions(-) diff --git a/VAR.WebFormsCore/Code/GlobalErrorHandler.cs b/VAR.WebFormsCore/Code/GlobalErrorHandler.cs index cf539b6..33bc5be 100644 --- a/VAR.WebFormsCore/Code/GlobalErrorHandler.cs +++ b/VAR.WebFormsCore/Code/GlobalErrorHandler.cs @@ -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("

Internal error

"); - Exception exAux = ex; - //if (exAux is HttpUnhandledException && exAux.InnerException != null) { exAux = exAux.InnerException; } - while (exAux != null) + try { - sbOutput.AppendFormat("

Message: {0}

", exAux.Message); - sbOutput.AppendFormat("

StackTrace:

{0}
", 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(""); - } - 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(""); + } + + 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); } } diff --git a/VAR.WebFormsCore/Code/GlobalRouterMiddleware.cs b/VAR.WebFormsCore/Code/GlobalRouterMiddleware.cs index c10758a..1cb3e65 100644 --- a/VAR.WebFormsCore/Code/GlobalRouterMiddleware.cs +++ b/VAR.WebFormsCore/Code/GlobalRouterMiddleware.cs @@ -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); } } diff --git a/VAR.WebFormsCore/Pages/FrmEcho.cs b/VAR.WebFormsCore/Pages/FrmEcho.cs index 7ea1ccc..3073d86 100644 --- a/VAR.WebFormsCore/Pages/FrmEcho.cs +++ b/VAR.WebFormsCore/Pages/FrmEcho.cs @@ -8,11 +8,11 @@ namespace VAR.WebFormsCore.Pages { #region IHttpHandler - public void ProcessRequest(HttpContext context) + public async void ProcessRequest(HttpContext context) { - context.Response.WriteAsync("
");
-            context.Response.WriteAsync(JsonWriter.WriteObject(context.Request, indent: true));
-            context.Response.WriteAsync("
"); + await context.Response.WriteAsync("
");
+            await context.Response.WriteAsync(JsonWriter.WriteObject(context.Request, indent: true));
+            await context.Response.WriteAsync("
"); } #endregion IHttpHandler diff --git a/VAR.WebFormsCore/Pages/Page.cs b/VAR.WebFormsCore/Pages/Page.cs index 87938cd..7552101 100644 --- a/VAR.WebFormsCore/Pages/Page.cs +++ b/VAR.WebFormsCore/Pages/Page.cs @@ -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 controls = ChildsOfType(); - 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 controls = ChildsOfType(); + 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;