refactor(api): introduce v1 response contract
This commit is contained in:
21
backend/internal/httpx/response.go
Normal file
21
backend/internal/httpx/response.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package httpx
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
// ErrorBody 描述稳定错误契约中的机器码和可执行中文信息。
|
||||
type ErrorBody struct {
|
||||
Code string `json:"code"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
// ErrorEnvelope 统一包装所有对外 HTTP 错误。
|
||||
type ErrorEnvelope struct {
|
||||
Error ErrorBody `json:"error"`
|
||||
}
|
||||
|
||||
// Error 中止当前请求并返回标准错误结构,避免向客户端泄露内部错误细节。
|
||||
func Error(c *gin.Context, status int, code, message string) {
|
||||
c.AbortWithStatusJSON(status, ErrorEnvelope{
|
||||
Error: ErrorBody{Code: code, Message: message},
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user