Swap custom implementation for errors.As()
This commit is contained in:
17
error.go
17
error.go
@@ -2,6 +2,7 @@ package jsrest
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
@@ -159,20 +160,10 @@ type multiUnwrap interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func GetHTTPError(err error) *HTTPError {
|
func GetHTTPError(err error) *HTTPError {
|
||||||
// TODO: Rewrite using errors.As
|
hErr := &HTTPError{}
|
||||||
if hErr, has := err.(*HTTPError); has { //nolint:errorlint
|
|
||||||
return hErr
|
|
||||||
}
|
|
||||||
|
|
||||||
if unwrap, ok := err.(singleUnwrap); ok { //nolint:errorlint
|
if errors.As(err, &hErr) {
|
||||||
return GetHTTPError(unwrap.Unwrap())
|
return hErr
|
||||||
} else if unwrap, ok := err.(multiUnwrap); ok { //nolint:errorlint
|
|
||||||
for _, sub := range unwrap.Unwrap() {
|
|
||||||
hErr := GetHTTPError(sub)
|
|
||||||
if hErr != nil {
|
|
||||||
return hErr
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
Reference in New Issue
Block a user