Fixes new errors API.

This commit is contained in:
Manu Mtz-Almeida 2015-05-22 03:43:39 +02:00
parent e94247f9ad
commit b7205a6ec2
3 changed files with 19 additions and 7 deletions

View File

@ -144,7 +144,7 @@ func (c *Context) IsAborted() bool {
// A middleware can be used to collect all the errors and push them to a database together, print a log, or append it in the HTTP response. // A middleware can be used to collect all the errors and push them to a database together, print a log, or append it in the HTTP response.
func (c *Context) Error(err error) *errorMsg { func (c *Context) Error(err error) *errorMsg {
newError := &errorMsg{ newError := &errorMsg{
Error: err, Err: err,
Flags: ErrorTypePrivate, Flags: ErrorTypePrivate,
} }
c.Errors = append(c.Errors, newError) c.Errors = append(c.Errors, newError)
@ -154,7 +154,7 @@ func (c *Context) Error(err error) *errorMsg {
func (c *Context) LastError() error { func (c *Context) LastError() error {
nuErrors := len(c.Errors) nuErrors := len(c.Errors)
if nuErrors > 0 { if nuErrors > 0 {
return c.Errors[nuErrors-1].Error return c.Errors[nuErrors-1].Err
} }
return nil return nil
} }

View File

@ -363,11 +363,11 @@ func TestContextError(t *testing.T) {
assert.Equal(t, c.Errors.String(), "Error #01: first error\n Meta: some data\n"+ assert.Equal(t, c.Errors.String(), "Error #01: first error\n Meta: some data\n"+
"Error #02: second error\n Meta: some data 2\n") "Error #02: second error\n Meta: some data 2\n")
assert.Equal(t, c.Errors[0].Error, errors.New("first error")) assert.Equal(t, c.Errors[0].Err, errors.New("first error"))
assert.Equal(t, c.Errors[0].Metadata, "some data") assert.Equal(t, c.Errors[0].Metadata, "some data")
assert.Equal(t, c.Errors[0].Flags, ErrorTypePrivate) assert.Equal(t, c.Errors[0].Flags, ErrorTypePrivate)
assert.Equal(t, c.Errors[1].Error, errors.New("second error")) assert.Equal(t, c.Errors[1].Err, errors.New("second error"))
assert.Equal(t, c.Errors[1].Metadata, "some data 2") assert.Equal(t, c.Errors[1].Metadata, "some data 2")
assert.Equal(t, c.Errors[1].Flags, ErrorTypePrivate) assert.Equal(t, c.Errors[1].Flags, ErrorTypePrivate)
} }

View File

@ -21,7 +21,7 @@ const (
// Used internally to collect errors that occurred during an http request. // Used internally to collect errors that occurred during an http request.
type errorMsg struct { type errorMsg struct {
Error error `json:"error"` Err error `json:"error"`
Flags int `json:"-"` Flags int `json:"-"`
Metadata interface{} `json:"meta"` Metadata interface{} `json:"meta"`
} }
@ -36,6 +36,10 @@ func (msg *errorMsg) Meta(data interface{}) *errorMsg {
return msg return msg
} }
func (msg *errorMsg) Error() string {
return msg.Err.Error()
}
type errorMsgs []*errorMsg type errorMsgs []*errorMsg
func (a errorMsgs) ByType(typ int) errorMsgs { func (a errorMsgs) ByType(typ int) errorMsgs {
@ -51,13 +55,21 @@ func (a errorMsgs) ByType(typ int) errorMsgs {
return result return result
} }
func (a errorMsgs) Last() *errorMsg {
length := len(a)
if length == 0 {
return nil
}
return a[length-1]
}
func (a errorMsgs) Errors() []string { func (a errorMsgs) Errors() []string {
if len(a) == 0 { if len(a) == 0 {
return []string{} return []string{}
} }
errorStrings := make([]string, len(a)) errorStrings := make([]string, len(a))
for i, err := range a { for i, err := range a {
errorStrings[i] = err.Error.Error() errorStrings[i] = err.Error()
} }
return errorStrings return errorStrings
} }
@ -68,7 +80,7 @@ func (a errorMsgs) String() string {
} }
var buffer bytes.Buffer var buffer bytes.Buffer
for i, msg := range a { for i, msg := range a {
fmt.Fprintf(&buffer, "Error #%02d: %s\n Meta: %v\n", (i + 1), msg.Error, msg.Metadata) fmt.Fprintf(&buffer, "Error #%02d: %s\n Meta: %v\n", (i + 1), msg.Err, msg.Metadata)
} }
return buffer.String() return buffer.String()
} }