Documentation ¶
Overview ¶
Package errcode is used for http and grpc error codes, include system-level error codes and business-level error codes
Index ¶
- Constants
- Variables
- func GetErrorCode(err error) int
- func GetStatusCode(err error) codes.Code
- func HCode(num int) int
- func ListGRPCErrCodes(w http.ResponseWriter, _ *http.Request)
- func RCode(num int) codes.Code
- func ShowConfig(jsonData []byte) func(w http.ResponseWriter, r *http.Request)
- type Detail
- type ErrInfo
- type Error
- func (e *Error) Code() int
- func (e *Error) Details() []string
- func (e *Error) Err(msg ...string) error
- func (e *Error) ErrToHTTP(msg ...string) error
- func (e *Error) Msg() string
- func (e *Error) NeedHTTPCode() bool
- func (e *Error) ToHTTPCode() int
- func (e *Error) WithDetails(details ...string) *Error
- func (e *Error) WithOutMsg(msg string) *Error
- func (e *Error) WithOutMsgI18n(langMsg map[int]map[string]string, lang string) *Error
- type RPCStatus
- type Responser
Constants ¶
const ToHTTPCodeLabel = "[standard http code]"
ToHTTPCodeLabel need to convert to standard http code label
Variables ¶
var ( Success = NewError(0, "ok") InvalidParams = NewError(10001, "Invalid Parameter") InternalServerError = NewError(10003, "Internal Server Error") NotFound = NewError(10004, "Not Found") Timeout = NewError(10006, "Request Timeout") TooManyRequests = NewError(10007, "Too Many Requests") Forbidden = NewError(10008, "Forbidden") LimitExceed = NewError(10009, "Limit Exceed") DeadlineExceeded = NewError(10010, "Deadline Exceeded") AccessDenied = NewError(10011, "Access Denied") MethodNotAllowed = NewError(10012, "Method Not Allowed") Canceled = NewError(10014, "Canceled") Unknown = NewError(10015, "Unknown") PermissionDenied = NewError(10016, "Permission Denied") ResourceExhausted = NewError(10017, "Resource Exhausted") FailedPrecondition = NewError(10018, "Failed Precondition") Aborted = NewError(10019, "Aborted") OutOfRange = NewError(10020, "Out Of Range") Unimplemented = NewError(10021, "Unimplemented") DataLoss = NewError(10022, "Data Loss") StatusBadGateway = NewError(10023, "Bad Gateway") // Deprecated: use Conflict instead AlreadyExists = NewError(10005, "Already Exists") Conflict = NewError(10409, "Conflict") TooEarly = NewError(10425, "Too Early") )
http system level error code, error code range 10000~20000
var ( StatusSuccess = NewRPCStatus(0, "ok") StatusCanceled = NewRPCStatus(30001, "Canceled") StatusUnknown = NewRPCStatus(30002, "Unknown") StatusInvalidParams = NewRPCStatus(30003, "Invalid Parameter") StatusDeadlineExceeded = NewRPCStatus(30004, "Deadline Exceeded") StatusNotFound = NewRPCStatus(30005, "Not Found") StatusAlreadyExists = NewRPCStatus(30006, "Already Exists") StatusPermissionDenied = NewRPCStatus(30007, "Permission Denied") StatusResourceExhausted = NewRPCStatus(30008, "Resource Exhausted") StatusFailedPrecondition = NewRPCStatus(30009, "Failed Precondition") StatusAborted = NewRPCStatus(30010, "Aborted") StatusOutOfRange = NewRPCStatus(30011, "Out Of Range") StatusUnimplemented = NewRPCStatus(30012, "Unimplemented") StatusInternalServerError = NewRPCStatus(30013, "Internal Server Error") StatusDataLoss = NewRPCStatus(30015, "Data Loss") StatusTimeout = NewRPCStatus(30017, "Request Timeout") StatusTooManyRequests = NewRPCStatus(30018, "Too Many Requests") StatusForbidden = NewRPCStatus(30019, "Forbidden") StatusLimitExceed = NewRPCStatus(30020, "Limit Exceed") StatusMethodNotAllowed = NewRPCStatus(30021, "Method Not Allowed") StatusAccessDenied = NewRPCStatus(30022, "Access Denied") StatusConflict = NewRPCStatus(30023, "Conflict") )
rpc system level error code with status prefix, error code range 30000~40000
var SkipResponse = errors.New("skip response") //nolint
SkipResponse skip response
Functions ¶
func GetErrorCode ¶ added in v1.0.5
GetErrorCode get Error code from error returned by http invoke
func GetStatusCode ¶ added in v1.0.5
GetStatusCode get status code from error returned by RPC invoke
func HCode ¶
HCode Generate an error code between 20000 and 30000 according to the number
http service level error code, Err prefix, example.
var ( ErrUserCreate = NewError(HCode(1)+1, "failed to create user") // 20101 ErrUserDelete = NewError(HCode(1)+2, "failed to delete user") // 20102 ErrUserUpdate = NewError(HCode(1)+3, "failed to update user") // 20103 ErrUserGet = NewError(HCode(1)+4, "failed to get user details") // 20104 )
func ListGRPCErrCodes ¶
func ListGRPCErrCodes(w http.ResponseWriter, _ *http.Request)
ListGRPCErrCodes list grpc error codes, http handle func
func RCode ¶
RCode Generate an error code between 40000 and 50000 according to the number
rpc service level error code, status prefix, example.
var ( StatusUserCreate = NewRPCStatus(RCode(1)+1, "failed to create user") // 40101 StatusUserDelete = NewRPCStatus(RCode(1)+2, "failed to delete user") // 40102 StatusUserUpdate = NewRPCStatus(RCode(1)+3, "failed to update user") // 40103 StatusUserGet = NewRPCStatus(RCode(1)+4, "failed to get user details") // 40104 )
func ShowConfig ¶
func ShowConfig(jsonData []byte) func(w http.ResponseWriter, r *http.Request)
ShowConfig show config info @Summary show config info @Description show config info @Tags system @Accept json @Produce json @Router /config [get]
Types ¶
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
Error error
func ParseError ¶
ParseError parsing out error codes from error messages
func (*Error) Err ¶
Err convert to standard error, if there is a parameter 'msg', it will replace the original message.
func (*Error) ErrToHTTP ¶
ErrToHTTP convert to standard error add ToHTTPCodeLabel to error message, use it if you need to convert to standard HTTP status code, if there is a parameter 'msg', it will replace the original message. Tips: you can call the GetErrorCode function to get the standard HTTP status code.
func (*Error) NeedHTTPCode ¶
NeedHTTPCode need to convert to standard http code
func (*Error) WithDetails ¶
WithDetails add error details
func (*Error) WithOutMsg ¶
WithOutMsg out error message Deprecated: in Err or ErrToHTTP parameter msg can be used to replace the original message.
func (*Error) WithOutMsgI18n ¶
WithOutMsgI18n out error message i18n langMsg example:
map[int]map[string]string{ 20010: { "en-US": "login failed", "zh-CN": "登录失败", }, }
lang BCP 47 code https://learn.microsoft.com/en-us/openspecs/office_standards/ms-oe376/6c085406-a698-4e12-9d4d-c3b0ee3dbc4a
type RPCStatus ¶
type RPCStatus struct {
// contains filtered or unexported fields
}
RPCStatus rpc status
func NewRPCStatus ¶
NewRPCStatus create a new rpc status
func (*RPCStatus) Err ¶
Err return error if there is a parameter 'desc', it will replace the original message
func (*RPCStatus) ErrToHTTP ¶
ErrToHTTP convert to standard error add ToHTTPCodeLabel to error message, usually used when HTTP calls the GRPC API, if there is a parameter 'desc', it will replace the original message.