improve package gerror/ghttp for error code handling

This commit is contained in:
John Guo
2021-08-07 10:44:57 +08:00
parent 0f6820df9e
commit cd3593182a
10 changed files with 51 additions and 28 deletions

View File

@ -165,10 +165,14 @@ func NewCodef(code int, format string, args ...interface{}) error {
// NewCodeSkip creates and returns an error which has error code and is formatted from given text.
// The parameter <skip> specifies the stack callers skipped amount.
func NewCodeSkip(code, skip int, text string) error {
func NewCodeSkip(code, skip int, text ...string) error {
errText := ""
if len(text) > 0 {
errText = text[0]
}
return &Error{
stack: callers(skip),
text: text,
text: errText,
code: code,
}
}
@ -185,14 +189,18 @@ func NewCodeSkipf(code, skip int, format string, args ...interface{}) error {
// WrapCode wraps error with code and text.
// It returns nil if given err is nil.
func WrapCode(code int, err error, text string) error {
func WrapCode(code int, err error, text ...string) error {
if err == nil {
return nil
}
errText := ""
if len(text) > 0 {
errText = text[0]
}
return &Error{
error: err,
stack: callers(),
text: text,
text: errText,
code: code,
}
}
@ -214,14 +222,18 @@ func WrapCodef(code int, err error, format string, args ...interface{}) error {
// WrapCodeSkip wraps error with code and text.
// It returns nil if given err is nil.
// The parameter <skip> specifies the stack callers skipped amount.
func WrapCodeSkip(code, skip int, err error, text string) error {
func WrapCodeSkip(code, skip int, err error, text ...string) error {
if err == nil {
return nil
}
errText := ""
if len(text) > 0 {
errText = text[0]
}
return &Error{
error: err,
stack: callers(skip),
text: text,
text: errText,
code: code,
}
}

View File

@ -27,6 +27,7 @@ const (
CodeServerBusy = 63 // Server is busy, please try again later.
CodeUnknown = 64 // Unknown error.
CodeResourceNotExist = 65 // Resource does not exist.
CodeInvalidRequest = 66 // Invalid request.
CodeBusinessValidationFailed = 300 // Business validation failed.
)
@ -51,6 +52,7 @@ var (
CodeServerBusy: "Server Is Busy",
CodeUnknown: "Unknown Error",
CodeResourceNotExist: "Resource Not Exist",
CodeInvalidRequest: "Invalid Request",
CodeBusinessValidationFailed: "Business Validation Failed",
}
)

View File

@ -51,7 +51,7 @@ func (err *Error) Error() string {
errStr = Message(err.code)
}
if err.error != nil {
if err.text != "" {
if errStr != "" {
errStr += ": "
}
errStr += err.error.Error()