diff --git a/.gitignore b/.gitignore index 96adc60..3a0ae9a 100644 --- a/.gitignore +++ b/.gitignore @@ -88,4 +88,6 @@ fabric.properties # Dependency directories (remove the comment below to include it) # vendor/ -etc \ No newline at end of file +etc + +tmp/ \ No newline at end of file diff --git a/.idea/gofiber-study.iml b/.idea/gofiber-study.iml index 5e764c4..9058451 100644 --- a/.idea/gofiber-study.iml +++ b/.idea/gofiber-study.iml @@ -2,7 +2,9 @@ - + + + diff --git a/common/errorx/errorx.go b/common/errorx/errorx.go index 9429fd9..fa7309d 100644 --- a/common/errorx/errorx.go +++ b/common/errorx/errorx.go @@ -4,7 +4,6 @@ import "gofiber.study.skcks.cn/common/response" type CodeError struct { *response.Response - Code response.Code `json:"code" example:"500"` } type CodeErrorResponse struct { @@ -14,14 +13,12 @@ type CodeErrorResponse struct { func NewCustomError(code int, data interface{}, msg string) error { return &CodeError{ Response: response.NewCustomResponse(code, data, msg), - Code: code, } } func NewErrorWithCode(code int, msg string) error { return &CodeError{ Response: response.NewCustomResponse(code, nil, msg), - Code: code, } } diff --git a/docs/docs.go b/docs/docs.go index 099b54c..92bd844 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -41,23 +41,151 @@ const docTemplate = `{ "200": { "description": "OK", "schema": { - "type": "string" + "allOf": [ + { + "$ref": "#/definitions/response.Response" + }, + { + "type": "object", + "properties": { + "data": { + "type": "string" + } + } + } + ] } }, - "400": { - "description": "Bad Request", + "default": { + "description": "", "schema": { "$ref": "#/definitions/errorx.CodeErrorResponse" } - }, - "404": { - "description": "Not Found", + } + } + } + }, + "/error": { + "get": { + "description": "错误信息示例", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Error" + ], + "summary": "错误处理", + "responses": { + "default": { + "description": "", "schema": { "$ref": "#/definitions/errorx.CodeErrorResponse" } - }, - "500": { - "description": "Internal Server Error", + } + } + }, + "put": { + "description": "错误信息示例", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Error" + ], + "summary": "错误处理", + "responses": { + "default": { + "description": "", + "schema": { + "$ref": "#/definitions/errorx.CodeErrorResponse" + } + } + } + }, + "post": { + "description": "错误信息示例", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Error" + ], + "summary": "错误处理", + "responses": { + "default": { + "description": "", + "schema": { + "$ref": "#/definitions/errorx.CodeErrorResponse" + } + } + } + }, + "delete": { + "description": "错误信息示例", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Error" + ], + "summary": "错误处理", + "responses": { + "default": { + "description": "", + "schema": { + "$ref": "#/definitions/errorx.CodeErrorResponse" + } + } + } + }, + "head": { + "description": "错误信息示例", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Error" + ], + "summary": "错误处理", + "responses": { + "default": { + "description": "", + "schema": { + "$ref": "#/definitions/errorx.CodeErrorResponse" + } + } + } + }, + "patch": { + "description": "错误信息示例", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Error" + ], + "summary": "错误处理", + "responses": { + "default": { + "description": "", "schema": { "$ref": "#/definitions/errorx.CodeErrorResponse" } @@ -80,6 +208,20 @@ const docTemplate = `{ "example": "OK" } } + }, + "response.Response": { + "type": "object", + "properties": { + "code": { + "type": "integer", + "example": 200 + }, + "data": {}, + "msg": { + "type": "string", + "example": "OK" + } + } } } }` diff --git a/docs/swagger.json b/docs/swagger.json index 64a5a31..1deb024 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -33,23 +33,151 @@ "200": { "description": "OK", "schema": { - "type": "string" + "allOf": [ + { + "$ref": "#/definitions/response.Response" + }, + { + "type": "object", + "properties": { + "data": { + "type": "string" + } + } + } + ] } }, - "400": { - "description": "Bad Request", + "default": { + "description": "", "schema": { "$ref": "#/definitions/errorx.CodeErrorResponse" } - }, - "404": { - "description": "Not Found", + } + } + } + }, + "/error": { + "get": { + "description": "错误信息示例", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Error" + ], + "summary": "错误处理", + "responses": { + "default": { + "description": "", "schema": { "$ref": "#/definitions/errorx.CodeErrorResponse" } - }, - "500": { - "description": "Internal Server Error", + } + } + }, + "put": { + "description": "错误信息示例", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Error" + ], + "summary": "错误处理", + "responses": { + "default": { + "description": "", + "schema": { + "$ref": "#/definitions/errorx.CodeErrorResponse" + } + } + } + }, + "post": { + "description": "错误信息示例", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Error" + ], + "summary": "错误处理", + "responses": { + "default": { + "description": "", + "schema": { + "$ref": "#/definitions/errorx.CodeErrorResponse" + } + } + } + }, + "delete": { + "description": "错误信息示例", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Error" + ], + "summary": "错误处理", + "responses": { + "default": { + "description": "", + "schema": { + "$ref": "#/definitions/errorx.CodeErrorResponse" + } + } + } + }, + "head": { + "description": "错误信息示例", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Error" + ], + "summary": "错误处理", + "responses": { + "default": { + "description": "", + "schema": { + "$ref": "#/definitions/errorx.CodeErrorResponse" + } + } + } + }, + "patch": { + "description": "错误信息示例", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Error" + ], + "summary": "错误处理", + "responses": { + "default": { + "description": "", "schema": { "$ref": "#/definitions/errorx.CodeErrorResponse" } @@ -72,6 +200,20 @@ "example": "OK" } } + }, + "response.Response": { + "type": "object", + "properties": { + "code": { + "type": "integer", + "example": 200 + }, + "data": {}, + "msg": { + "type": "string", + "example": "OK" + } + } } } } \ No newline at end of file diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 914de76..6708dfb 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -10,6 +10,16 @@ definitions: example: OK type: string type: object + response.Response: + properties: + code: + example: 200 + type: integer + data: {} + msg: + example: OK + type: string + type: object info: contact: email: 919411476@qq.com @@ -33,20 +43,102 @@ paths: "200": description: OK schema: - type: string - "400": - description: Bad Request - schema: - $ref: '#/definitions/errorx.CodeErrorResponse' - "404": - description: Not Found - schema: - $ref: '#/definitions/errorx.CodeErrorResponse' - "500": - description: Internal Server Error + allOf: + - $ref: '#/definitions/response.Response' + - properties: + data: + type: string + type: object + default: + description: "" schema: $ref: '#/definitions/errorx.CodeErrorResponse' summary: HelloWorld tags: - HelloWorld + /error: + delete: + consumes: + - application/json + description: 错误信息示例 + produces: + - application/json + responses: + default: + description: "" + schema: + $ref: '#/definitions/errorx.CodeErrorResponse' + summary: 错误处理 + tags: + - Error + get: + consumes: + - application/json + description: 错误信息示例 + produces: + - application/json + responses: + default: + description: "" + schema: + $ref: '#/definitions/errorx.CodeErrorResponse' + summary: 错误处理 + tags: + - Error + head: + consumes: + - application/json + description: 错误信息示例 + produces: + - application/json + responses: + default: + description: "" + schema: + $ref: '#/definitions/errorx.CodeErrorResponse' + summary: 错误处理 + tags: + - Error + patch: + consumes: + - application/json + description: 错误信息示例 + produces: + - application/json + responses: + default: + description: "" + schema: + $ref: '#/definitions/errorx.CodeErrorResponse' + summary: 错误处理 + tags: + - Error + post: + consumes: + - application/json + description: 错误信息示例 + produces: + - application/json + responses: + default: + description: "" + schema: + $ref: '#/definitions/errorx.CodeErrorResponse' + summary: 错误处理 + tags: + - Error + put: + consumes: + - application/json + description: 错误信息示例 + produces: + - application/json + responses: + default: + description: "" + schema: + $ref: '#/definitions/errorx.CodeErrorResponse' + summary: 错误处理 + tags: + - Error swagger: "2.0" diff --git a/main.go b/main.go index 71d659a..21bf9a4 100644 --- a/main.go +++ b/main.go @@ -50,10 +50,11 @@ func main() { app.Get("/swagger/*", swagger.New(swagger.Config{ DeepLinking: false, // Expand ("list") or Collapse ("none") tag groups by default - DocExpansion: "full", + DocExpansion: "list", })) helloWorld(app) + errorHandler(app) app.Get("/routes", func(ctx *fiber.Ctx) error { return ctx.JSON(response.NewResponse(app.Stack())) @@ -78,10 +79,8 @@ func main() { // @Tags HelloWorld // @Accept json // @Produce json -// @Success 200 {string} string -// @Failure 400 {object} errorx.CodeErrorResponse -// @Failure 404 {object} errorx.CodeErrorResponse -// @Failure 500 {object} errorx.CodeErrorResponse +// @Success 200 {object} response.Response{data=string} +// @Failure default {object} errorx.CodeErrorResponse // @Router / [get] func helloWorld(app *fiber.App) { app.Get("/", func(c *fiber.Ctx) error { @@ -89,3 +88,23 @@ func helloWorld(app *fiber.App) { return c.JSON(response.NewResponse("Hello, World 👋!")) }) } + +// errorHandler +// +// @Summary 错误处理 +// @Description 错误信息示例 +// @Tags Error +// @Accept json +// @Produce json +// @Failure default {object} errorx.CodeErrorResponse +// @Router /error [get] +// @Router /error [post] +// @Router /error [put] +// @Router /error [delete] +// @Router /error [patch] +// @Router /error [head] +func errorHandler(app *fiber.App) { + app.All("/error", func(ctx *fiber.Ctx) error { + return ctx.JSON(errorx.NewDefaultError("错误信息示例")) + }) +}