Skip to content

Commit a077109

Browse files
author
Derek Dowling
committed
Enhanced Error Handling
Provides a StatusCode() call to the generic ErrorType. Abstracts away the need for an end user to require type checking in order to determine the error status code associated with the type. Adds more documentation around errors, and adds an additional Validation check to provide a specific error for a non-set error status.
1 parent 068d95f commit a077109

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

error.go

+36-6
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,19 @@ 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-
// ErrorType represents the common interface requirements that libraries may
20-
// specify if they would like to accept either a single error or a list
19+
/*
20+
ErrorType represents the common interface requirements that libraries may
21+
specify if they would like to accept either a single error or a list.
22+
*/
2123
type ErrorType interface {
24+
// Error returns a formatted error and allows it to conform to the stdErr
25+
// interface.
2226
Error() string
27+
// Validate checks that the error is valid in the context of JSONAPI
2328
Validate(r *http.Request, response bool) *Error
29+
// StatusCode returns the first encountered HTTP Status Code for the error type.
30+
// Returns 0 if none is set.
31+
StatusCode() int
2432
}
2533

2634
// ErrorList is wraps an Error Array so that it can implement Sendable
@@ -49,6 +57,18 @@ func (e ErrorList) Error() string {
4957
return msg
5058
}
5159

60+
/*
61+
StatusCode (HTTP) of the first error in the list. Defaults to 0 if the list is
62+
empty or one has not yet been set for the first error.
63+
*/
64+
func (e ErrorList) StatusCode() int {
65+
if len(e) == 0 {
66+
return 0
67+
}
68+
69+
return e[0].Status
70+
}
71+
5272
/*
5373
Error consists of a number of contextual attributes to make conveying
5474
certain error type simpler as per the JSON API specification:
@@ -95,15 +115,25 @@ Validate ensures that the an error meets all JSON API criteria.
95115
*/
96116
func (e *Error) Validate(r *http.Request, response bool) *Error {
97117

98-
if e.Status < 400 || e.Status > 600 {
99-
return ISE(fmt.Sprintf("Invalid HTTP Status for error %+v\n", e))
100-
} else if e.Status == 422 && e.Source.Pointer == "" {
101-
return ISE(fmt.Sprintf("Source Pointer must be set for 422 Status errors"))
118+
switch {
119+
case e.Status == 0:
120+
return ISE(fmt.Sprintf("No HTTP Status set for error %+v\n", e))
121+
case e.Status < 400 || e.Status > 600:
122+
return ISE(fmt.Sprintf("HTTP Status out of valid range for error %+v\n", e))
123+
case e.Status == 422 && e.Source.Pointer == "":
124+
return ISE(fmt.Sprintf("Source Pointer must be set for 422 Status error"))
102125
}
103126

104127
return nil
105128
}
106129

130+
/*
131+
StatusCode (HTTP) for the error. Defaults to 0.
132+
*/
133+
func (e *Error) StatusCode() int {
134+
return e.Status
135+
}
136+
107137
/*
108138
ISE is a convenience function for creating a ready-to-go Internal Service Error
109139
response. The message you pass in is set to the ErrorObject.ISE attribute so you

0 commit comments

Comments
 (0)