Skip to content

Commit

Permalink
Convenience Status() func for error object
Browse files Browse the repository at this point in the history
  • Loading branch information
Derek Dowling committed Dec 18, 2015
1 parent ea63c83 commit 46894e8
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 31 deletions.
9 changes: 9 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ func (e *Error) Error() string {
return err
}

// Status returns the HTTP Code of the first Error Object, or 0 if none
func (e *Error) Status() int {
if len(e.Objects) > 0 {
return e.Objects[0].Status
}

return 0
}

// Internal prints a formatted error list including ISE's, useful for debugging
func (e *Error) Internal() string {
err := "Errors:"
Expand Down
83 changes: 52 additions & 31 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,48 +52,69 @@ func TestError(t *testing.T) {
})
})

Convey("Error List Tests", func() {
Convey("->Status()", func() {
err := &Error{}

Convey("->Add()", func() {
Convey("should return 0 if no error objects are present", func() {
code := err.Status()
So(code, ShouldEqual, 0)
})

testError := &Error{}
Convey("should return the first code if error has objects", func() {
addErr := err.Add(&ErrorObject{
Status: 400,
})
So(addErr, ShouldBeNil)

Convey("should successfully add a valid error", func() {
err := testError.Add(testErrorObject)
So(err, ShouldBeNil)
So(len(testError.Objects), ShouldEqual, 1)
addErr = err.Add(&ErrorObject{
Status: 500,
})
So(addErr, ShouldBeNil)

Convey("should error if validation fails while adding an error", func() {
badError := &ErrorObject{
Title: "Invalid",
Detail: "So badly",
}
code := err.Status()
So(code, ShouldEqual, 400)
})
})

err := testError.Add(badError)
So(err.Objects[0].Status, ShouldEqual, 500)
So(testError.Objects, ShouldBeEmpty)
})
Convey("->Add()", func() {

testError := &Error{}

Convey("should successfully add a valid error", func() {
err := testError.Add(testErrorObject)
So(err, ShouldBeNil)
So(len(testError.Objects), ShouldEqual, 1)
})

Convey("->Send()", func() {
Convey("should error if validation fails while adding an error", func() {
badError := &ErrorObject{
Title: "Invalid",
Detail: "So badly",
}

testError := NewError(&ErrorObject{
Status: http.StatusForbidden,
Title: "Forbidden",
Detail: "Can't Go Here",
})
err := testError.Add(badError)
So(err.Objects[0].Status, ShouldEqual, 500)
So(testError.Objects, ShouldBeEmpty)
})
})

Convey("should send a properly formatted JSON error list", func() {
err := Send(writer, request, testError)
So(err, ShouldBeNil)
So(writer.Code, ShouldEqual, http.StatusForbidden)
Convey("->Send()", func() {

contentLength, convErr := strconv.Atoi(writer.HeaderMap.Get("Content-Length"))
So(convErr, ShouldBeNil)
So(contentLength, ShouldBeGreaterThan, 0)
So(writer.HeaderMap.Get("Content-Type"), ShouldEqual, ContentType)
})
testError := NewError(&ErrorObject{
Status: http.StatusForbidden,
Title: "Forbidden",
Detail: "Can't Go Here",
})

Convey("should send a properly formatted JSON error list", func() {
err := Send(writer, request, testError)
So(err, ShouldBeNil)
So(writer.Code, ShouldEqual, http.StatusForbidden)

contentLength, convErr := strconv.Atoi(writer.HeaderMap.Get("Content-Length"))
So(convErr, ShouldBeNil)
So(contentLength, ShouldBeGreaterThan, 0)
So(writer.HeaderMap.Get("Content-Type"), ShouldEqual, ContentType)
})
})
})
Expand Down

0 comments on commit 46894e8

Please sign in to comment.