Return JSON-encoded errors
This commit is contained in:
32
internal/errorx/http.go
Normal file
32
internal/errorx/http.go
Normal file
@ -0,0 +1,32 @@
|
||||
package errorx
|
||||
|
||||
const defaultCode = 1001
|
||||
|
||||
type CodeError struct {
|
||||
Code int `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
}
|
||||
|
||||
type CodeErrorResponse struct {
|
||||
Code int `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
}
|
||||
|
||||
func NewCodeError(code int, msg string) error {
|
||||
return &CodeError{Code: code, Msg: msg}
|
||||
}
|
||||
|
||||
func NewDefaultError(msg string) error {
|
||||
return NewCodeError(defaultCode, msg)
|
||||
}
|
||||
|
||||
func (e *CodeError) Error() string {
|
||||
return e.Msg
|
||||
}
|
||||
|
||||
func (e *CodeError) Data() *CodeErrorResponse {
|
||||
return &CodeErrorResponse{
|
||||
Code: e.Code,
|
||||
Msg: e.Msg,
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user