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"`
}
// 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)
}

View File

@ -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

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) {
// 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