api-base/pkg/errors/errors.go

40 lines
778 B
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 New(status response.RespType, code ...response.MessageCode) *APIError {
e := &APIError{
status: status,
}
if len(code) > 0 {
e.code = &code[0]
}
return e
}