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'
|
// Implements 'error'
|
||||||
func (e Error) Error() string {
|
func (e Error) Error() string {
|
||||||
|
if e.Arguments == nil || len(e.Arguments) < 1 {
|
||||||
return fmt.Sprintf("[%d] %s", e.Code, e.Reason)
|
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
|
Data ResponseData
|
||||||
Status int
|
Status int
|
||||||
Headers http.Header
|
Headers http.Header
|
||||||
Err Error
|
err Error
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewResponse creates an empty response. An optional error can be passed as its first argument.
|
// 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{
|
res := &Response{
|
||||||
Status: http.StatusOK,
|
Status: http.StatusOK,
|
||||||
Data: make(ResponseData),
|
Data: make(ResponseData),
|
||||||
Err: ErrorFailure(),
|
err: ErrorFailure(),
|
||||||
Headers: make(http.Header),
|
Headers: make(http.Header),
|
||||||
}
|
}
|
||||||
|
|
||||||
// optional error
|
// optional error
|
||||||
if len(errors) == 1 {
|
if len(errors) == 1 {
|
||||||
res.Err = errors[0]
|
res.err = errors[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
// WrapError sets the error from a base error with error arguments.
|
// SetError sets the error from a base error with error arguments.
|
||||||
func (res *Response) WrapError(baseError Error, arguments ...interface{}) {
|
func (res *Response) SetError(baseError Error, arguments ...interface{}) {
|
||||||
if len(arguments) > 0 {
|
if len(arguments) > 0 {
|
||||||
baseError.SetArguments(arguments[0], arguments[1:])
|
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
|
// SetData adds/overrides a new response field
|
||||||
|
@ -62,7 +67,7 @@ func (res *Response) MarshalJSON() ([]byte, error) {
|
||||||
fmt[k] = v
|
fmt[k] = v
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt["error"] = res.Err
|
fmt["error"] = res.err
|
||||||
|
|
||||||
return json.Marshal(fmt)
|
return json.Marshal(fmt)
|
||||||
}
|
}
|
||||||
|
|
|
@ -113,14 +113,14 @@ func (s *Server) ServeHTTP(res http.ResponseWriter, req *http.Request) {
|
||||||
if serviceHandler == nil {
|
if serviceHandler == nil {
|
||||||
if serviceFound {
|
if serviceFound {
|
||||||
apiResponse := api.NewResponse()
|
apiResponse := api.NewResponse()
|
||||||
apiResponse.WrapError(api.ErrorUncallableMethod(), servicePath, req.Method)
|
apiResponse.SetError(api.ErrorUncallableMethod(), servicePath, req.Method)
|
||||||
apiResponse.Write(res)
|
apiResponse.Write(res)
|
||||||
logError(apiResponse)
|
logError(apiResponse)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
apiResponse := api.NewResponse()
|
apiResponse := api.NewResponse()
|
||||||
apiResponse.WrapError(api.ErrorUncallableService(), servicePath)
|
apiResponse.SetError(api.ErrorUncallableService(), servicePath)
|
||||||
apiResponse.Write(res)
|
apiResponse.Write(res)
|
||||||
logError(apiResponse)
|
logError(apiResponse)
|
||||||
return
|
return
|
||||||
|
|
2
util.go
2
util.go
|
@ -79,5 +79,5 @@ func (s *Server) extractParameters(store *reqdata.Store, methodParam map[string]
|
||||||
|
|
||||||
// Prints an error as HTTP response
|
// Prints an error as HTTP response
|
||||||
func logError(res *api.Response) {
|
func logError(res *api.Response) {
|
||||||
log.Printf("[http.fail] %v\n", res.Err)
|
log.Printf("[http.fail] %v\n", res)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue