Skip to content

Commit 9d1f0c0

Browse files
author
Derek Dowling
committed
Adding error list support
1 parent a43bd0e commit 9d1f0c0

File tree

4 files changed

+43
-10
lines changed

4 files changed

+43
-10
lines changed

document.go

+11-7
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ of each attribute: http://jsonapi.org/format/#document-structure
1313
*/
1414
type Document struct {
1515
Data List `json:"data,omitempty"`
16-
Errors []*Error `json:"errors,omitempty"`
16+
Errors ErrorList `json:"errors,omitempty"`
1717
Links *Link `json:"links,omitempty"`
1818
Included []*Object `json:"included,omitempty"`
1919
Meta interface{} `json:"meta,omitempty"`
@@ -62,10 +62,16 @@ func Build(payload Sendable) *Document {
6262

6363
err, isError := payload.(*Error)
6464
if isError {
65-
document.Errors = []*Error{err}
65+
document.Errors = ErrorList{err}
6666
document.Status = err.Status
6767
}
6868

69+
errorList, isErrorList := payload.(ErrorList)
70+
if isErrorList {
71+
document.Errors = errorList
72+
document.Status = errorList[0].Status
73+
}
74+
6975
return document
7076
}
7177

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

106-
for _, docErr := range d.Errors {
107-
err := docErr.Validate(r, response)
108-
if err != nil {
109-
return err
110-
}
112+
err = d.Errors.Validate(r, response)
113+
if err != nil {
114+
return err
111115
}
112116

113117
return nil

error.go

+15
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,21 @@ var DefaultErrorDetail = "Request failed, something went wrong."
1616
// DefaultTitle can be customized to provide a more customized ISE Title
1717
var DefaultErrorTitle = "Internal Server Error"
1818

19+
// ErrorList is wraps an Error Array so that it can implement Sendable
20+
type ErrorList []*Error
21+
22+
// Validate checks all errors within the list to ensure that they are valid
23+
func (e ErrorList) Validate(r *http.Request, response bool) *Error {
24+
for _, err := range e {
25+
validationErr := err.Validate(r, response)
26+
if validationErr != nil {
27+
return validationErr
28+
}
29+
}
30+
31+
return nil
32+
}
33+
1934
/*
2035
Error consists of a number of contextual attributes to make conveying
2136
certain error type simpler as per the JSON API specification:

error_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,20 @@ func TestError(t *testing.T) {
7070
So(contentLength, ShouldBeGreaterThan, 0)
7171
So(writer.HeaderMap.Get("Content-Type"), ShouldEqual, ContentType)
7272
})
73+
74+
Convey("should work for an ErrorList", func() {
75+
76+
errorList := ErrorList{testError}
77+
78+
err := Send(writer, request, errorList)
79+
So(err, ShouldBeNil)
80+
So(writer.Code, ShouldEqual, http.StatusForbidden)
81+
82+
contentLength, convErr := strconv.Atoi(writer.HeaderMap.Get("Content-Length"))
83+
So(convErr, ShouldBeNil)
84+
So(contentLength, ShouldBeGreaterThan, 0)
85+
So(writer.HeaderMap.Get("Content-Type"), ShouldEqual, ContentType)
86+
})
7387
})
7488
})
7589
}

object.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ specifying each struct attribute that failed. In this case, all you need to do i
6363
jsh.Send(w, r, errors)
6464
}
6565
*/
66-
func (o *Object) Unmarshal(resourceType string, target interface{}) []*Error {
66+
func (o *Object) Unmarshal(resourceType string, target interface{}) ErrorList {
6767

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

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

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

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

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

186186
// parse out validation error

0 commit comments

Comments
 (0)