diff --git a/core/nstatus/api_err.go b/core/nstatus/api_err.go index 1a7e661..1a9bf29 100644 --- a/core/nstatus/api_err.go +++ b/core/nstatus/api_err.go @@ -1,6 +1,12 @@ package nstatus -import "git.noahlan.cn/noahlan/ntool-biz/core/nstatus/code" +import ( + "git.noahlan.cn/noahlan/ntool-biz/core/nstatus/code" +) + +/** +API错误,用于返回带HTTP状态码的错误返回信息 +*/ // NewApiErr 构建Api错误,code为http状态码 func NewApiErr(code int, message string) error { diff --git a/core/nstatus/biz_err.go b/core/nstatus/biz_err.go index 3f11d96..a128a03 100644 --- a/core/nstatus/biz_err.go +++ b/core/nstatus/biz_err.go @@ -1,10 +1,24 @@ package nstatus +import ( + "git.noahlan.cn/noahlan/ntool-biz/core/nstatus/code" + "git.noahlan.cn/noahlan/ntool-biz/core/nstatus/msg" +) + +/** +业务代码类型错误,错误状态码统一为500,详细状态码在返回体中 +*/ + // NewBizErr 构建业务错误 func NewBizErr(code int, message string) error { return NewResult(code, message, BizErr) } +// NewBizErrWithCode 通过Code构建业务错误 +func NewBizErrWithCode(c code.Code) error { + return NewBizErr(int(c), msg.Msg(c)) +} + // NewBizErrNoMsg 构建业务错误,空错误消息 func NewBizErrNoMsg(code int) error { return NewBizErr(code, "") diff --git a/core/nstatus/code/code.go b/core/nstatus/code/code.go index 8cd3197..3273ecf 100644 --- a/core/nstatus/code/code.go +++ b/core/nstatus/code/code.go @@ -2,22 +2,24 @@ package code import "net/http" +type Code int + const ( - StatusUnknown int = -1 - StatusOK = http.StatusOK - StatusFailed = 0 + StatusUnknown = -1 + StatusOK = http.StatusOK + StatusFailed = 0 - StatusBadRequest = http.StatusBadRequest - StatusUnauthorized = http.StatusUnauthorized - StatusForbidden = http.StatusForbidden - StatusNotFound = http.StatusNotFound - StatusRequestTimeout = http.StatusRequestTimeout - StatusConflict = http.StatusConflict - StatusTooManyRequests = http.StatusTooManyRequests + StatusBadRequest = http.StatusBadRequest + StatusUnauthorized = http.StatusUnauthorized + StatusForbidden = http.StatusForbidden + StatusNotFound = http.StatusNotFound + StatusRequestTimeoutCode = http.StatusRequestTimeout + StatusConflict = http.StatusConflict + StatusTooManyRequests = http.StatusTooManyRequests - StatusInternalServerError = http.StatusInternalServerError - StatusNotImplemented = http.StatusNotImplemented - StatusBadGateway = http.StatusBadGateway - StatusServiceUnavailable = http.StatusServiceUnavailable - StatusGatewayTimeout = http.StatusGatewayTimeout + StatusInternalServerError = http.StatusInternalServerError + StatusNotImplemented = http.StatusNotImplemented + StatusBadGateway = http.StatusBadGateway + StatusServiceUnavailable = http.StatusServiceUnavailable + StatusGatewayTimeout = http.StatusGatewayTimeout ) diff --git a/core/nstatus/grpc_err.go b/core/nstatus/grpc_err.go index 69a25f2..46168a0 100644 --- a/core/nstatus/grpc_err.go +++ b/core/nstatus/grpc_err.go @@ -52,6 +52,10 @@ func IsGrpcErr(err error) bool { return ok } +/** +RPC错误 +*/ + func NewGrpcErr(code codes.Code, msg string) error { return NewResult(int(code), msg, RpcErr) } diff --git a/core/nstatus/msg/msg.go b/core/nstatus/msg/msg.go index 6032ae8..9c19d4b 100644 --- a/core/nstatus/msg/msg.go +++ b/core/nstatus/msg/msg.go @@ -1,23 +1,44 @@ package msg +import ( + "git.noahlan.cn/noahlan/ntool-biz/core/nstatus/code" + "sync" +) + +var codeMsgMap sync.Map + // message keys const ( - Success = "common.success" - Failed = "common.failed" - UpdateSuccess = "common.updateSuccess" - UpdateFailed = "common.updateFailed" - CreateSuccess = "common.createSuccess" - CreateFailed = "common.createFailed" - DeleteSuccess = "common.deleteSuccess" - DeleteFailed = "common.deleteFailed" - ObjectNotFound = "common.objectNotFound" - DatabaseError = "common.databaseError" - CacheError = "common.cacheError" - ConstraintError = "common.constraintError" - ValidationError = "common.validationError" - NotSingularError = "common.notSingularError" - PermissionDeny = "common.permissionDeny" + Success = "common.success" + Failed = "common.failed" + UpdateSuccess = "common.updateSuccess" + UpdateFailed = "common.updateFailed" + CreateSuccess = "common.createSuccess" + CreateFailed = "common.createFailed" + DeleteSuccess = "common.deleteSuccess" + DeleteFailed = "common.deleteFailed" + ObjectNotFound = "common.objectNotFound" + DatabaseError = "common.databaseError" + CacheError = "common.cacheError" + ConstraintError = "common.constraintError" + ValidationError = "common.validationError" + NotSingularError = "common.notSingularError" + PermissionDeny = "common.permissionDeny" - AlreadyInit = "init.alreadyInit" - InitRunning = "init.initializeIsRunning" + AlreadyInit = "init.alreadyInit" + 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 +} diff --git a/zero/statusz/handler.go b/zero/statusz/handler.go index eab21b8..600808c 100644 --- a/zero/statusz/handler.go +++ b/zero/statusz/handler.go @@ -28,13 +28,23 @@ func ResponseHandler(req *http.Request, w http.ResponseWriter, trans bool, resp if trans { 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返回 c = result.Code - if c > http.StatusNetworkAuthenticationRequired { + if c > http.StatusNetworkAuthenticationRequired || c < http.StatusContinue { c = http.StatusInternalServerError } + case nstatus.BizErr: + // 业务代码类型错误,统一状态码为500 + c = http.StatusInternalServerError + case nstatus.RpcErr: + // RPC错误,统一状态码为500 + c = http.StatusInternalServerError + default: + c = http.StatusInternalServerError } // 其它错误统一按照500返回 httpx.WriteJson(w, c, result)