From b9ca1801bdf9f0cd6c7fe8b8cf4290f5f98941fe Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Wed, 1 May 2019 18:05:58 +0200 Subject: [PATCH] fix: rename 'NewError' to 'WrapError' --- api/error.go | 4 ++-- server.go | 6 ++---- util.go | 20 ++++---------------- 3 files changed, 8 insertions(+), 22 deletions(-) diff --git a/api/error.go b/api/error.go index 2abcc01..eaf32b7 100644 --- a/api/error.go +++ b/api/error.go @@ -13,8 +13,8 @@ type Error struct { Arguments []interface{} `json:"arguments"` } -// NewError returns a new error from a base error with errorarguments. -func NewError(baseError Error, arguments ...interface{}) Error { +// WrapError returns a new error from a base error with errorarguments. +func WrapError(baseError Error, arguments ...interface{}) Error { for _, arg := range arguments { baseError.Put(arg) } diff --git a/server.go b/server.go index eadbb99..b3d4d42 100644 --- a/server.go +++ b/server.go @@ -112,15 +112,13 @@ func (s *Server) ServeHTTP(res http.ResponseWriter, req *http.Request) { // fail if found no handler if serviceHandler == nil { if serviceFound { - apiError := api.NewError(api.ErrorUncallableMethod(), servicePath, req.Method) - apiResponse := api.NewResponse(apiError) + apiResponse := api.NewResponse(api.WrapError(api.ErrorUncallableMethod(), servicePath, req.Method)) apiResponse.Write(res) logError(apiResponse) return } - apiError := api.NewError(api.ErrorUncallableService(), servicePath) - apiResponse := api.NewResponse(apiError) + apiResponse := api.NewResponse(api.WrapError(api.ErrorUncallableService(), servicePath)) apiResponse.Write(res) logError(apiResponse) return diff --git a/util.go b/util.go index 7a94034..b027634 100644 --- a/util.go +++ b/util.go @@ -13,7 +13,6 @@ import ( func (s *Server) extractParameters(store *reqdata.Store, methodParam map[string]*config.Parameter) (map[string]interface{}, api.Error) { // init vars - apiError := api.ErrorSuccess() parameters := make(map[string]interface{}) // for each param of the config @@ -24,9 +23,7 @@ func (s *Server) extractParameters(store *reqdata.Store, methodParam map[string] // 2. fail if required & missing if !isset && !param.Optional { - apiError = api.ErrorMissingParam() - apiError.Put(name) - return nil, apiError + return nil, api.WrapError(api.ErrorMissingParam(), name) } // 3. optional & missing: set default value @@ -53,10 +50,7 @@ func (s *Server) extractParameters(store *reqdata.Store, methodParam map[string] // 5. fail on unexpected multipart file waitFile, gotFile := param.Type == "FILE", p.File if gotFile && !waitFile || !gotFile && waitFile { - apiError = api.ErrorInvalidParam() - apiError.Put(param.Rename) - apiError.Put("FILE") - return nil, apiError + return nil, api.WrapError(api.ErrorInvalidParam(), param.Rename, "FILE") } // 6. do not check if file @@ -67,20 +61,14 @@ func (s *Server) extractParameters(store *reqdata.Store, methodParam map[string] // 7. check type if s.Checkers.Run(param.Type, p.Value) != nil { - - apiError = api.ErrorInvalidParam() - apiError.Put(param.Rename) - apiError.Put(param.Type) - apiError.Put(p.Value) - break - + return nil, api.WrapError(api.ErrorInvalidParam(), param.Rename, param.Type, p.Value) } parameters[param.Rename] = p.Value } - return parameters, apiError + return parameters, api.ErrorSuccess() } // Prints an error as HTTP response