mirror of
https://github.com/rancher/norman.git
synced 2025-09-08 10:39:17 +00:00
More initial dev
This commit is contained in:
35
httperror/handler.go
Normal file
35
httperror/handler.go
Normal file
@@ -0,0 +1,35 @@
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user