feat: 内建 业务错误状态码+消息 缓存。

main v1.2.4
NoahLan 1 year ago
parent 29c064f702
commit eb07dba9bd

@ -1,6 +1,12 @@
package nstatus package nstatus
import "git.noahlan.cn/noahlan/ntool-biz/core/nstatus/code" import (
"git.noahlan.cn/noahlan/ntool-biz/core/nstatus/code"
)
/**
APIHTTP
*/
// NewApiErr 构建Api错误code为http状态码 // NewApiErr 构建Api错误code为http状态码
func NewApiErr(code int, message string) error { func NewApiErr(code int, message string) error {

@ -1,10 +1,24 @@
package nstatus package nstatus
import (
"git.noahlan.cn/noahlan/ntool-biz/core/nstatus/code"
"git.noahlan.cn/noahlan/ntool-biz/core/nstatus/msg"
)
/**
500
*/
// NewBizErr 构建业务错误 // NewBizErr 构建业务错误
func NewBizErr(code int, message string) error { func NewBizErr(code int, message string) error {
return NewResult(code, message, BizErr) return NewResult(code, message, BizErr)
} }
// NewBizErrWithCode 通过Code构建业务错误
func NewBizErrWithCode(c code.Code) error {
return NewBizErr(int(c), msg.Msg(c))
}
// NewBizErrNoMsg 构建业务错误,空错误消息 // NewBizErrNoMsg 构建业务错误,空错误消息
func NewBizErrNoMsg(code int) error { func NewBizErrNoMsg(code int) error {
return NewBizErr(code, "") return NewBizErr(code, "")

@ -2,22 +2,24 @@ package code
import "net/http" import "net/http"
type Code int
const ( const (
StatusUnknown int = -1 StatusUnknown = -1
StatusOK = http.StatusOK StatusOK = http.StatusOK
StatusFailed = 0 StatusFailed = 0
StatusBadRequest = http.StatusBadRequest StatusBadRequest = http.StatusBadRequest
StatusUnauthorized = http.StatusUnauthorized StatusUnauthorized = http.StatusUnauthorized
StatusForbidden = http.StatusForbidden StatusForbidden = http.StatusForbidden
StatusNotFound = http.StatusNotFound StatusNotFound = http.StatusNotFound
StatusRequestTimeout = http.StatusRequestTimeout StatusRequestTimeoutCode = http.StatusRequestTimeout
StatusConflict = http.StatusConflict StatusConflict = http.StatusConflict
StatusTooManyRequests = http.StatusTooManyRequests StatusTooManyRequests = http.StatusTooManyRequests
StatusInternalServerError = http.StatusInternalServerError StatusInternalServerError = http.StatusInternalServerError
StatusNotImplemented = http.StatusNotImplemented StatusNotImplemented = http.StatusNotImplemented
StatusBadGateway = http.StatusBadGateway StatusBadGateway = http.StatusBadGateway
StatusServiceUnavailable = http.StatusServiceUnavailable StatusServiceUnavailable = http.StatusServiceUnavailable
StatusGatewayTimeout = http.StatusGatewayTimeout StatusGatewayTimeout = http.StatusGatewayTimeout
) )

@ -52,6 +52,10 @@ func IsGrpcErr(err error) bool {
return ok return ok
} }
/**
RPC
*/
func NewGrpcErr(code codes.Code, msg string) error { func NewGrpcErr(code codes.Code, msg string) error {
return NewResult(int(code), msg, RpcErr) return NewResult(int(code), msg, RpcErr)
} }

@ -1,23 +1,44 @@
package msg package msg
import (
"git.noahlan.cn/noahlan/ntool-biz/core/nstatus/code"
"sync"
)
var codeMsgMap sync.Map
// message keys // message keys
const ( const (
Success = "common.success" Success = "common.success"
Failed = "common.failed" Failed = "common.failed"
UpdateSuccess = "common.updateSuccess" UpdateSuccess = "common.updateSuccess"
UpdateFailed = "common.updateFailed" UpdateFailed = "common.updateFailed"
CreateSuccess = "common.createSuccess" CreateSuccess = "common.createSuccess"
CreateFailed = "common.createFailed" CreateFailed = "common.createFailed"
DeleteSuccess = "common.deleteSuccess" DeleteSuccess = "common.deleteSuccess"
DeleteFailed = "common.deleteFailed" DeleteFailed = "common.deleteFailed"
ObjectNotFound = "common.objectNotFound" ObjectNotFound = "common.objectNotFound"
DatabaseError = "common.databaseError" DatabaseError = "common.databaseError"
CacheError = "common.cacheError" CacheError = "common.cacheError"
ConstraintError = "common.constraintError" ConstraintError = "common.constraintError"
ValidationError = "common.validationError" ValidationError = "common.validationError"
NotSingularError = "common.notSingularError" NotSingularError = "common.notSingularError"
PermissionDeny = "common.permissionDeny" PermissionDeny = "common.permissionDeny"
AlreadyInit = "init.alreadyInit" AlreadyInit = "init.alreadyInit"
InitRunning = "init.initializeIsRunning" InitRunning = "init.initializeIsRunning"
) )
// Add 添加消息
func Add(c code.Code, msg string) {
codeMsgMap.Store(c, msg)
}
// Msg 获取string消息
func Msg(c code.Code) string {
msg, ok := codeMsgMap.Load(c)
if ok {
return msg.(string)
}
return Failed
}

@ -28,13 +28,23 @@ func ResponseHandler(req *http.Request, w http.ResponseWriter, trans bool, resp
if trans { if trans {
result.Msg = i18n.Trans(req.Context(), result.Msg) result.Msg = i18n.Trans(req.Context(), result.Msg)
} }
c := http.StatusInternalServerError
if result.Type == nstatus.ApiErr { var c int
switch result.Type {
case nstatus.ApiErr:
// API错误按照Result内的code返回若code不是标准http错误则按照500返回 // API错误按照Result内的code返回若code不是标准http错误则按照500返回
c = result.Code c = result.Code
if c > http.StatusNetworkAuthenticationRequired { if c > http.StatusNetworkAuthenticationRequired || c < http.StatusContinue {
c = http.StatusInternalServerError c = http.StatusInternalServerError
} }
case nstatus.BizErr:
// 业务代码类型错误统一状态码为500
c = http.StatusInternalServerError
case nstatus.RpcErr:
// RPC错误统一状态码为500
c = http.StatusInternalServerError
default:
c = http.StatusInternalServerError
} }
// 其它错误统一按照500返回 // 其它错误统一按照500返回
httpx.WriteJson(w, c, result) httpx.WriteJson(w, c, result)

Loading…
Cancel
Save