response

package
v0.0.0-...-8e0b234 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 28, 2024 License: OSL-3.0 Imports: 3 Imported by: 0

README

数据响应格式统一

为什么要统一?

body里面的数据

{
    code: 5000
    reason: "token expired"
    message: "过期10分钟"
}
{
    code: 0
    data: []/{}
}

通过外面多包一层code字段来判断 来判断这次API请求是否成功

{
    "code": 0
    "message": "失败原因"
    data: {}/[]
}

怎么设计统一返回

HTTP 协议, HTTP Status Code来判断一个请求是否成功 4xx, 5xx

借住于Http Status Code, 来表达 API请求是否成功

成功返回: 直接返回数据内容, 对象本身

{}/[]

失败返回: 直接业务异常, APIException对象, 如果err 不是APIException, 就需要进行转换

"code":0,
"reason": "expired"
"message":  "过期了10分钟"

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Failed

func Failed(c *gin.Context, err error)

API失败 返回错误, API Excepiton

func Success

func Success(c *gin.Context, data any)

API成功, 返回数据

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL