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 |
||||
|
||||
import "net/http" |
||||
import ( |
||||
"errors" |
||||
"fmt" |
||||
) |
||||
|
||||
// BadRequest new BadRequest error that is mapped to a 400 response.
|
||||
func BadRequest(domain, reason, message string) *Error { |
||||
return New(http.StatusBadRequest, domain, reason, message) |
||||
// ErrorInfo is describes the cause of the error with structured details.
|
||||
// For more details see https://github.com/googleapis/googleapis/blob/master/google/rpc/error_details.proto.
|
||||
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.
|
||||
// It supports wrapped errors.
|
||||
func IsBadRequest(err error) bool { |
||||
return Code(err) == http.StatusBadRequest |
||||
func (e *ErrorInfo) Error() string { |
||||
return fmt.Sprintf("error: domain = %s reason = %s", e.Domain, e.Reason) |
||||
} |
||||
|
||||
// Unauthorized new Unauthorized error that is mapped to a 401 response.
|
||||
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 |
||||
func (e *ErrorInfo) Unwrap() error { |
||||
return e.err |
||||
} |
||||
|
||||
// ServiceUnavailable new ServiceUnavailable error that is mapped to a HTTP 503 response.
|
||||
func ServiceUnavailable(domain, reason, message string) *Error { |
||||
return New(http.StatusServiceUnavailable, domain, reason, message) |
||||
// Is matches each error in the chain with the target value.
|
||||
func (e *ErrorInfo) Is(err error) bool { |
||||
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.
|
||||
// It supports wrapped errors.
|
||||
func IsServiceUnavailable(err error) bool { |
||||
return Code(err) == http.StatusServiceUnavailable |
||||
// WithMetadata with an MD formed by the mapping of key, value.
|
||||
func (e *ErrorInfo) WithMetadata(md map[string]string) *ErrorInfo { |
||||
err := *e |
||||
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