46 lines
1.4 KiB
C#
46 lines
1.4 KiB
C#
using System;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Net.Http.Formatting;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Web.Http;
|
|
using System.Web.Http.ExceptionHandling;
|
|
|
|
namespace Thermo.Active
|
|
{
|
|
public class WebApiUnhandledExceptionHandler : ExceptionHandler
|
|
{
|
|
public override void Handle(ExceptionHandlerContext context)
|
|
{
|
|
context.Result = new TextPlainErrorResult
|
|
{
|
|
Request = context.ExceptionContext.Request,
|
|
Exception = context.Exception,
|
|
HttpStatusCode = HttpStatusCode.InternalServerError
|
|
};
|
|
}
|
|
|
|
private class TextPlainErrorResult : IHttpActionResult
|
|
{
|
|
public HttpRequestMessage Request { get; set; }
|
|
|
|
public Exception Exception { get; set; }
|
|
|
|
public HttpStatusCode HttpStatusCode { get; set; }
|
|
|
|
public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
|
|
{
|
|
HttpError error = new HttpError(Exception, true);
|
|
HttpResponseMessage response = new HttpResponseMessage()
|
|
{
|
|
Content = new ObjectContent<HttpError>(error, new JsonMediaTypeFormatter()),
|
|
StatusCode = HttpStatusCode,
|
|
RequestMessage = Request
|
|
};
|
|
|
|
return Task.FromResult(response);
|
|
}
|
|
}
|
|
}
|
|
} |