40 lines
897 B
Go
40 lines
897 B
Go
package catalog
|
|||
|
|
|
||
|
|
import "errors"
|
||
|
|
|
||
|
|
var (
|
||
|
|
ErrSystemImmutable = errors.New("system records cannot be modified or deleted")
|
||
|
|
ErrNotFound = errors.New("not found")
|
||
|
|
)
|
||
|
|
|
||
|
|
// clientError is a validation/business message safe to return to API clients.
|
||
|
|
type clientError struct {
|
||
|
|
msg string
|
||
|
|
}
|
||
|
|
|
||
|
|
func (e *clientError) Error() string { return e.msg }
|
||
|
|
|
||
|
|
// ClientMsg marks a message as safe to expose in HTTP 4xx responses.
|
||
|
|
func ClientMsg(msg string) error {
|
||
|
|
return &clientError{msg: msg}
|
||
|
|
}
|
||
|
|
|
||
|
|
// ClientError reports whether err is a known client-facing catalog error.
|
||
|
|
func ClientError(err error) (msg string, ok bool) {
|
||
|
|
if err == nil {
|
||
|
|
return "", false
|
||
|
|
}
|
||
|
|
var ce *clientError
|
||
|
|
if errors.As(err, &ce) {
|
||
|
|
return ce.msg, true
|
||
|
|
}
|
||
|
|
switch {
|
||
|
|
case errors.Is(err, ErrNotFound):
|
||
|
|
return "not found", true
|
||
|
|
case errors.Is(err, ErrSystemImmutable):
|
||
|
|
return err.Error(), true
|
||
|
|
default:
|
||
|
|
return "", false
|
||
|
|
}
|
||
|
|
}
|