diff --git a/api/error.go b/api/error.go index dcc6ecb..7112fbb 100644 --- a/api/error.go +++ b/api/error.go @@ -32,5 +32,9 @@ func (e *Error) SetArguments(arg0 interface{}, args ...interface{}) { // Implements 'error' func (e Error) Error() string { - return fmt.Sprintf("[%d] %s", e.Code, e.Reason) + if e.Arguments == nil || len(e.Arguments) < 1 { + return fmt.Sprintf("[%d] %s", e.Code, e.Reason) + } + + return fmt.Sprintf("[%d] %s (%v)", e.Code, e.Reason, e.Arguments) } diff --git a/api/response.go b/api/response.go index d8bd700..bbaa058 100644 --- a/api/response.go +++ b/api/response.go @@ -13,7 +13,7 @@ type Response struct { Data ResponseData Status int Headers http.Header - Err Error + err Error } // NewResponse creates an empty response. An optional error can be passed as its first argument. @@ -21,24 +21,29 @@ func NewResponse(errors ...Error) *Response { res := &Response{ Status: http.StatusOK, Data: make(ResponseData), - Err: ErrorFailure(), + err: ErrorFailure(), Headers: make(http.Header), } // optional error if len(errors) == 1 { - res.Err = errors[0] + res.err = errors[0] } return res } -// WrapError sets the error from a base error with error arguments. -func (res *Response) WrapError(baseError Error, arguments ...interface{}) { +// SetError sets the error from a base error with error arguments. +func (res *Response) SetError(baseError Error, arguments ...interface{}) { if len(arguments) > 0 { baseError.SetArguments(arguments[0], arguments[1:]) } - res.Err = baseError + res.err = baseError +} + +// Error implements the error interface and dispatches to internal error. +func (res *Response) Error() string { + return res.err.Error() } // SetData adds/overrides a new response field @@ -62,7 +67,7 @@ func (res *Response) MarshalJSON() ([]byte, error) { fmt[k] = v } - fmt["error"] = res.Err + fmt["error"] = res.err return json.Marshal(fmt) } diff --git a/server.go b/server.go index 6964f60..a737a98 100644 --- a/server.go +++ b/server.go @@ -113,14 +113,14 @@ func (s *Server) ServeHTTP(res http.ResponseWriter, req *http.Request) { if serviceHandler == nil { if serviceFound { apiResponse := api.NewResponse() - apiResponse.WrapError(api.ErrorUncallableMethod(), servicePath, req.Method) + apiResponse.SetError(api.ErrorUncallableMethod(), servicePath, req.Method) apiResponse.Write(res) logError(apiResponse) return } apiResponse := api.NewResponse() - apiResponse.WrapError(api.ErrorUncallableService(), servicePath) + apiResponse.SetError(api.ErrorUncallableService(), servicePath) apiResponse.Write(res) logError(apiResponse) return diff --git a/util.go b/util.go index 2550d1b..9bc7964 100644 --- a/util.go +++ b/util.go @@ -79,5 +79,5 @@ func (s *Server) extractParameters(store *reqdata.Store, methodParam map[string] // Prints an error as HTTP response func logError(res *api.Response) { - log.Printf("[http.fail] %v\n", res.Err) + log.Printf("[http.fail] %v\n", res) }