Swap custom implementation for errors.As()

This commit is contained in:
Ian Gulliver
2023-04-30 15:16:23 -07:00
parent 2ae31b2554
commit 9f19b9d8da

View File

@@ -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