fix wrong error code on wrapped status errors

This commit is contained in:
Alex Grintsvayg 2018-08-22 12:26:02 -04:00
parent f06abb1f02
commit 837e146b2b
No known key found for this signature in database
GPG key ID: AEB3F089F86A22B5
2 changed files with 24 additions and 1 deletions

View file

@ -78,7 +78,8 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if rsp.Status == 0 {
if rsp.Error != nil {
if statusError, ok := rsp.Error.(StatusError); ok {
ogErr := errors.Unwrap(rsp.Error)
if statusError, ok := ogErr.(StatusError); ok {
rsp.Status = statusError.Status
} else {
rsp.Status = http.StatusInternalServerError

View file

@ -41,6 +41,28 @@ func Wrap(err interface{}, skip int) *errors.Error {
return errors.Wrap(err, skip+1)
}
// Unwrap returns the original error that was wrapped
func Unwrap(err error) error {
if err == nil {
return nil
}
deeper := true
for deeper {
deeper = false
if e, ok := err.(*errors.Error); ok {
err = e.Err
deeper = true
}
if c, ok := err.(causer); ok {
err = c.Cause()
deeper = true
}
}
return err
}
// Is compares two wrapped errors to determine if the underlying errors are the same
// It also interops with errors from pkg/errors
func Is(e error, original error) bool {