using System;
using System.Net;
using System.Runtime.ExceptionServices;
using System.Threading.Tasks;
using System.Web;
using System.Web.Util;
using Swan.Logging;
namespace EmbedIO
{
///
///
///
The server has encountered an error and was not able to process your request.
"); text.Write("Please contact the server administrator"); if (!string.IsNullOrEmpty(ContactInformation)) text.Write(" ({0})", WebUtility.HtmlEncode(ContactInformation)); text.Write(", informing them of the time this error occurred and the action(s) you performed that resulted in this error.
"); text.Write("The following information may help them in finding out what happened and restoring full functionality.
"); text.Write( "Exception type: {0}
Message: {1}",
WebUtility.HtmlEncode(exception.GetType().FullName ?? "
Stack trace:
{0}",
WebUtility.HtmlEncode(exception.StackTrace));
}
});
internal static async Task Handle(string logSource, IHttpContext context, Exception exception, ExceptionHandlerCallback? handler, HttpExceptionHandlerCallback? httpHandler)
{
if (handler == null)
{
ExceptionDispatchInfo.Capture(exception).Throw();
return;
}
exception.Log(logSource, $"[{context.Id}] Unhandled exception.");
try
{
context.Response.SetEmptyResponse((int)HttpStatusCode.InternalServerError);
context.Response.DisableCaching();
await handler(context, exception)
.ConfigureAwait(false);
}
catch (OperationCanceledException) when (context.CancellationToken.IsCancellationRequested)
{
throw;
}
catch (HttpListenerException)
{
throw;
}
catch (Exception httpException) when (httpException is IHttpException httpException1)
{
if (httpHandler == null)
throw;
await httpHandler(context, httpException1).ConfigureAwait(false);
}
catch (Exception exception2)
{
exception2.Log(logSource, $"[{context.Id}] Unhandled exception while handling exception.");
}
}
}
}