1
0
mirror of https://github.com/rancher/norman.git synced 2025-09-15 06:44:31 +00:00

Support unwrapping errors to find httperror.APIError

This commit is contained in:
Darren Shepherd
2020-07-27 22:55:26 -07:00
parent b3163ad4eb
commit 559ff3e820

View File

@@ -1,6 +1,7 @@
package handler package handler
import ( import (
"errors"
"net/url" "net/url"
"github.com/rancher/norman/httperror" "github.com/rancher/norman/httperror"
@@ -9,17 +10,16 @@ import (
) )
func ErrorHandler(request *types.APIContext, err error) { func ErrorHandler(request *types.APIContext, err error) {
var error *httperror.APIError error := &httperror.APIError{}
if apiError, ok := err.(*httperror.APIError); ok { if errors.As(err, &error) {
if apiError.Cause != nil { if error.Cause != nil {
url, _ := url.PathUnescape(request.Request.URL.String()) url, _ := url.PathUnescape(request.Request.URL.String())
if url == "" { if url == "" {
url = request.Request.URL.String() url = request.Request.URL.String()
} }
logrus.Errorf("API error response %v for %v %v. Cause: %v", apiError.Code.Status, request.Request.Method, logrus.Errorf("API error response %v for %v %v. Cause: %v", error.Code.Status, request.Request.Method,
url, apiError.Cause) url, error.Cause)
} }
error = apiError
} else { } else {
logrus.Errorf("Unknown error: %v", err) logrus.Errorf("Unknown error: %v", err)
error = &httperror.APIError{ error = &httperror.APIError{