fix: rename 'WrapError' to 'SetError' + shadow response error + response implements Error()
This commit is contained in:
parent
1e4476ced5
commit
7a87288876
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue