From a077109d98f6d6223555c633af4b04f9b7a3e3ec Mon Sep 17 00:00:00 2001 From: Derek Dowling Date: Thu, 3 Mar 2016 12:02:34 -0800 Subject: [PATCH] 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. --- error.go | 42 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/error.go b/error.go index ccab888..25baadc 100644 --- a/error.go +++ b/error.go @@ -16,11 +16,19 @@ 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 +/* +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 returns a formatted error and allows it to conform to the stdErr + // interface. Error() string + // Validate checks that the error is valid in the context of JSONAPI Validate(r *http.Request, response bool) *Error + // StatusCode returns the first encountered HTTP Status Code for the error type. + // Returns 0 if none is set. + StatusCode() int } // ErrorList is wraps an Error Array so that it can implement Sendable @@ -49,6 +57,18 @@ func (e ErrorList) Error() string { return msg } +/* +StatusCode (HTTP) of the first error in the list. Defaults to 0 if the list is +empty or one has not yet been set for the first error. +*/ +func (e ErrorList) StatusCode() int { + if len(e) == 0 { + return 0 + } + + return e[0].Status +} + /* Error consists of a number of contextual attributes to make conveying 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. */ func (e *Error) Validate(r *http.Request, response bool) *Error { - if e.Status < 400 || e.Status > 600 { - return ISE(fmt.Sprintf("Invalid HTTP Status for error %+v\n", e)) - } else if e.Status == 422 && e.Source.Pointer == "" { - return ISE(fmt.Sprintf("Source Pointer must be set for 422 Status errors")) + switch { + case e.Status == 0: + return ISE(fmt.Sprintf("No HTTP Status set for error %+v\n", e)) + case e.Status < 400 || e.Status > 600: + return ISE(fmt.Sprintf("HTTP Status out of valid range for error %+v\n", e)) + case e.Status == 422 && e.Source.Pointer == "": + return ISE(fmt.Sprintf("Source Pointer must be set for 422 Status error")) } return nil } +/* +StatusCode (HTTP) for the error. Defaults to 0. +*/ +func (e *Error) StatusCode() int { + return e.Status +} + /* ISE is a convenience function for creating a ready-to-go Internal Service Error response. The message you pass in is set to the ErrorObject.ISE attribute so you