public virtual void Handle(HttpStatusCode statusCode, NancyContext context)
{
object errorHandled;
if (context.Items.TryGetValue("ERROR_HANDLED", out errorHandled) && (errorHandled as bool? ?? false))
return;
object exceptionObject;
if (!context.Items.TryGetValue("ERROR_EXCEPTION", out exceptionObject))
return;
var exception = UnwrapException((Exception)exceptionObject);
// We're not that interested in Nancys exception really
if (exception is RequestExecutionException)
exception = exception.InnerException;
if (exception is ResourceNotFoundException)
{
context.Response = new NotFoundResponse();
return;
}
if (exception is ResourcePreconditionFailedException)
{
context.Response = new Response
{
StatusCode = HttpStatusCode.PreconditionFailed,
ContentType = "text/html"
};
return;
}
var resp = new Response();
object errorTrace;
context.Items.TryGetValue("ERROR_TRACE", out errorTrace);
resp.Contents = stream =>
{
using (var streamWriter = new StreamWriter(stream))
{
if (exception != null)
{
streamWriter.WriteLine("Exception:");
streamWriter.WriteLine(exception);
}
if (errorTrace != null)
{
streamWriter.WriteLine("Trace:");
streamWriter.WriteLine(errorTrace);
}
streamWriter.WriteLine("Ey.. Got an exception there matey!!");
}
};
resp.ContentType = "text/plain";
resp.StatusCode = HttpStatusCode.InternalServerError;
context.Response = resp;
}