fix: rename 'NewError' to 'WrapError'

This commit is contained in:
Adrien Marquès 2019-05-01 18:05:58 +02:00
parent b9f240b86b
commit b9ca1801bd
3 changed files with 8 additions and 22 deletions

View File

@ -13,8 +13,8 @@ type Error struct {
Arguments []interface{} `json:"arguments"` Arguments []interface{} `json:"arguments"`
} }
// NewError returns a new error from a base error with errorarguments. // WrapError returns a new error from a base error with errorarguments.
func NewError(baseError Error, arguments ...interface{}) Error { func WrapError(baseError Error, arguments ...interface{}) Error {
for _, arg := range arguments { for _, arg := range arguments {
baseError.Put(arg) baseError.Put(arg)
} }

View File

@ -112,15 +112,13 @@ func (s *Server) ServeHTTP(res http.ResponseWriter, req *http.Request) {
// fail if found no handler // fail if found no handler
if serviceHandler == nil { if serviceHandler == nil {
if serviceFound { if serviceFound {
apiError := api.NewError(api.ErrorUncallableMethod(), servicePath, req.Method) apiResponse := api.NewResponse(api.WrapError(api.ErrorUncallableMethod(), servicePath, req.Method))
apiResponse := api.NewResponse(apiError)
apiResponse.Write(res) apiResponse.Write(res)
logError(apiResponse) logError(apiResponse)
return return
} }
apiError := api.NewError(api.ErrorUncallableService(), servicePath) apiResponse := api.NewResponse(api.WrapError(api.ErrorUncallableService(), servicePath))
apiResponse := api.NewResponse(apiError)
apiResponse.Write(res) apiResponse.Write(res)
logError(apiResponse) logError(apiResponse)
return return

20
util.go
View File

@ -13,7 +13,6 @@ import (
func (s *Server) extractParameters(store *reqdata.Store, methodParam map[string]*config.Parameter) (map[string]interface{}, api.Error) { func (s *Server) extractParameters(store *reqdata.Store, methodParam map[string]*config.Parameter) (map[string]interface{}, api.Error) {
// init vars // init vars
apiError := api.ErrorSuccess()
parameters := make(map[string]interface{}) parameters := make(map[string]interface{})
// for each param of the config // 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 // 2. fail if required & missing
if !isset && !param.Optional { if !isset && !param.Optional {
apiError = api.ErrorMissingParam() return nil, api.WrapError(api.ErrorMissingParam(), name)
apiError.Put(name)
return nil, apiError
} }
// 3. optional & missing: set default value // 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 // 5. fail on unexpected multipart file
waitFile, gotFile := param.Type == "FILE", p.File waitFile, gotFile := param.Type == "FILE", p.File
if gotFile && !waitFile || !gotFile && waitFile { if gotFile && !waitFile || !gotFile && waitFile {
apiError = api.ErrorInvalidParam() return nil, api.WrapError(api.ErrorInvalidParam(), param.Rename, "FILE")
apiError.Put(param.Rename)
apiError.Put("FILE")
return nil, apiError
} }
// 6. do not check if file // 6. do not check if file
@ -67,20 +61,14 @@ func (s *Server) extractParameters(store *reqdata.Store, methodParam map[string]
// 7. check type // 7. check type
if s.Checkers.Run(param.Type, p.Value) != nil { if s.Checkers.Run(param.Type, p.Value) != nil {
return nil, api.WrapError(api.ErrorInvalidParam(), param.Rename, param.Type, p.Value)
apiError = api.ErrorInvalidParam()
apiError.Put(param.Rename)
apiError.Put(param.Type)
apiError.Put(p.Value)
break
} }
parameters[param.Rename] = p.Value parameters[param.Rename] = p.Value
} }
return parameters, apiError return parameters, api.ErrorSuccess()
} }
// Prints an error as HTTP response // Prints an error as HTTP response