Fork me on GitHub

ASP.NET Core 统一异常处理和返回

业务场景:

业务需求要求,需要对 ASP.NET Core 异常进行统一处理和返回,比如出现 500 错误和业务服务错误进行不同的处理和返回。

具体实现:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using System;
using System.Net;
using System.Threading.Tasks;

public class ErrorHandlingMiddleware
{
    private readonly RequestDelegate next;

    public ErrorHandlingMiddleware(RequestDelegate next)
    {
        this.next = next;
    }

    public async Task Invoke(HttpContext context)
    {
        try
        {
            await next(context);
        }
        catch (Exception ex)
        {
            var statusCode = context.Response.StatusCode;
            if (ex is ArgumentException)
            {
                statusCode = 200;
            }
            await HandleExceptionAsync(context, statusCode, ex.Message);
        }
        finally
        {
            var statusCode = context.Response.StatusCode;
            var msg = "";
            if (statusCode == 401)
            {
                msg = "未授权";
            }
            else if (statusCode == 404)
            {
                msg = "未找到服务";
            }
            else if (statusCode == 502)
            {
                msg = "请求错误";
            }
            else if (statusCode != 200)
            {
                msg = "未知错误";
            }
            if (!string.IsNullOrWhiteSpace(msg))
            {
                await HandleExceptionAsync(context, statusCode, msg);
            }
        }
    }

    private static Task HandleExceptionAsync(HttpContext context, int statusCode, string msg)
    {
        var data = new { code = statusCode.ToString(), is_success = false, msg = msg };
        var result = JsonConvert.SerializeObject(new { data = data });
        context.Response.ContentType = "application/json;charset=utf-8";
        return context.Response.WriteAsync(result);
    }
}

public static class ErrorHandlingExtensions
{
    public static IApplicationBuilder UseErrorHandling(this IApplicationBuilder builder)
    {
        return builder.UseMiddleware<ErrorHandlingMiddleware>();
    }
}

Startup添加对应配置:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    app.UseErrorHandling();
}
posted @ 2017-04-04 22:36  田园里的蟋蟀  阅读(11517)  评论(7编辑  收藏  举报