Skip to content

Instantly share code, notes, and snippets.

@ccpu
Forked from Ciantic/ExceptionFilter.cs
Created December 9, 2020 18:05
Show Gist options
  • Select an option

  • Save ccpu/064b1e3e3fd93f67fabcf8d99eec69ef to your computer and use it in GitHub Desktop.

Select an option

Save ccpu/064b1e3e3fd93f67fabcf8d99eec69ef to your computer and use it in GitHub Desktop.

Revisions

  1. @Ciantic Ciantic revised this gist Apr 20, 2016. 1 changed file with 19 additions and 0 deletions.
    19 changes: 19 additions & 0 deletions ExceptionFilter.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    using System;
    using Microsoft.AspNetCore.Mvc.Filters;

    namespace Example
    {
    public class ApiExceptionFilter : Attribute, IExceptionFilter
    {
    public void OnException(ExceptionContext context)
    {
    if (context.Exception is Exception) {
    context.Result = new ObjectResult(new {
    Error = "Exception",
    Exception = context.Exception
    });
    context.Exception = null;
    }
    }
    }
    }
  2. @Ciantic Ciantic revised this gist Apr 20, 2016. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions ExceptionMiddleware.cs
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,7 @@
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.AspNetCore.Mvc.Internal;
    using Microsoft.Extensions.Options;
    using System;

    namespace Example
    {
  3. @Ciantic Ciantic revised this gist Apr 20, 2016. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions ExceptionMiddleware.cs
    Original file line number Diff line number Diff line change
    @@ -32,11 +32,11 @@ public async Task Invoke(HttpContext context)
    throw;
    }
    context.Response.StatusCode = 500;
    context.Response.Clear();
    context.Response.Clear();
    await _oex.ExecuteAsync(new ActionContext() { HttpContext = context }, new ObjectResult(new {
    Error = "Exception",
    Exception = e
    }));
    Error = "Exception",
    Exception = e
    }));
    }
    }
    }
  4. @Ciantic Ciantic revised this gist Apr 20, 2016. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions ExceptionMiddleware.cs
    Original file line number Diff line number Diff line change
    @@ -32,11 +32,11 @@ public async Task Invoke(HttpContext context)
    throw;
    }
    context.Response.StatusCode = 500;
    context.Response.Clear();
    context.Response.Clear();
    await _oex.ExecuteAsync(new ActionContext() { HttpContext = context }, new ObjectResult(new {
    Error = "Exception",
    Exception = e
    }));
    Error = "Exception",
    Exception = e
    }));
    }
    }
    }
  5. @Ciantic Ciantic created this gist Apr 20, 2016.
    43 changes: 43 additions & 0 deletions ExceptionMiddleware.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    using Microsoft.AspNetCore.Http;
    using System.Threading.Tasks;
    using Microsoft.Extensions.Logging;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.AspNetCore.Mvc.Internal;
    using Microsoft.Extensions.Options;

    namespace Example
    {

    public class ApiExceptionMiddleware
    {
    private readonly RequestDelegate _next;
    private readonly ILogger _logger;

    private readonly ObjectResultExecutor _oex;

    public ApiExceptionMiddleware(RequestDelegate next, ILoggerFactory loggerFactory, ObjectResultExecutor oex)
    {
    _next = next;
    _logger = loggerFactory.CreateLogger<ApiExceptionMiddleware>();
    _oex = oex;
    }

    public async Task Invoke(HttpContext context)
    {
    try {
    await _next.Invoke(context);
    } catch (Exception e) {
    if (context.Response.HasStarted) {
    _logger.LogWarning("The response has already started, the api exception middleware will not be executed");
    throw;
    }
    context.Response.StatusCode = 500;
    context.Response.Clear();
    await _oex.ExecuteAsync(new ActionContext() { HttpContext = context }, new ObjectResult(new {
    Error = "Exception",
    Exception = e
    }));
    }
    }
    }
    }