util/errors: add ErrForbidden

This commit is contained in:
Simone Gotti 2019-05-03 23:18:51 +02:00
parent af67198dec
commit 5a50a2681d
2 changed files with 24 additions and 0 deletions

View File

@ -34,6 +34,8 @@ func ErrorResponseFromError(err error) *ErrorResponse {
case util.IsErrBadRequest(err): case util.IsErrBadRequest(err):
fallthrough fallthrough
case util.IsErrNotFound(err): case util.IsErrNotFound(err):
fallthrough
case util.IsErrForbidden(err):
return &ErrorResponse{Message: err.Error()} return &ErrorResponse{Message: err.Error()}
} }
@ -59,6 +61,9 @@ func httpError(w http.ResponseWriter, err error) bool {
case util.IsErrNotFound(err): case util.IsErrNotFound(err):
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
w.Write(resj) w.Write(resj)
case util.IsErrForbidden(err):
w.WriteHeader(http.StatusForbidden)
w.Write(resj)
default: default:
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
w.Write(resj) w.Write(resj)

View File

@ -93,3 +93,22 @@ func IsErrNotFound(err error) bool {
_, ok := err.(*ErrNotFound) _, ok := err.(*ErrNotFound)
return ok return ok
} }
// ErrForbidden represent an error caused by an forbidden operation
// it's used to differentiate an internal error from an user error
type ErrForbidden struct {
Err error
}
func (e *ErrForbidden) Error() string {
return e.Err.Error()
}
func NewErrForbidden(err error) *ErrForbidden {
return &ErrForbidden{Err: err}
}
func IsErrForbidden(err error) bool {
_, ok := err.(*ErrForbidden)
return ok
}