@@ -16,11 +16,19 @@ var DefaultErrorDetail = "Request failed, something went wrong."
16
16
// DefaultTitle can be customized to provide a more customized ISE Title
17
17
var DefaultErrorTitle = "Internal Server Error"
18
18
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
+ */
21
23
type ErrorType interface {
24
+ // Error returns a formatted error and allows it to conform to the stdErr
25
+ // interface.
22
26
Error () string
27
+ // Validate checks that the error is valid in the context of JSONAPI
23
28
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
24
32
}
25
33
26
34
// ErrorList is wraps an Error Array so that it can implement Sendable
@@ -49,6 +57,18 @@ func (e ErrorList) Error() string {
49
57
return msg
50
58
}
51
59
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
+
52
72
/*
53
73
Error consists of a number of contextual attributes to make conveying
54
74
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.
95
115
*/
96
116
func (e * Error ) Validate (r * http.Request , response bool ) * Error {
97
117
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" ))
102
125
}
103
126
104
127
return nil
105
128
}
106
129
130
+ /*
131
+ StatusCode (HTTP) for the error. Defaults to 0.
132
+ */
133
+ func (e * Error ) StatusCode () int {
134
+ return e .Status
135
+ }
136
+
107
137
/*
108
138
ISE is a convenience function for creating a ready-to-go Internal Service Error
109
139
response. The message you pass in is set to the ErrorObject.ISE attribute so you
0 commit comments