首页 新闻 会员 周边

core3.1使用异常中间件捕获400等错误,显示多个异常提示,怎样只显示自己定义的

0
悬赏园豆:10 [待解决问题]

postman 提示界面如图:上面是自带提示,下面是我自定义。

 {
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "|e83645f6-45a222f9ff0ab1de.",
"errors": {
    "$.UserDesc": [
        "Expected depth to be zero at the end of the JSON payload. There is an open JSON object or array that should be closed. Path: $.UserDesc | LineNumber: 6 | BytePositionInLine: 0."
    ]
}

}

{
"code": 400,
"msg": "执行方法中抛出异常错误",
"data": "url:/AteUser"
}

程序输出方式:await context.Response.WriteAsync(json);

问题补充:

执行方式:

  public async Task Invoke(HttpContext context)
       {
        try
        {
            await _next(context);
        }
        catch (Exception ex)
        {
            if (ex is ServiceException serviceException)
            {
                await HandleExceptionAsync(context, serviceException.LogSn, ex.Message);
            }
        }
        finally
        {
            if (context.Response.StatusCode >= 400&& context.Response.StatusCode < 599)
            {
                await HandleExceptionAsync(context, context.Response.StatusCode, "");
            }

        }
    }

catch可正常抛出异常,finaly则会抛出两个对象异常

输出方式:

     try
        {
            context.Response.Clear();
        }
        catch (Exception ex)
        {
            //throw ex;
        }
        return context.Response.WriteAsync(result);
suqq小白的主页 suqq小白 | 初学一级 | 园豆:4
提问于:2020-12-02 17:42
< >
分享
所有回答(2)
0

context.Response.Clear() 之后 在write

czd890 | 园豆:14412 (专家六级) | 2020-12-02 19:01

是在clear之后进行写入的 问题补充那截图了

支持(0) 反对(0) suqq小白 | 园豆:4 (初学一级) | 2020-12-02 19:22

@suqq小白:

完整的代码发出来...

支持(0) 反对(0) czd890 | 园豆:14412 (专家六级) | 2020-12-02 19:37

@czd890: 已发出来了

支持(0) 反对(0) suqq小白 | 园豆:4 (初学一级) | 2020-12-02 20:00
0
智客工坊 | 园豆:1855 (小虾三级) | 2020-12-02 21:47
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册