mirror of
https://github.com/rancher/norman.git
synced 2025-09-06 17:50:25 +00:00
Send k8s error bad as something other than 500 server error
This commit is contained in:
@@ -46,6 +46,13 @@ type APIError struct {
|
|||||||
fieldName string
|
fieldName string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewAPIErrorLong(status int, code, message string) error {
|
||||||
|
return NewAPIError(ErrorCode{
|
||||||
|
code: code,
|
||||||
|
status: status,
|
||||||
|
}, message)
|
||||||
|
}
|
||||||
|
|
||||||
func NewAPIError(code ErrorCode, message string) error {
|
func NewAPIError(code ErrorCode, message string) error {
|
||||||
return &APIError{
|
return &APIError{
|
||||||
code: code,
|
code: code,
|
||||||
|
@@ -15,7 +15,7 @@ import (
|
|||||||
type Store struct {
|
type Store struct {
|
||||||
Factory *Factory
|
Factory *Factory
|
||||||
k8sClient rest.Interface
|
k8sClient rest.Interface
|
||||||
schemaStores map[string]*proxy.Store
|
schemaStores map[string]types.Store
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCRDStoreFromConfig(config rest.Config) (*Store, error) {
|
func NewCRDStoreFromConfig(config rest.Config) (*Store, error) {
|
||||||
@@ -44,7 +44,7 @@ func NewCRDStoreFromClients(apiExtClientSet apiextclientset.Interface, k8sClient
|
|||||||
APIExtClientSet: apiExtClientSet,
|
APIExtClientSet: apiExtClientSet,
|
||||||
},
|
},
|
||||||
k8sClient: k8sClient,
|
k8sClient: k8sClient,
|
||||||
schemaStores: map[string]*proxy.Store{},
|
schemaStores: map[string]types.Store{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
52
store/proxy/error_wrapper.go
Normal file
52
store/proxy/error_wrapper.go
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
package proxy
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/rancher/norman/httperror"
|
||||||
|
"github.com/rancher/norman/types"
|
||||||
|
"k8s.io/apimachinery/pkg/api/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
type errorStore struct {
|
||||||
|
types.Store
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *errorStore) ByID(apiContext *types.APIContext, schema *types.Schema, id string) (map[string]interface{}, error) {
|
||||||
|
data, err := e.Store.ByID(apiContext, schema, id)
|
||||||
|
return data, translateError(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *errorStore) List(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) ([]map[string]interface{}, error) {
|
||||||
|
data, err := e.Store.List(apiContext, schema, opt)
|
||||||
|
return data, translateError(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *errorStore) Create(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}) (map[string]interface{}, error) {
|
||||||
|
data, err := e.Store.Create(apiContext, schema, data)
|
||||||
|
return data, translateError(err)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *errorStore) Update(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}, id string) (map[string]interface{}, error) {
|
||||||
|
data, err := e.Store.Update(apiContext, schema, data, id)
|
||||||
|
return data, translateError(err)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *errorStore) Delete(apiContext *types.APIContext, schema *types.Schema, id string) (map[string]interface{}, error) {
|
||||||
|
data, err := e.Store.Delete(apiContext, schema, id)
|
||||||
|
return data, translateError(err)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *errorStore) Watch(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) (chan map[string]interface{}, error) {
|
||||||
|
data, err := e.Store.Watch(apiContext, schema, opt)
|
||||||
|
return data, translateError(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func translateError(err error) error {
|
||||||
|
if apiError, ok := err.(errors.APIStatus); ok {
|
||||||
|
status := apiError.Status()
|
||||||
|
return httperror.NewAPIErrorLong(int(status.Code), string(status.Reason), status.Message)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
@@ -40,6 +40,24 @@ type Store struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewProxyStore(k8sClient rest.Interface,
|
func NewProxyStore(k8sClient rest.Interface,
|
||||||
|
prefix []string, group, version, kind, resourcePlural string) types.Store {
|
||||||
|
return &errorStore{
|
||||||
|
Store: &Store{
|
||||||
|
k8sClient: k8sClient,
|
||||||
|
prefix: prefix,
|
||||||
|
group: group,
|
||||||
|
version: version,
|
||||||
|
kind: kind,
|
||||||
|
resourcePlural: resourcePlural,
|
||||||
|
authContext: map[string]string{
|
||||||
|
"apiGroup": group,
|
||||||
|
"resource": resourcePlural,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRawProxyStore(k8sClient rest.Interface,
|
||||||
prefix []string, group, version, kind, resourcePlural string) *Store {
|
prefix []string, group, version, kind, resourcePlural string) *Store {
|
||||||
return &Store{
|
return &Store{
|
||||||
k8sClient: k8sClient,
|
k8sClient: k8sClient,
|
||||||
|
Reference in New Issue
Block a user