GlobalErrorHandler: Class to handle global errors
This commit is contained in:
54
Scrummer/Code/GlobalErrorHandler.cs
Normal file
54
Scrummer/Code/GlobalErrorHandler.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using Scrummer.Code.Pages;
|
||||
|
||||
namespace Scrummer.Code
|
||||
{
|
||||
public static class GlobalErrorHandler
|
||||
{
|
||||
#region Private methods
|
||||
|
||||
private static void ShowInternalError(HttpContext context, Exception ex)
|
||||
{
|
||||
context.Response.StatusCode = 500;
|
||||
context.Response.Clear();
|
||||
context.Response.Write("<h2>Internal error</h2>");
|
||||
context.Response.Write(String.Format("<p><b>Message:</b> {0}</p>", ex.Message));
|
||||
context.Response.Write(String.Format("<p><b>StackTrace:</b></p> <pre><code>{0}</code></pre>", ex.StackTrace));
|
||||
|
||||
// Fill response to 512 bytes to avoid browser "beauty" response of errors.
|
||||
long fillResponse = 512 - (ex.Message.Length + ex.StackTrace.Length); ;
|
||||
if (fillResponse > 0)
|
||||
{
|
||||
context.Response.Write("<!--");
|
||||
for (int i = 0; i < fillResponse; i++)
|
||||
{
|
||||
context.Response.Write("A");
|
||||
}
|
||||
context.Response.Write("-->");
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public methods
|
||||
|
||||
public static void HandleError(HttpContext context,Exception ex){
|
||||
try
|
||||
{
|
||||
IHttpHandler frmError = new FrmError(ex);
|
||||
context.Response.Clear();
|
||||
context.Handler = frmError;
|
||||
frmError.ProcessRequest(context);
|
||||
}
|
||||
catch
|
||||
{
|
||||
ShowInternalError(context, ex);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
30
Scrummer/Code/Pages/FrmError.cs
Normal file
30
Scrummer/Code/Pages/FrmError.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using System.Web.UI;
|
||||
using System.Web.UI.WebControls;
|
||||
using Scrummer.Code.Controls;
|
||||
|
||||
namespace Scrummer.Code.Pages
|
||||
{
|
||||
public class FrmError : Page
|
||||
{
|
||||
Exception _ex = null;
|
||||
|
||||
public FrmError(Exception ex)
|
||||
{
|
||||
_ex = ex;
|
||||
Init += FrmError_Init;
|
||||
}
|
||||
|
||||
void FrmError_Init(object sender, EventArgs e)
|
||||
{
|
||||
CLabel lblMessage = new CLabel { ID = "lblMessage", Text = _ex.Message, Tag = "P" };
|
||||
Controls.Add(lblMessage);
|
||||
|
||||
CLabel lblStackTrace = new CLabel { ID = "lblStackTrace", Text = _ex.StackTrace, Tag = "P" };
|
||||
Controls.Add(lblStackTrace);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,7 @@
|
||||
using System.Web;
|
||||
using System;
|
||||
using System.Web;
|
||||
using Scrummer.Code;
|
||||
using Scrummer.Code.JSON;
|
||||
|
||||
namespace Scrummer
|
||||
{
|
||||
@@ -10,8 +13,15 @@ namespace Scrummer
|
||||
}
|
||||
|
||||
public void ProcessRequest(HttpContext context)
|
||||
{
|
||||
try
|
||||
{
|
||||
context.Response.Write("Hello world!");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
GlobalErrorHandler.HandleError(context, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -72,10 +72,14 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Code\Controls\CLabel.cs" />
|
||||
<Compile Include="Code\GlobalErrorHandler.cs" />
|
||||
<Compile Include="Code\JSON\ParserContext.cs" />
|
||||
<Compile Include="GlobalRouter.cs" />
|
||||
<Compile Include="Code\JSON\JSONParser.cs" />
|
||||
<Compile Include="Code\JSON\JSONWriter.cs" />
|
||||
<Compile Include="Code\Pages\FrmError.cs">
|
||||
<SubType>ASPXCodeBehind</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<PropertyGroup>
|
||||
|
||||
Reference in New Issue
Block a user