1
0
mirror of https://github.com/rancher/steve.git synced 2025-04-28 11:14:43 +00:00

Fix nil panic

This commit is contained in:
Darren Shepherd 2019-08-12 21:32:47 -07:00
parent 5671b6c3f9
commit d61942f795
3 changed files with 27 additions and 18 deletions

View File

@ -100,6 +100,10 @@ func (s *Server) setDefaults(ctx *types.APIRequest) {
} }
ctx.AccessControl = s.AccessControl ctx.AccessControl = s.AccessControl
if ctx.Schemas == nil {
ctx.Schemas = s.Schemas
}
} }
func (s *Server) AddSchemas(schemas *types.Schemas) error { func (s *Server) AddSchemas(schemas *types.Schemas) error {
@ -162,14 +166,15 @@ func (s *Server) Handle(apiOp *types.APIRequest) {
} }
func (s *Server) handle(apiOp *types.APIRequest, rw http.ResponseWriter, req *http.Request, parser parse.Parser) { func (s *Server) handle(apiOp *types.APIRequest, rw http.ResponseWriter, req *http.Request, parser parse.Parser) {
if apiOp.Schemas == nil {
apiOp.Schemas = s.Schemas
}
if err := parser(apiOp, parse.MuxURLParser); err != nil { if err := parser(apiOp, parse.MuxURLParser); err != nil {
// ensure defaults set so writer is assigned
s.setDefaults(apiOp)
s.handleError(apiOp, err) s.handleError(apiOp, err)
return return
} }
s.setDefaults(apiOp)
if code, data, err := s.handleOp(apiOp); err != nil { if code, data, err := s.handleOp(apiOp); err != nil {
s.handleError(apiOp, err) s.handleError(apiOp, err)
} else { } else {
@ -200,8 +205,6 @@ func determineVerb(apiOp *types.APIRequest) Verb {
} }
func (s *Server) handleOp(apiOp *types.APIRequest) (int, interface{}, error) { func (s *Server) handleOp(apiOp *types.APIRequest) (int, interface{}, error) {
s.setDefaults(apiOp)
if err := CheckCSRF(apiOp); err != nil { if err := CheckCSRF(apiOp); err != nil {
return 0, nil, err return 0, nil, err
} }
@ -261,10 +264,10 @@ func handleAction(action *types.Action, context *types.APIRequest) error {
} }
func (s *Server) handleError(apiOp *types.APIRequest, err error) { func (s *Server) handleError(apiOp *types.APIRequest, err error) {
if apiOp.Schema == nil { if apiOp.Schema != nil && apiOp.Schema.ErrorHandler != nil {
s.Defaults.ErrorHandler(apiOp, err)
} else if apiOp.Schema.ErrorHandler != nil {
apiOp.Schema.ErrorHandler(apiOp, err) apiOp.Schema.ErrorHandler(apiOp, err)
} else if s.Defaults.ErrorHandler != nil {
s.Defaults.ErrorHandler(apiOp, err)
} }
} }

View File

@ -62,6 +62,7 @@ func (j *EncodingResponseWriter) VersionBody(apiOp *types.APIRequest, writer io.
case types.RawResource: case types.RawResource:
output = v output = v
default: default:
if v != nil {
mapData, err := convert.EncodeToMap(obj) mapData, err := convert.EncodeToMap(obj)
if err != nil { if err != nil {
return err return err
@ -72,6 +73,7 @@ func (j *EncodingResponseWriter) VersionBody(apiOp *types.APIRequest, writer io.
} }
output = j.convert(builder, apiOp, mapData) output = j.convert(builder, apiOp, mapData)
} }
}
if list, ok := output.(*types.GenericCollection); ok && revision != "" { if list, ok := output.(*types.GenericCollection); ok && revision != "" {
list.Revision = revision list.Revision = revision

View File

@ -238,10 +238,14 @@ func (a *APIObject) Raw() interface{} {
} }
func (a *APIObject) Map() data.Object { func (a *APIObject) Map() data.Object {
if a == nil { if a == nil || a.IsNil() {
return nil return nil
} }
data, err := convert.EncodeToMap(a.Object)
if err != nil {
return convert.ToMapInterface(a.Object) return convert.ToMapInterface(a.Object)
}
return data
} }
func (a APIObject) IsNil() bool { func (a APIObject) IsNil() bool {