Lajith
Lajith

Reputation: 1877

Global exception for try catch in asp.net core 2.2 or 3

i need a global exception handler for my all controller method if any error happens( i need to send some error code to the client)..currently am writing try catch block in every controller..Below is my controller method..Is this good approach or please suggest me a solution/approach am using asp.net core 3 preview..

[HttpPost]
        public ActionResult<Caste> InsertCaste(CasteModel caste)
        {
            try
            {

                var result = casteService.InsertCaste(caste);

                return CreatedAtAction(nameof(InsertCaste), new { id = result.Id }, result);
            }
            catch (Exception ex)
            {
                Log.Logger.log().Error(ex.Message);
                return null;
            }
        }

Upvotes: 2

Views: 7394

Answers (1)

Ali Bahrami
Ali Bahrami

Reputation: 6073

TL;DR:

Handle errors in ASP.NET Core

ASP.NET Core Middleware

One of the approaches in ASP.NET Core is to use Middlewares. Inside Startup class and Configure method, add this piece of code:

   app.UseExceptionHandler(errorApp =>
   {
        errorApp.Run(async context =>
        {
                    context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                    context.Response.ContentType = "application/json";
 
                    var contextFeature = context.Features.Get<IExceptionHandlerFeature>();
                    if(contextFeature != null)
                    {  
                        await context.Response.WriteAsync(new ExceptionInfo()
                        {
                            StatusCode = context.Response.StatusCode,
                            Message = "Internal Server Error."
                        }.ToString());
                    }
        });
    });

And ExceptionInfo class:

public class ExceptionInfo
{
    public int StatusCode { get; set; }
    public string Message { get; set; }
 
    public override string ToString()
    {
        return JsonConvert.SerializeObject(this);
    }
}

Update 1:

The order of middlewares is very important, you MUST put it before any other middlewares such as mvc:

    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
        app.UseHsts();
    }
 
    app.UseExceptionHandler(... like code above);
 
    app.UseHttpsRedirection();
    app.UseMvc();

Update 2:

In case of logging your exceptions, you can inject your logger by adding its type to the Configure method in Startup class:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILogger logger)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
        app.UseHsts();
    }
 
    app.UseExceptionHandler(... like code above);
 
    app.UseHttpsRedirection();
    app.UseMvc();
}

Update 3:

Use a custom middleware as a global exception handler:

public class CustomErrorHandlerMiddleware
{
    private readonly RequestDelegate _next;
    private readonly ILoggerManager _logger;
 
    public ExceptionMiddleware(RequestDelegate next, Ilogger logger)
    {
        _logger = logger;
        _next = next;
    }
 
    public async Task InvokeAsync(HttpContext httpContext)
    {
        try
        {
            await _next(httpContext);
        }
        catch (Exception ex)
        {
            _logger.LogError($"Something went wrong: {ex}");
            await HandleExceptionAsync(httpContext, ex);
        }
    }
 
    private static Task HandleExceptionAsync(HttpContext context, Exception exception)
    {
        context.Response.ContentType = "application/json";
        context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
 
        return context.Response.WriteAsync(new ExceptionInfo()
        {
            StatusCode = context.Response.StatusCode,
            Message = "Internal Server Error"
        }.ToString());
    }
}

Then use it in Configure method:

app.UseMiddleware<CustomErrorHandlerMiddleware>();

Upvotes: 6

Related Questions