refactor errors (#863)
parent
b9e905c1af
commit
e1c84ece84
@ -0,0 +1,80 @@ |
|||||||
|
package errors |
||||||
|
|
||||||
|
import "net/http" |
||||||
|
|
||||||
|
// BadRequest new BadRequest error that is mapped to a 400 response.
|
||||||
|
func BadRequest(domain, reason, message string) *ErrorInfo { |
||||||
|
return Errorf(http.StatusBadRequest, domain, reason, message) |
||||||
|
} |
||||||
|
|
||||||
|
// IsBadRequest determines if err is an error which indicates a BadRequest error.
|
||||||
|
// It supports wrapped errors.
|
||||||
|
func IsBadRequest(err error) bool { |
||||||
|
return Code(err) == http.StatusBadRequest |
||||||
|
} |
||||||
|
|
||||||
|
// Unauthorized new Unauthorized error that is mapped to a 401 response.
|
||||||
|
func Unauthorized(domain, reason, message string) *ErrorInfo { |
||||||
|
return Errorf(http.StatusUnauthorized, domain, reason, message) |
||||||
|
} |
||||||
|
|
||||||
|
// IsUnauthorized determines if err is an error which indicates a Unauthorized error.
|
||||||
|
// It supports wrapped errors.
|
||||||
|
func IsUnauthorized(err error) bool { |
||||||
|
return Code(err) == http.StatusUnauthorized |
||||||
|
} |
||||||
|
|
||||||
|
// Forbidden new Forbidden error that is mapped to a 403 response.
|
||||||
|
func Forbidden(domain, reason, message string) *ErrorInfo { |
||||||
|
return Errorf(http.StatusForbidden, domain, reason, message) |
||||||
|
} |
||||||
|
|
||||||
|
// IsForbidden determines if err is an error which indicates a Forbidden error.
|
||||||
|
// It supports wrapped errors.
|
||||||
|
func IsForbidden(err error) bool { |
||||||
|
return Code(err) == http.StatusForbidden |
||||||
|
} |
||||||
|
|
||||||
|
// NotFound new NotFound error that is mapped to a 404 response.
|
||||||
|
func NotFound(domain, reason, message string) *ErrorInfo { |
||||||
|
return Errorf(http.StatusNotFound, domain, reason, message) |
||||||
|
} |
||||||
|
|
||||||
|
// IsNotFound determines if err is an error which indicates an NotFound error.
|
||||||
|
// It supports wrapped errors.
|
||||||
|
func IsNotFound(err error) bool { |
||||||
|
return Code(err) == http.StatusNotFound |
||||||
|
} |
||||||
|
|
||||||
|
// Conflict new Conflict error that is mapped to a 409 response.
|
||||||
|
func Conflict(domain, reason, message string) *ErrorInfo { |
||||||
|
return Errorf(http.StatusConflict, domain, reason, message) |
||||||
|
} |
||||||
|
|
||||||
|
// IsConflict determines if err is an error which indicates a Conflict error.
|
||||||
|
// It supports wrapped errors.
|
||||||
|
func IsConflict(err error) bool { |
||||||
|
return Code(err) == http.StatusConflict |
||||||
|
} |
||||||
|
|
||||||
|
// InternalServer new InternalServer error that is mapped to a 500 response.
|
||||||
|
func InternalServer(domain, reason, message string) *ErrorInfo { |
||||||
|
return Errorf(http.StatusInternalServerError, domain, reason, message) |
||||||
|
} |
||||||
|
|
||||||
|
// IsInternalServer determines if err is an error which indicates an InternalServer error.
|
||||||
|
// It supports wrapped errors.
|
||||||
|
func IsInternalServer(err error) bool { |
||||||
|
return Code(err) == http.StatusInternalServerError |
||||||
|
} |
||||||
|
|
||||||
|
// ServiceUnavailable new ServiceUnavailable error that is mapped to a HTTP 503 response.
|
||||||
|
func ServiceUnavailable(domain, reason, message string) *ErrorInfo { |
||||||
|
return Errorf(http.StatusServiceUnavailable, domain, reason, message) |
||||||
|
} |
||||||
|
|
||||||
|
// IsServiceUnavailable determines if err is an error which indicates a ServiceUnavailable error.
|
||||||
|
// It supports wrapped errors.
|
||||||
|
func IsServiceUnavailable(err error) bool { |
||||||
|
return Code(err) == http.StatusServiceUnavailable |
||||||
|
} |
@ -1,80 +1,37 @@ |
|||||||
package errors |
package errors |
||||||
|
|
||||||
import "net/http" |
import ( |
||||||
|
"errors" |
||||||
|
"fmt" |
||||||
|
) |
||||||
|
|
||||||
// BadRequest new BadRequest error that is mapped to a 400 response.
|
// ErrorInfo is describes the cause of the error with structured details.
|
||||||
func BadRequest(domain, reason, message string) *Error { |
// For more details see https://github.com/googleapis/googleapis/blob/master/google/rpc/error_details.proto.
|
||||||
return New(http.StatusBadRequest, domain, reason, message) |
type ErrorInfo struct { |
||||||
|
err *Error |
||||||
|
Domain string `json:"domain"` |
||||||
|
Reason string `json:"reason"` |
||||||
|
Metadata map[string]string `json:"metadata"` |
||||||
} |
} |
||||||
|
|
||||||
// IsBadRequest determines if err is an error which indicates a BadRequest error.
|
func (e *ErrorInfo) Error() string { |
||||||
// It supports wrapped errors.
|
return fmt.Sprintf("error: domain = %s reason = %s", e.Domain, e.Reason) |
||||||
func IsBadRequest(err error) bool { |
|
||||||
return Code(err) == http.StatusBadRequest |
|
||||||
} |
} |
||||||
|
func (e *ErrorInfo) Unwrap() error { |
||||||
// Unauthorized new Unauthorized error that is mapped to a 401 response.
|
return e.err |
||||||
func Unauthorized(domain, reason, message string) *Error { |
|
||||||
return New(http.StatusUnauthorized, domain, reason, message) |
|
||||||
} |
|
||||||
|
|
||||||
// IsUnauthorized determines if err is an error which indicates a Unauthorized error.
|
|
||||||
// It supports wrapped errors.
|
|
||||||
func IsUnauthorized(err error) bool { |
|
||||||
return Code(err) == http.StatusUnauthorized |
|
||||||
} |
|
||||||
|
|
||||||
// Forbidden new Forbidden error that is mapped to a 403 response.
|
|
||||||
func Forbidden(domain, reason, message string) *Error { |
|
||||||
return New(http.StatusForbidden, domain, reason, message) |
|
||||||
} |
|
||||||
|
|
||||||
// IsForbidden determines if err is an error which indicates a Forbidden error.
|
|
||||||
// It supports wrapped errors.
|
|
||||||
func IsForbidden(err error) bool { |
|
||||||
return Code(err) == http.StatusForbidden |
|
||||||
} |
|
||||||
|
|
||||||
// NotFound new NotFound error that is mapped to a 404 response.
|
|
||||||
func NotFound(domain, reason, message string) *Error { |
|
||||||
return New(http.StatusNotFound, domain, reason, message) |
|
||||||
} |
|
||||||
|
|
||||||
// IsNotFound determines if err is an error which indicates an NotFound error.
|
|
||||||
// It supports wrapped errors.
|
|
||||||
func IsNotFound(err error) bool { |
|
||||||
return Code(err) == http.StatusNotFound |
|
||||||
} |
|
||||||
|
|
||||||
// Conflict new Conflict error that is mapped to a 409 response.
|
|
||||||
func Conflict(domain, reason, message string) *Error { |
|
||||||
return New(http.StatusConflict, domain, reason, message) |
|
||||||
} |
|
||||||
|
|
||||||
// IsConflict determines if err is an error which indicates a Conflict error.
|
|
||||||
// It supports wrapped errors.
|
|
||||||
func IsConflict(err error) bool { |
|
||||||
return Code(err) == http.StatusConflict |
|
||||||
} |
|
||||||
|
|
||||||
// InternalServer new InternalServer error that is mapped to a 500 response.
|
|
||||||
func InternalServer(domain, reason, message string) *Error { |
|
||||||
return New(http.StatusInternalServerError, domain, reason, message) |
|
||||||
} |
|
||||||
|
|
||||||
// IsInternalServer determines if err is an error which indicates an InternalServer error.
|
|
||||||
// It supports wrapped errors.
|
|
||||||
func IsInternalServer(err error) bool { |
|
||||||
return Code(err) == http.StatusInternalServerError |
|
||||||
} |
} |
||||||
|
|
||||||
// ServiceUnavailable new ServiceUnavailable error that is mapped to a HTTP 503 response.
|
// Is matches each error in the chain with the target value.
|
||||||
func ServiceUnavailable(domain, reason, message string) *Error { |
func (e *ErrorInfo) Is(err error) bool { |
||||||
return New(http.StatusServiceUnavailable, domain, reason, message) |
if target := new(ErrorInfo); errors.As(err, &target) { |
||||||
|
return target.Domain == e.Domain && target.Reason == e.Reason |
||||||
|
} |
||||||
|
return false |
||||||
} |
} |
||||||
|
|
||||||
// IsServiceUnavailable determines if err is an error which indicates a ServiceUnavailable error.
|
// WithMetadata with an MD formed by the mapping of key, value.
|
||||||
// It supports wrapped errors.
|
func (e *ErrorInfo) WithMetadata(md map[string]string) *ErrorInfo { |
||||||
func IsServiceUnavailable(err error) bool { |
err := *e |
||||||
return Code(err) == http.StatusServiceUnavailable |
err.Metadata = md |
||||||
|
return &err |
||||||
} |
} |
||||||
|
@ -0,0 +1,36 @@ |
|||||||
|
package errors |
||||||
|
|
||||||
|
import ( |
||||||
|
stderrors "errors" |
||||||
|
) |
||||||
|
|
||||||
|
// Is reports whether any error in err's chain matches target.
|
||||||
|
//
|
||||||
|
// The chain consists of err itself followed by the sequence of errors obtained by
|
||||||
|
// repeatedly calling Unwrap.
|
||||||
|
//
|
||||||
|
// An error is considered to match a target if it is equal to that target or if
|
||||||
|
// it implements a method Is(error) bool such that Is(target) returns true.
|
||||||
|
func Is(err, target error) bool { return stderrors.Is(err, target) } |
||||||
|
|
||||||
|
// As finds the first error in err's chain that matches target, and if so, sets
|
||||||
|
// target to that error value and returns true.
|
||||||
|
//
|
||||||
|
// The chain consists of err itself followed by the sequence of errors obtained by
|
||||||
|
// repeatedly calling Unwrap.
|
||||||
|
//
|
||||||
|
// An error matches target if the error's concrete value is assignable to the value
|
||||||
|
// pointed to by target, or if the error has a method As(interface{}) bool such that
|
||||||
|
// As(target) returns true. In the latter case, the As method is responsible for
|
||||||
|
// setting target.
|
||||||
|
//
|
||||||
|
// As will panic if target is not a non-nil pointer to either a type that implements
|
||||||
|
// error, or to any interface type. As returns false if err is nil.
|
||||||
|
func As(err error, target interface{}) bool { return stderrors.As(err, target) } |
||||||
|
|
||||||
|
// Unwrap returns the result of calling the Unwrap method on err, if err's
|
||||||
|
// type contains an Unwrap method returning error.
|
||||||
|
// Otherwise, Unwrap returns nil.
|
||||||
|
func Unwrap(err error) error { |
||||||
|
return stderrors.Unwrap(err) |
||||||
|
} |
Loading…
Reference in new issue