Skip to content

当请求体中的 json 参数类型错误,异常拦截报错不准确 #184

@colorful3

Description

@colorful3

描述 bug

  • 你是如何操作的?
    后端接口的 DTO 对象中定义一个列表类型参数;前端故意将参数类型传错,把请求 body 里面的列表类型参数改为整型或字符串类型。

  • 发生了什么?

后端返回的报错为:

{
    "code": 10030,
    "message": "请求体不可为空",
    "request": "PUT /cms/admin/user/2"
}

(截图为”管理员更新用户信息”的接口请求)
image

  • 你觉得应该出现什么?
    应该出现如下报错:
{
    "code": 10030,
    "message": "group_ids字段类型错误",
    "request": "PUT /cms/admin/user/2"
}

请求体不可为空的报错信息非常模糊,对前端误导性极强。

你使用哪个版本出现该问题?

如果使用master,请表明是 master 分支,否则给出具体的版本号
master 分支,0.2.0-RC2 版本。

If your bug is deterministic, can you give a minimal reproducing code?
Some bugs are not deterministic. Can you describe with precision in which context it happened?
If this is possible, can you share your code?
修改如下代码:

@ExceptionHandler({HttpMessageNotReadableException.class})
public UnifyResponseVO processException(HttpMessageNotReadableException exception, HttpServletRequest request, HttpServletResponse response) {
log.error("", exception);
UnifyResponseVO result = new UnifyResponseVO();
result.setRequest(getSimpleRequest(request));
String errorMessage = CodeMessageConfiguration.getMessage(10170);
if (StrUtil.isBlank(errorMessage)) {
result.setMessage(exception.getMessage());
} else {
result.setMessage(errorMessage);
}
result.setCode(Code.PARAMETER_ERROR.getCode());
response.setStatus(HttpStatus.BAD_REQUEST.value());
return result;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bug该 Issue 提出了一个 Bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions