GeezerVison is showing github.com — refactor: unify http error status code · gitLibs/server@2752303
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: internal/web/handler/common/validation.go +2-3Lines changed: 2 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -26,15 +26,14 @@ import ( |
26 | 26 | "github.com/go-playground/validator/v10" |
27 | 27 | zhTranslations "github.com/go-playground/validator/v10/translations/zh" |
28 | 28 | "github.com/gofiber/fiber/v2" |
29 | | - "github.com/gofiber/fiber/v2/utils" |
30 | 29 |
|
31 | 30 | "github.com/bangumi/server/internal/pkg/errgo" |
32 | 31 | "github.com/bangumi/server/internal/web/res" |
33 | 32 | ) |
34 | 33 |
|
35 | 34 | func (h Common) ValidationError(c *fiber.Ctx, err error) error { |
36 | | - return res.JSON(c.Status(http.StatusUnprocessableEntity), res.Error{ |
37 | | - Title: utils.StatusMessage(http.StatusUnprocessableEntity), |
| 35 | + return res.JSON(c.Status(http.StatusBadRequest), res.Error{ |
| 36 | + Title: "Bad Request", |
38 | 37 | Description: "can't validate request body", |
39 | 38 | Details: h.translationValidationError(err), |
40 | 39 | }) |
|
Collapse file
Copy file name to clipboardExpand all lines: internal/web/res/error.go +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -57,7 +57,7 @@ func FromError(c *fiber.Ctx, err error, code int, message string) error { |
57 | 57 | } |
58 | 58 |
|
59 | 59 | func JSONError(c *fiber.Ctx, err error) error { |
60 | | - c.Status(http.StatusUnprocessableEntity) |
| 60 | + c.Status(http.StatusBadRequest) |
61 | 61 |
|
62 | 62 | return JSON(c, Error{ |
63 | 63 | Title: "JSON Syntax Error", |
|
Collapse file
Copy file name to clipboardExpand all lines: openapi/v0.yaml -16Lines changed: 0 additions & 16 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2,22 +2,6 @@ openapi: 3.0.2 |
2 | 2 | info: |
3 | 3 | title: "" |
4 | 4 | version: "" |
5 | | - description: | |
6 | | - ## 错误处理 |
7 | | -
|
8 | | - ### 不带有 Body 的请求: |
9 | | -
|
10 | | - `400`: 请求 path 或者 query 中的数据错误,如 `?subject_id=-1`,或者对应的信息缺失。 |
11 | | -
|
12 | | - ### 带有 Body 的请求: |
13 | | -
|
14 | | - `415`: 请求没有设置 `content-type` header,应该统一设置为 `application/json`。 |
15 | | -
|
16 | | - `422`: 无法以 JSON 格式解析请求(跟 WebDAV 的 RFC 定义的不同) |
17 | | -
|
18 | | - `400`: 请求数据错误,比如请求体或者 path, query 中的数据错误。(不代表请求体一定能够正确以 JSON 格式解析) |
19 | | -
|
20 | | - **未用到的多余的字段会被直接忽略,不会报错。** |
21 | 5 |
|
22 | 6 | tags: |
23 | 7 | - name: "条目" |
|