Return JSON-encoded errors

This commit is contained in:
2022-08-28 15:07:46 +02:00
parent 9da60fa4e7
commit 0110d6994b
5 changed files with 52 additions and 7 deletions

32
internal/errorx/http.go Normal file
View 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,
}
}