22 lines
608 B
Go
22 lines
608 B
Go
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},
|
|
})
|
|
}
|