feat: rename 'Error.Put' to 'SetArguments' with n >= 1 arguments + move 'WrapError' to response

This commit is contained in:
Adrien Marquès 2019-05-01 18:15:44 +02:00
parent b9ca1801bd
commit 1e4476ced5
4 changed files with 42 additions and 31 deletions

View File

@ -13,26 +13,21 @@ type Error struct {
Arguments []interface{} `json:"arguments"` Arguments []interface{} `json:"arguments"`
} }
// WrapError returns a new error from a base error with errorarguments. // SetArguments set one or multiple arguments to the error
func WrapError(baseError Error, arguments ...interface{}) Error {
for _, arg := range arguments {
baseError.Put(arg)
}
return baseError
}
// Put adds an argument to the error
// to be displayed back to API caller // to be displayed back to API caller
func (e *Error) Put(arg interface{}) { func (e *Error) SetArguments(arg0 interface{}, args ...interface{}) {
/* (1) Make slice if not */ // 1. clear arguments */
if e.Arguments == nil { e.Arguments = make([]interface{}, 0)
e.Arguments = make([]interface{}, 0)
// 2. add arg[0]
e.Arguments = append(e.Arguments, arg0)
// 3. add optional other arguments
for _, arg := range args {
e.Arguments = append(e.Arguments, arg)
} }
/* (2) Append argument */
e.Arguments = append(e.Arguments, arg)
} }
// Implements 'error' // Implements 'error'

View File

@ -33,38 +33,46 @@ func NewResponse(errors ...Error) *Response {
return res return res
} }
// WrapError sets the error from a base error with error arguments.
func (res *Response) WrapError(baseError Error, arguments ...interface{}) {
if len(arguments) > 0 {
baseError.SetArguments(arguments[0], arguments[1:])
}
res.Err = baseError
}
// SetData adds/overrides a new response field // SetData adds/overrides a new response field
func (i *Response) SetData(name string, value interface{}) { func (res *Response) SetData(name string, value interface{}) {
i.Data[name] = value res.Data[name] = value
} }
// GetData gets a response field // GetData gets a response field
func (i *Response) GetData(name string) interface{} { func (res *Response) GetData(name string) interface{} {
value, _ := i.Data[name] value, _ := res.Data[name]
return value return value
} }
// MarshalJSON implements the 'json.Marshaler' interface and is used // MarshalJSON implements the 'json.Marshaler' interface and is used
// to generate the JSON representation of the response // to generate the JSON representation of the response
func (i *Response) MarshalJSON() ([]byte, error) { func (res *Response) MarshalJSON() ([]byte, error) {
fmt := make(map[string]interface{}) fmt := make(map[string]interface{})
for k, v := range i.Data { for k, v := range res.Data {
fmt[k] = v fmt[k] = v
} }
fmt["error"] = i.Err fmt["error"] = res.Err
return json.Marshal(fmt) return json.Marshal(fmt)
} }
// Write writes to an HTTP response. // Write writes to an HTTP response.
func (i *Response) Write(w http.ResponseWriter) error { func (res *Response) Write(w http.ResponseWriter) error {
w.WriteHeader(i.Status) w.WriteHeader(res.Status)
w.Header().Add("Content-Type", "application/json") w.Header().Add("Content-Type", "application/json")
fmt, err := json.Marshal(i) fmt, err := json.Marshal(res)
if err != nil { if err != nil {
return err return err
} }

View File

@ -112,13 +112,15 @@ 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 {
apiResponse := api.NewResponse(api.WrapError(api.ErrorUncallableMethod(), servicePath, req.Method)) apiResponse := api.NewResponse()
apiResponse.WrapError(api.ErrorUncallableMethod(), servicePath, req.Method)
apiResponse.Write(res) apiResponse.Write(res)
logError(apiResponse) logError(apiResponse)
return return
} }
apiResponse := api.NewResponse(api.WrapError(api.ErrorUncallableService(), servicePath)) apiResponse := api.NewResponse()
apiResponse.WrapError(api.ErrorUncallableService(), servicePath)
apiResponse.Write(res) apiResponse.Write(res)
logError(apiResponse) logError(apiResponse)
return return

12
util.go
View File

@ -23,7 +23,9 @@ 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 {
return nil, api.WrapError(api.ErrorMissingParam(), name) apiErr := api.ErrorMissingParam()
apiErr.SetArguments(name)
return nil, apiErr
} }
// 3. optional & missing: set default value // 3. optional & missing: set default value
@ -50,7 +52,9 @@ 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 {
return nil, api.WrapError(api.ErrorInvalidParam(), param.Rename, "FILE") apiErr := api.ErrorInvalidParam()
apiErr.SetArguments(param.Rename, "FILE")
return nil, apiErr
} }
// 6. do not check if file // 6. do not check if file
@ -61,7 +65,9 @@ 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) apiErr := api.ErrorInvalidParam()
apiErr.SetArguments(param.Rename, param.Type, p.Value)
return nil, apiErr
} }
parameters[param.Rename] = p.Value parameters[param.Rename] = p.Value