From b7205a6ec22beac117291039182844d642eb4738 Mon Sep 17 00:00:00 2001 From: Manu Mtz-Almeida Date: Fri, 22 May 2015 03:43:39 +0200 Subject: [PATCH] Fixes new errors API. --- context.go | 4 ++-- context_test.go | 4 ++-- errors.go | 18 +++++++++++++++--- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/context.go b/context.go index c78e8c3..9716c67 100644 --- a/context.go +++ b/context.go @@ -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. func (c *Context) Error(err error) *errorMsg { newError := &errorMsg{ - Error: err, + Err: err, Flags: ErrorTypePrivate, } c.Errors = append(c.Errors, newError) @@ -154,7 +154,7 @@ func (c *Context) Error(err error) *errorMsg { func (c *Context) LastError() error { nuErrors := len(c.Errors) if nuErrors > 0 { - return c.Errors[nuErrors-1].Error + return c.Errors[nuErrors-1].Err } return nil } diff --git a/context_test.go b/context_test.go index cdf549f..17c123e 100644 --- a/context_test.go +++ b/context_test.go @@ -363,11 +363,11 @@ func TestContextError(t *testing.T) { 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") - 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].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].Flags, ErrorTypePrivate) } diff --git a/errors.go b/errors.go index e26d50f..4aab56d 100644 --- a/errors.go +++ b/errors.go @@ -21,7 +21,7 @@ const ( // Used internally to collect errors that occurred during an http request. type errorMsg struct { - Error error `json:"error"` + Err error `json:"error"` Flags int `json:"-"` Metadata interface{} `json:"meta"` } @@ -36,6 +36,10 @@ func (msg *errorMsg) Meta(data interface{}) *errorMsg { return msg } +func (msg *errorMsg) Error() string { + return msg.Err.Error() +} + type errorMsgs []*errorMsg func (a errorMsgs) ByType(typ int) errorMsgs { @@ -51,13 +55,21 @@ func (a errorMsgs) ByType(typ int) errorMsgs { return result } +func (a errorMsgs) Last() *errorMsg { + length := len(a) + if length == 0 { + return nil + } + return a[length-1] +} + func (a errorMsgs) Errors() []string { if len(a) == 0 { return []string{} } errorStrings := make([]string, len(a)) for i, err := range a { - errorStrings[i] = err.Error.Error() + errorStrings[i] = err.Error() } return errorStrings } @@ -68,7 +80,7 @@ func (a errorMsgs) String() string { } var buffer bytes.Buffer 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() }