62 lines
1.2 KiB
Go
62 lines
1.2 KiB
Go
package errors
|
|
|
|
import (
|
|
"fmt"
|
|
"go-api/pkg/response"
|
|
)
|
|
|
|
var (
|
|
ErrDataFormat = New(response.RespDataFormat)
|
|
ErrUnauthorized = New(response.RespUnauthorized)
|
|
ErrNotFound = New(response.RespNotFound)
|
|
ErrInternalError = New(response.RespInternalError)
|
|
)
|
|
|
|
type APIError struct {
|
|
code *response.MessageCode
|
|
status response.RespType
|
|
message string
|
|
}
|
|
|
|
func (e *APIError) Error() string {
|
|
s := fmt.Sprintf("Status: %s, Message: %s", e.status, e.message)
|
|
if e.code != nil {
|
|
c, m := response.GetCodeMessage(*e.code)
|
|
s = fmt.Sprintf("%s, Code: %d, CodeMessage: %s", s, c, m)
|
|
}
|
|
return s
|
|
}
|
|
|
|
func (e *APIError) SetCode(code response.MessageCode) *APIError {
|
|
e = &APIError{
|
|
status: e.status,
|
|
code: &code,
|
|
message: e.message,
|
|
}
|
|
return e
|
|
}
|
|
|
|
func (e *APIError) SetMessage(s string) *APIError {
|
|
e = &APIError{
|
|
status: e.status,
|
|
code: e.code,
|
|
message: s,
|
|
}
|
|
|
|
return e
|
|
}
|
|
|
|
func (e *APIError) Code() *response.MessageCode { return e.code }
|
|
func (e *APIError) Status() response.RespType { return e.status }
|
|
|
|
func New(status response.RespType, code ...response.MessageCode) *APIError {
|
|
e := &APIError{
|
|
status: status,
|
|
}
|
|
|
|
if len(code) > 0 {
|
|
e.code = &code[0]
|
|
}
|
|
return e
|
|
}
|