2015-05-15 03:21:39 +02:00
|
|
|
package errcode
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
2015-05-27 03:16:45 +02:00
|
|
|
"sync"
|
2015-05-15 03:21:39 +02: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
|
|
|
|
|
|
|
|
// ErrorDescriptor provides relevant information about a given error code.
|
|
|
|
type ErrorDescriptor struct {
|
|
|
|
// Code is the error code that this descriptor describes.
|
|
|
|
Code ErrorCode
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
// Message is a short, human readable decription of the error condition
|
|
|
|
// included in API responses.
|
|
|
|
Message string
|
|
|
|
|
|
|
|
// Description provides a complete account of the errors purpose, suitable
|
|
|
|
// for use in documentation.
|
|
|
|
Description string
|
|
|
|
|
|
|
|
// HTTPStatusCode provides the http status code that is associated with
|
|
|
|
// this error condition.
|
|
|
|
HTTPStatusCode int
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
errorCodeToDescriptors = map[ErrorCode]ErrorDescriptor{}
|
|
|
|
idToDescriptors = map[string]ErrorDescriptor{}
|
2015-05-27 03:16:45 +02:00
|
|
|
groupToDescriptors = map[string][]ErrorDescriptor{}
|
2015-05-15 03:21:39 +02:00
|
|
|
)
|
|
|
|
|
2015-05-27 03:16:45 +02:00
|
|
|
// ErrorCodeUnknown is a generic error that can be used as a last
|
|
|
|
// resort if there is no situation-specific error message that can be used
|
|
|
|
var ErrorCodeUnknown = Register("registry.api.errcode", ErrorDescriptor{
|
|
|
|
Value: "UNKNOWN",
|
|
|
|
Message: "unknown error",
|
|
|
|
Description: `Generic error returned when the error does not have an
|
2015-05-15 03:21:39 +02:00
|
|
|
API classification.`,
|
2015-05-27 03:16:45 +02:00
|
|
|
HTTPStatusCode: http.StatusInternalServerError,
|
|
|
|
})
|
2015-05-15 03:21:39 +02:00
|
|
|
|
2015-05-27 03:16:45 +02:00
|
|
|
var nextCode = 1000
|
|
|
|
var registerLock sync.Mutex
|
|
|
|
|
|
|
|
// Register will make the passed-in error known to the environment and
|
|
|
|
// return a new ErrorCode
|
|
|
|
func Register(group string, descriptor ErrorDescriptor) ErrorCode {
|
|
|
|
registerLock.Lock()
|
|
|
|
defer registerLock.Unlock()
|
|
|
|
code := ErrorCode(nextCode)
|
|
|
|
|
|
|
|
descriptor.Code = code
|
|
|
|
|
|
|
|
if _, ok := idToDescriptors[descriptor.Value]; ok {
|
|
|
|
panic(fmt.Sprintf("ErrorValue %s is already registered", descriptor.Value))
|
|
|
|
}
|
|
|
|
if _, ok := errorCodeToDescriptors[descriptor.Code]; ok {
|
|
|
|
panic(fmt.Sprintf("ErrorCode %d is already registered", descriptor.Code))
|
|
|
|
}
|
|
|
|
|
|
|
|
groupToDescriptors[group] = append(groupToDescriptors[group], descriptor)
|
|
|
|
errorCodeToDescriptors[code] = descriptor
|
|
|
|
idToDescriptors[descriptor.Value] = descriptor
|
2015-05-15 03:21:39 +02:00
|
|
|
|
2015-05-27 03:16:45 +02:00
|
|
|
nextCode++
|
|
|
|
return code
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 03:21:39 +02:00
|
|
|
}
|
2015-05-27 03:16:45 +02:00
|
|
|
|
|
|
|
return ErrorCodeUnknown
|
2015-05-15 03:21:39 +02:00
|
|
|
}
|
|
|
|
|
2015-05-27 03:16:45 +02:00
|
|
|
// GetGroupNames returns the list of Error group names that are registered
|
|
|
|
func GetGroupNames() []string {
|
|
|
|
keys := []string{}
|
2015-05-15 03:21:39 +02:00
|
|
|
|
2015-05-27 03:16:45 +02:00
|
|
|
for k := range groupToDescriptors {
|
|
|
|
keys = append(keys, k)
|
2015-05-15 03:21:39 +02:00
|
|
|
}
|
2015-05-27 03:16:45 +02:00
|
|
|
return keys
|
|
|
|
}
|
2015-05-15 03:21:39 +02:00
|
|
|
|
2015-05-27 03:16:45 +02:00
|
|
|
// GetErrorCodeGroup returns the named group of error descriptors
|
|
|
|
func GetErrorCodeGroup(name string) []ErrorDescriptor {
|
|
|
|
return groupToDescriptors[name]
|
2015-05-15 03:21:39 +02: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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error provides a wrapper around ErrorCode with extra Details provided.
|
|
|
|
type Error struct {
|
2015-05-27 02:18:32 +02:00
|
|
|
Code ErrorCode `json:"code"`
|
|
|
|
Detail interface{} `json:"detail,omitempty"`
|
2015-05-15 03:21:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Error returns a human readable representation of the error.
|
|
|
|
func (e Error) Error() string {
|
|
|
|
return fmt.Sprintf("%s: %s",
|
|
|
|
strings.ToLower(strings.Replace(e.Code.String(), "_", " ", -1)),
|
2015-05-27 02:18:32 +02:00
|
|
|
e.Code.Message())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Message returned the human-readable error message for this Error
|
|
|
|
func (e Error) Message() string {
|
|
|
|
return e.Code.Message()
|
2015-05-15 03:21:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Errors provides the envelope for multiple errors and a few sugar methods
|
|
|
|
// for use within the application.
|
2015-05-27 02:18:32 +02:00
|
|
|
type Errors []Error
|
2015-05-15 03:21:39 +02:00
|
|
|
|
2015-05-27 02:18:32 +02:00
|
|
|
// NewError creates a new Error struct based on the passed-in info
|
|
|
|
func NewError(code ErrorCode, details ...interface{}) Error {
|
2015-05-15 03:21:39 +02:00
|
|
|
if len(details) > 1 {
|
|
|
|
panic("please specify zero or one detail items for this error")
|
|
|
|
}
|
|
|
|
|
|
|
|
var detail interface{}
|
|
|
|
if len(details) > 0 {
|
|
|
|
detail = details[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
if err, ok := detail.(error); ok {
|
|
|
|
detail = err.Error()
|
|
|
|
}
|
|
|
|
|
2015-05-27 02:18:32 +02:00
|
|
|
return Error{
|
|
|
|
Code: code,
|
|
|
|
Detail: detail,
|
2015-05-15 03:21:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-27 02:18:32 +02:00
|
|
|
func (errs Errors) Error() string {
|
|
|
|
switch len(errs) {
|
2015-05-15 03:21:39 +02:00
|
|
|
case 0:
|
|
|
|
return "<nil>"
|
|
|
|
case 1:
|
2015-05-27 02:18:32 +02:00
|
|
|
return errs[0].Error()
|
2015-05-15 03:21:39 +02:00
|
|
|
default:
|
|
|
|
msg := "errors:\n"
|
2015-05-27 02:18:32 +02:00
|
|
|
for _, err := range errs {
|
2015-05-15 03:21:39 +02:00
|
|
|
msg += err.Error() + "\n"
|
|
|
|
}
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Len returns the current number of errors.
|
2015-05-27 02:18:32 +02:00
|
|
|
func (errs Errors) Len() int {
|
|
|
|
return len(errs)
|
2015-05-15 03:21:39 +02:00
|
|
|
}
|