2015-05-15 01:21:39 +00:00
|
|
|
package errcode
|
|
|
|
|
|
|
|
import (
|
2015-06-03 13:52:39 +00:00
|
|
|
"encoding/json"
|
2015-05-15 01:21:39 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2015-06-03 13:52:39 +00:00
|
|
|
// ErrorCoder is the base interface for ErrorCode and Error allowing
|
|
|
|
// users of each to just call ErrorCode to get the real ID of each
|
|
|
|
type ErrorCoder interface {
|
|
|
|
ErrorCode() ErrorCode
|
|
|
|
}
|
|
|
|
|
2015-05-15 01:21:39 +00:00
|
|
|
// ErrorCode represents the error type. The errors are serialized via strings
|
|
|
|
// and the integer format may change and should *never* be exported.
|
|
|
|
type ErrorCode int
|
|
|
|
|
2015-07-16 18:35:02 +00:00
|
|
|
var _ error = ErrorCode(0)
|
|
|
|
|
2015-06-03 13:52:39 +00:00
|
|
|
// ErrorCode just returns itself
|
|
|
|
func (ec ErrorCode) ErrorCode() ErrorCode {
|
|
|
|
return ec
|
2015-05-15 01:21:39 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 13:52:39 +00:00
|
|
|
// Error returns the ID/Value
|
|
|
|
func (ec ErrorCode) Error() string {
|
2015-08-21 00:36:24 +00:00
|
|
|
// NOTE(stevvooe): Cannot use message here since it may have unpopulated args.
|
|
|
|
return strings.ToLower(strings.Replace(ec.String(), "_", " ", -1))
|
2015-05-15 01:21:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Descriptor returns the descriptor for the error code.
|
|
|
|
func (ec ErrorCode) Descriptor() ErrorDescriptor {
|
|
|
|
d, ok := errorCodeToDescriptors[ec]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return ErrorCodeUnknown.Descriptor()
|
|
|
|
}
|
|
|
|
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns the canonical identifier for this error code.
|
|
|
|
func (ec ErrorCode) String() string {
|
|
|
|
return ec.Descriptor().Value
|
|
|
|
}
|
|
|
|
|
|
|
|
// Message returned the human-readable error message for this error code.
|
|
|
|
func (ec ErrorCode) Message() string {
|
|
|
|
return ec.Descriptor().Message
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalText encodes the receiver into UTF-8-encoded text and returns the
|
|
|
|
// result.
|
|
|
|
func (ec ErrorCode) MarshalText() (text []byte, err error) {
|
|
|
|
return []byte(ec.String()), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalText decodes the form generated by MarshalText.
|
|
|
|
func (ec *ErrorCode) UnmarshalText(text []byte) error {
|
|
|
|
desc, ok := idToDescriptors[string(text)]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
desc = ErrorCodeUnknown.Descriptor()
|
|
|
|
}
|
|
|
|
|
|
|
|
*ec = desc.Code
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-20 22:45:08 +00:00
|
|
|
// WithMessage creates a new Error struct based on the passed-in info and
|
|
|
|
// overrides the Message property.
|
|
|
|
func (ec ErrorCode) WithMessage(message string) Error {
|
|
|
|
return Error{
|
|
|
|
Code: ec,
|
|
|
|
Message: message,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-03 13:52:39 +00:00
|
|
|
// WithDetail creates a new Error struct based on the passed-in info and
|
|
|
|
// set the Detail property appropriately
|
|
|
|
func (ec ErrorCode) WithDetail(detail interface{}) Error {
|
2015-06-18 00:39:27 +00:00
|
|
|
return Error{
|
|
|
|
Code: ec,
|
|
|
|
Message: ec.Message(),
|
|
|
|
}.WithDetail(detail)
|
|
|
|
}
|
2015-06-03 13:52:39 +00:00
|
|
|
|
2015-06-18 00:39:27 +00:00
|
|
|
// WithArgs creates a new Error struct and sets the Args slice
|
|
|
|
func (ec ErrorCode) WithArgs(args ...interface{}) Error {
|
2015-06-03 13:52:39 +00:00
|
|
|
return Error{
|
2015-06-18 00:39:27 +00:00
|
|
|
Code: ec,
|
|
|
|
Message: ec.Message(),
|
|
|
|
}.WithArgs(args...)
|
2015-06-03 13:52:39 +00:00
|
|
|
}
|
|
|
|
|
2015-05-15 01:21:39 +00:00
|
|
|
// Error provides a wrapper around ErrorCode with extra Details provided.
|
|
|
|
type Error struct {
|
2015-06-18 00:39:27 +00:00
|
|
|
Code ErrorCode `json:"code"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
Detail interface{} `json:"detail,omitempty"`
|
|
|
|
|
|
|
|
// TODO(duglin): See if we need an "args" property so we can do the
|
|
|
|
// variable substitution right before showing the message to the user
|
2015-05-15 01:21:39 +00:00
|
|
|
}
|
|
|
|
|
2015-07-16 18:35:02 +00:00
|
|
|
var _ error = Error{}
|
|
|
|
|
2015-06-03 13:52:39 +00:00
|
|
|
// ErrorCode returns the ID/Value of this Error
|
|
|
|
func (e Error) ErrorCode() ErrorCode {
|
|
|
|
return e.Code
|
|
|
|
}
|
|
|
|
|
2015-05-15 01:21:39 +00:00
|
|
|
// Error returns a human readable representation of the error.
|
|
|
|
func (e Error) Error() string {
|
2015-08-21 00:36:24 +00:00
|
|
|
return fmt.Sprintf("%s: %s", e.Code.Error(), e.Message)
|
2015-05-27 00:18:32 +00:00
|
|
|
}
|
|
|
|
|
2015-06-18 00:39:27 +00:00
|
|
|
// WithDetail will return a new Error, based on the current one, but with
|
|
|
|
// some Detail info added
|
|
|
|
func (e Error) WithDetail(detail interface{}) Error {
|
|
|
|
return Error{
|
|
|
|
Code: e.Code,
|
|
|
|
Message: e.Message,
|
|
|
|
Detail: detail,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithArgs uses the passed-in list of interface{} as the substitution
|
|
|
|
// variables in the Error's Message string, but returns a new Error
|
|
|
|
func (e Error) WithArgs(args ...interface{}) Error {
|
|
|
|
return Error{
|
|
|
|
Code: e.Code,
|
|
|
|
Message: fmt.Sprintf(e.Code.Message(), args...),
|
|
|
|
Detail: e.Detail,
|
|
|
|
}
|
2015-05-15 01:21:39 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 13:52:39 +00:00
|
|
|
// ErrorDescriptor provides relevant information about a given error code.
|
|
|
|
type ErrorDescriptor struct {
|
|
|
|
// Code is the error code that this descriptor describes.
|
|
|
|
Code ErrorCode
|
2015-05-15 01:21:39 +00:00
|
|
|
|
2015-06-03 13:52:39 +00:00
|
|
|
// Value provides a unique, string key, often captilized with
|
|
|
|
// underscores, to identify the error code. This value is used as the
|
|
|
|
// keyed value when serializing api errors.
|
|
|
|
Value string
|
2015-05-15 01:21:39 +00:00
|
|
|
|
2020-11-29 11:14:24 +00:00
|
|
|
// Message is a short, human readable description of the error condition
|
2015-06-03 13:52:39 +00:00
|
|
|
// included in API responses.
|
|
|
|
Message string
|
2015-05-15 01:21:39 +00:00
|
|
|
|
2015-06-03 13:52:39 +00:00
|
|
|
// Description provides a complete account of the errors purpose, suitable
|
|
|
|
// for use in documentation.
|
|
|
|
Description string
|
2015-05-15 01:21:39 +00:00
|
|
|
|
2015-06-03 13:52:39 +00:00
|
|
|
// HTTPStatusCode provides the http status code that is associated with
|
|
|
|
// this error condition.
|
|
|
|
HTTPStatusCode int
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseErrorCode returns the value by the string error code.
|
|
|
|
// `ErrorCodeUnknown` will be returned if the error is not known.
|
|
|
|
func ParseErrorCode(value string) ErrorCode {
|
|
|
|
ed, ok := idToDescriptors[value]
|
|
|
|
if ok {
|
|
|
|
return ed.Code
|
2015-05-15 01:21:39 +00:00
|
|
|
}
|
2015-06-03 13:52:39 +00:00
|
|
|
|
|
|
|
return ErrorCodeUnknown
|
2015-05-15 01:21:39 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 13:52:39 +00:00
|
|
|
// Errors provides the envelope for multiple errors and a few sugar methods
|
|
|
|
// for use within the application.
|
|
|
|
type Errors []error
|
|
|
|
|
2015-07-16 18:35:02 +00:00
|
|
|
var _ error = Errors{}
|
|
|
|
|
2015-05-27 00:18:32 +00:00
|
|
|
func (errs Errors) Error() string {
|
|
|
|
switch len(errs) {
|
2015-05-15 01:21:39 +00:00
|
|
|
case 0:
|
|
|
|
return "<nil>"
|
|
|
|
case 1:
|
2015-05-27 00:18:32 +00:00
|
|
|
return errs[0].Error()
|
2015-05-15 01:21:39 +00:00
|
|
|
default:
|
|
|
|
msg := "errors:\n"
|
2015-05-27 00:18:32 +00:00
|
|
|
for _, err := range errs {
|
2015-05-15 01:21:39 +00:00
|
|
|
msg += err.Error() + "\n"
|
|
|
|
}
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Len returns the current number of errors.
|
2015-05-27 00:18:32 +00:00
|
|
|
func (errs Errors) Len() int {
|
|
|
|
return len(errs)
|
2015-05-15 01:21:39 +00:00
|
|
|
}
|
2015-06-03 13:52:39 +00:00
|
|
|
|
|
|
|
// MarshalJSON converts slice of error, ErrorCode or Error into a
|
|
|
|
// slice of Error - then serializes
|
|
|
|
func (errs Errors) MarshalJSON() ([]byte, error) {
|
2015-06-19 01:00:26 +00:00
|
|
|
var tmpErrs struct {
|
2015-06-18 00:39:27 +00:00
|
|
|
Errors []Error `json:"errors,omitempty"`
|
2015-06-19 01:00:26 +00:00
|
|
|
}
|
2015-06-03 13:52:39 +00:00
|
|
|
|
|
|
|
for _, daErr := range errs {
|
|
|
|
var err Error
|
|
|
|
|
2019-02-05 00:01:04 +00:00
|
|
|
switch daErr := daErr.(type) {
|
2015-06-03 13:52:39 +00:00
|
|
|
case ErrorCode:
|
2019-02-05 00:01:04 +00:00
|
|
|
err = daErr.WithDetail(nil)
|
2015-06-03 13:52:39 +00:00
|
|
|
case Error:
|
2019-02-05 00:01:04 +00:00
|
|
|
err = daErr
|
2015-06-03 13:52:39 +00:00
|
|
|
default:
|
|
|
|
err = ErrorCodeUnknown.WithDetail(daErr)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-06-18 00:39:27 +00:00
|
|
|
// If the Error struct was setup and they forgot to set the
|
|
|
|
// Message field (meaning its "") then grab it from the ErrCode
|
|
|
|
msg := err.Message
|
|
|
|
if msg == "" {
|
|
|
|
msg = err.Code.Message()
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpErrs.Errors = append(tmpErrs.Errors, Error{
|
2015-06-03 13:52:39 +00:00
|
|
|
Code: err.Code,
|
2015-06-18 00:39:27 +00:00
|
|
|
Message: msg,
|
2015-06-03 13:52:39 +00:00
|
|
|
Detail: err.Detail,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.Marshal(tmpErrs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON deserializes []Error and then converts it into slice of
|
|
|
|
// Error or ErrorCode
|
|
|
|
func (errs *Errors) UnmarshalJSON(data []byte) error {
|
2015-06-19 01:00:26 +00:00
|
|
|
var tmpErrs struct {
|
2015-06-18 00:39:27 +00:00
|
|
|
Errors []Error
|
2015-06-19 01:00:26 +00:00
|
|
|
}
|
2015-06-03 13:52:39 +00:00
|
|
|
|
|
|
|
if err := json.Unmarshal(data, &tmpErrs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var newErrs Errors
|
2015-06-19 01:00:26 +00:00
|
|
|
for _, daErr := range tmpErrs.Errors {
|
2015-06-18 00:39:27 +00:00
|
|
|
// If Message is empty or exactly matches the Code's message string
|
|
|
|
// then just use the Code, no need for a full Error struct
|
|
|
|
if daErr.Detail == nil && (daErr.Message == "" || daErr.Message == daErr.Code.Message()) {
|
2015-06-03 13:52:39 +00:00
|
|
|
// Error's w/o details get converted to ErrorCode
|
|
|
|
newErrs = append(newErrs, daErr.Code)
|
|
|
|
} else {
|
|
|
|
// Error's w/ details are untouched
|
|
|
|
newErrs = append(newErrs, Error{
|
2015-06-18 00:39:27 +00:00
|
|
|
Code: daErr.Code,
|
|
|
|
Message: daErr.Message,
|
|
|
|
Detail: daErr.Detail,
|
2015-06-03 13:52:39 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*errs = newErrs
|
|
|
|
return nil
|
|
|
|
}
|