【建议】异常类的状态码能自定义,不同插件返回的数据结构统一标准。

zz7628

异常类规范统一的建议

1、异常类的HTTP状态码能自定义,目前基本是写死的200,有部分异支持(但也只支持HTML分支),会导致前端无法通过状态码拦截(多一层判断或自定义异常类),全局审计日志也是多一层判断
2、msg 统一成 message,因为validation 是 message,为了统一结构不得不重写异常类,官方应该要统一规范一下,不然要写自定义异常类
3、这个建议主要是减少写自定义异常类,减少工作量,虽然是个很小的建议,希望webman能更完美

Webman 官方异常类与返回结构

范围:workerman/webman-framework 框架核心 + 官方 webman/validationwebman/limiter 插件。

核心规律:JSON 响应一律返回 HTTP 200,真实状态码放在 body 的 code 字段;只有 HTML 响应才使用真实 HTTP 状态码。

异常类与返回结构

异常类(所属包) 继承自 默认 code JSON 响应(HTTP 状态 / body) HTML 响应(HTTP 状态 / body)
Webman\Exception\ExceptionHandler(webman-framework,默认兜底处理器,非异常类) 500 HTTP 200,{"code": <getCode()?:500>, "msg": <debug 时 getMessage(),否则 'Server internal error'>}(debug 时追加 "traces" HTTP 500,Server internal error(debug 时 nl2br(异常堆栈)
Webman\Exception\BusinessException(webman-framework) RuntimeException 0(?:500 HTTP 200,{"code": <getCode()?:500>, "msg": <getMessage()>, "data": <$this->data>} HTTP 200,<getMessage()>
support\exception\BusinessException(webman-framework) Webman\Exception\BusinessException(空壳) 0(?:500 HTTP 200,{"code": <getCode()?:500>, "msg": <getMessage()>, "data": <$this->data>} HTTP 200,<getMessage()>
support\exception\NotFoundException(webman-framework) support\exception\BusinessException(空壳) 0(?:500 HTTP 200,{"code": <getCode()?:500>, "msg": <getMessage()>, "data": <$this->data>} HTTP 200,<getMessage()>
support\exception\PageNotFoundException(webman-framework) support\exception\NotFoundException 404 HTTP 200,{"code": 404, "msg": <trans(getMessage(), data)>, "data": <$this->data>} HTTP 404,/app/view/404 模板页
support\exception\InputTypeException(webman-framework) support\exception\PageNotFoundException 400 HTTP 200,{"code": 400, "msg": <trans(getMessage(), data)>, "data": <$this->data>} HTTP 400,/app/view/400 模板页
support\exception\InputValueException(webman-framework) support\exception\PageNotFoundException 400 HTTP 200,{"code": 400, "msg": <trans(getMessage(), data)>, "data": <$this->data>} HTTP 400,/app/view/400 模板页
support\exception\MissingInputException(webman-framework) support\exception\PageNotFoundException 400 HTTP 200,{"code": 400, "msg": <trans(getMessage(), data)>, "data": <debug 时 $this->data,否则 {"parameter":""}>} HTTP 400,/app/view/400 模板页
Webman\Exception\FileException(webman-framework) RuntimeException(无自定义 render) HTTP 200,走默认处理器,{"code": <getCode()?:500>, "msg": <debug 时 getMessage(),否则 'Server internal error'>} HTTP 500,Server internal error(debug 时堆栈)
Webman\Exception\NotFoundException(webman-framework) \Exception(实现 NotFoundExceptionInterface,无自定义 render) HTTP 200,走默认处理器,{"code": <getCode()?:500>, "msg": <debug 时 getMessage(),否则 'Server internal error'>} HTTP 500,Server internal error(debug 时堆栈)
support\validation\ValidationException(webman/validation) Webman\Validation\Exception\ValidationExceptionWebman\Exception\BusinessException 400(Validator 抛出时传 400,但 render 忽略) HTTP 200,{"code": 422(硬编码), "message": <getMessage()>, "data": <getData()>} HTTP 200,<getMessage()>
support\limiter\RateLimitException(webman/limiter) Webman\Limiter\RateLimitExceptionsupport\exception\BusinessException 429 HTTP 200,{"code": <getCode()?:429>, "msg": <trans(getMessage(), data)>, "data": <$this->data ?? []>} HTTP 429,<getMessage()>

结论

  • 官方所有异常类 JSON 一律返回 HTTP 200,真实状态码放在 body 的 code 字段。
  • HTML 才用真实 HTTP 状态码(404 / 400 / 429 / 500)。
  • 字段名不统一:官方框架、RateLimitException、默认处理器用 msg;只有 ValidationExceptionmessage
  • 只有 ValidationException 的 body code硬编码 422,其余都取 getCode()
15 0 0
0个回答

🔝