Skip to content

Commit 69ccb17

Browse files
author
Derek Dowling
committed
Adding error type support
1 parent a7a750c commit 69ccb17

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

error.go

+23-13
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ 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
21+
type ErrorType interface {
22+
Error() string
23+
Validate(r *http.Request, response bool) *Error
24+
}
25+
1926
// ErrorList is wraps an Error Array so that it can implement Sendable
2027
type ErrorList []*Error
2128

@@ -31,6 +38,17 @@ func (e ErrorList) Validate(r *http.Request, response bool) *Error {
3138
return nil
3239
}
3340

41+
// Fulfills the default error interface
42+
func (e ErrorList) Error() string {
43+
var msg string
44+
45+
for _, err := range e {
46+
msg += fmt.Sprintf("%s\n", err.Error())
47+
}
48+
49+
return msg
50+
}
51+
3452
/*
3553
Error consists of a number of contextual attributes to make conveying
3654
certain error type simpler as per the JSON API specification:
@@ -60,21 +78,13 @@ format if not. As usual, err.Error() should not be considered safe for presentat
6078
to the end user, use err.SafeError() instead.
6179
*/
6280
func (e *Error) Error() string {
63-
if e.ISE != "" {
64-
return fmt.Sprintf("HTTP %d - %s", e.Status, e.ISE)
81+
msg := fmt.Sprintf("%d: %s - %s", e.Status, e.Title, e.Detail)
82+
if e.Source.Pointer != "" {
83+
msg += fmt.Sprintf("(Source.Pointer: %s)", e.Source.Pointer)
6584
}
6685

67-
return e.SafeError()
68-
}
69-
70-
/*
71-
SafeError is a formatted error string that does not include an associated internal
72-
server error such that it is safe to return this error message to an end user.
73-
*/
74-
func (e *Error) SafeError() string {
75-
msg := fmt.Sprintf("HTTP %d: %s - %s", e.Status, e.Title, e.Detail)
76-
if e.Source.Pointer != "" {
77-
msg += fmt.Sprintf("Source.Pointer: %s", e.Source.Pointer)
86+
if e.ISE != "" {
87+
msg += fmt.Sprintf("\nInternal Error: %s", e.ISE)
7888
}
7989

8090
return msg

0 commit comments

Comments
 (0)