Skip to content

Commit

Permalink
Adding error type support
Browse files Browse the repository at this point in the history
  • Loading branch information
Derek Dowling committed Jan 6, 2016
1 parent a7a750c commit 69ccb17
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ var DefaultErrorDetail = "Request failed, something went wrong."
// DefaultTitle can be customized to provide a more customized ISE Title
var DefaultErrorTitle = "Internal Server Error"

// ErrorType represents the common interface requirements that libraries may
// specify if they would like to accept either a single error or a list
type ErrorType interface {
Error() string
Validate(r *http.Request, response bool) *Error
}

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

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

// Fulfills the default error interface
func (e ErrorList) Error() string {
var msg string

for _, err := range e {
msg += fmt.Sprintf("%s\n", err.Error())
}

return msg
}

/*
Error consists of a number of contextual attributes to make conveying
certain error type simpler as per the JSON API specification:
Expand Down Expand Up @@ -60,21 +78,13 @@ format if not. As usual, err.Error() should not be considered safe for presentat
to the end user, use err.SafeError() instead.
*/
func (e *Error) Error() string {
if e.ISE != "" {
return fmt.Sprintf("HTTP %d - %s", e.Status, e.ISE)
msg := fmt.Sprintf("%d: %s - %s", e.Status, e.Title, e.Detail)
if e.Source.Pointer != "" {
msg += fmt.Sprintf("(Source.Pointer: %s)", e.Source.Pointer)
}

return e.SafeError()
}

/*
SafeError is a formatted error string that does not include an associated internal
server error such that it is safe to return this error message to an end user.
*/
func (e *Error) SafeError() string {
msg := fmt.Sprintf("HTTP %d: %s - %s", e.Status, e.Title, e.Detail)
if e.Source.Pointer != "" {
msg += fmt.Sprintf("Source.Pointer: %s", e.Source.Pointer)
if e.ISE != "" {
msg += fmt.Sprintf("\nInternal Error: %s", e.ISE)
}

return msg
Expand Down

0 comments on commit 69ccb17

Please sign in to comment.