import "github.com/geek/herrors"func handler(w http.ResponseWriter, r *http.Request) {
// perform some validation
if !isValid(r) {
herrors.Write(w, herrors.ErrBadRequest)
return
}
}func validate(r *http.Request) error {
// ... do some validation and return an error on failure
}
func handler(w http.ResponseWriter, r *http.Request) {
if err := validate(r); err != nil {
err = herrors.Wrap(err, http.StatusBadRequest)
herrors.Write(w, err)
return
}
}func New(statusCode uint) errorNew constructs an ErrHttp error with the code and message populated
func Wrap(err error, statusCode uint) errorWrap will create a new ErrHttp and place the provided error inside of it. There is a complementary errors.Unwrap to retrieve the wrapped error.
func Write(w http.ResponseWriter, err error)Write sets the response status code to the provided error code. When unable to find an ErrHttp error in the error chain then a 500 internal error is output.
type ErrHttp struct {
}func (e *ErrHttp) Code() uintCode returns http status code that the error represents
func (e *ErrHttp) Error() stringError returns http friendly error message
func (e *ErrHttp) Is(target error) boolIs used by errors.Is for comparing ErrHttp
func (e *ErrHttp) Unwrap() errorUnwrap will return the first wrapped error if there is one