mirror of
https://github.com/rancher/norman.git
synced 2025-07-18 01:11:27 +00:00
36 lines
710 B
Go
36 lines
710 B
Go
|
package httperror
|
||
|
|
||
|
import (
|
||
|
"github.com/rancher/norman/types"
|
||
|
"github.com/sirupsen/logrus"
|
||
|
)
|
||
|
|
||
|
func ErrorHandler(request *types.APIContext, err error) {
|
||
|
var error *APIError
|
||
|
if apiError, ok := err.(*APIError); ok {
|
||
|
error = apiError
|
||
|
} else {
|
||
|
logrus.Errorf("Unknown error: %v", err)
|
||
|
error = &APIError{
|
||
|
code: SERVER_ERROR,
|
||
|
message: err.Error(),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
data := toError(error)
|
||
|
request.WriteResponse(error.code.status, data)
|
||
|
}
|
||
|
|
||
|
func toError(apiError *APIError) map[string]interface{} {
|
||
|
e := map[string]interface{}{
|
||
|
"type": "/v3/schema",
|
||
|
"code": apiError.code.code,
|
||
|
"message": apiError.message,
|
||
|
}
|
||
|
if apiError.fieldName != "" {
|
||
|
e["fieldName"] = apiError.fieldName
|
||
|
}
|
||
|
|
||
|
return e
|
||
|
}
|