Skip to content

Commit

Permalink
Adding error list support
Browse files Browse the repository at this point in the history
  • Loading branch information
Derek Dowling committed Jan 5, 2016
1 parent a43bd0e commit 9d1f0c0
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
18 changes: 11 additions & 7 deletions document.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ of each attribute: http://jsonapi.org/format/#document-structure
*/
type Document struct {
Data List `json:"data,omitempty"`
Errors []*Error `json:"errors,omitempty"`
Errors ErrorList `json:"errors,omitempty"`
Links *Link `json:"links,omitempty"`
Included []*Object `json:"included,omitempty"`
Meta interface{} `json:"meta,omitempty"`
Expand Down Expand Up @@ -62,10 +62,16 @@ func Build(payload Sendable) *Document {

err, isError := payload.(*Error)
if isError {
document.Errors = []*Error{err}
document.Errors = ErrorList{err}
document.Status = err.Status
}

errorList, isErrorList := payload.(ErrorList)
if isErrorList {
document.Errors = errorList
document.Status = errorList[0].Status
}

return document
}

Expand Down Expand Up @@ -103,11 +109,9 @@ func (d *Document) Validate(r *http.Request, response bool) *Error {
return err
}

for _, docErr := range d.Errors {
err := docErr.Validate(r, response)
if err != nil {
return err
}
err = d.Errors.Validate(r, response)
if err != nil {
return err
}

return nil
Expand Down
15 changes: 15 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ var DefaultErrorDetail = "Request failed, something went wrong."
// DefaultTitle can be customized to provide a more customized ISE Title
var DefaultErrorTitle = "Internal Server Error"

// ErrorList is wraps an Error Array so that it can implement Sendable
type ErrorList []*Error

// Validate checks all errors within the list to ensure that they are valid
func (e ErrorList) Validate(r *http.Request, response bool) *Error {
for _, err := range e {
validationErr := err.Validate(r, response)
if validationErr != nil {
return validationErr
}
}

return nil
}

/*
Error consists of a number of contextual attributes to make conveying
certain error type simpler as per the JSON API specification:
Expand Down
14 changes: 14 additions & 0 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,20 @@ func TestError(t *testing.T) {
So(contentLength, ShouldBeGreaterThan, 0)
So(writer.HeaderMap.Get("Content-Type"), ShouldEqual, ContentType)
})

Convey("should work for an ErrorList", func() {

errorList := ErrorList{testError}

err := Send(writer, request, errorList)
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)
})
})
})
}
6 changes: 3 additions & 3 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ specifying each struct attribute that failed. In this case, all you need to do i
jsh.Send(w, r, errors)
}
*/
func (o *Object) Unmarshal(resourceType string, target interface{}) []*Error {
func (o *Object) Unmarshal(resourceType string, target interface{}) ErrorList {

if resourceType != o.Type {
return []*Error{ISE(fmt.Sprintf(
Expand Down Expand Up @@ -172,15 +172,15 @@ func (o *Object) String() string {

// validateInput runs go-validator on each attribute on the struct and returns all
// errors that it picks up
func validateInput(target interface{}) []*Error {
func validateInput(target interface{}) ErrorList {

_, validationError := govalidator.ValidateStruct(target)
if validationError != nil {

errorList, isType := validationError.(govalidator.Errors)
if isType {

errors := []*Error{}
errors := ErrorList{}
for _, singleErr := range errorList.Errors() {

// parse out validation error
Expand Down

0 comments on commit 9d1f0c0

Please sign in to comment.