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) error
New constructs an ErrHttp error with the code and message populated
func Wrap(err error, statusCode uint) error
Wrap 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() uint
Code returns http status code that the error represents
func (e *ErrHttp) Error() string
Error returns http friendly error message
func (e *ErrHttp) Is(target error) bool
Is used by errors.Is for comparing ErrHttp
func (e *ErrHttp) Unwrap() error
Unwrap will return the first wrapped error if there is one