1
// This package provides an Error implementation which knows about types of error, and which has support
11
// Public available error types.
12
// These errors are provided because they are specifically required by business logic in the callers.
13
UnspecifiedError = Code("Unspecified")
14
NotFoundError = Code("NotFound")
15
DuplicateValueError = Code("DuplicateValue")
16
TimeoutError = Code("Timeout")
17
UnauthorisedError = Code("Unauthorised")
18
NotImplementedError = Code("NotImplemented")
21
// Error instances store an optional error cause.
22
type Error interface {
27
type gooseError struct {
34
var _ Error = (*gooseError)(nil)
36
// Code returns the error code.
37
func (err *gooseError) code() Code {
38
if err.errcode != UnspecifiedError {
41
if e, ok := err.cause.(*gooseError); ok {
44
return UnspecifiedError
47
// Cause returns the error cause.
48
func (err *gooseError) Cause() error {
52
// CausedBy returns true if this error or its cause are of the specified error code.
53
func (err *gooseError) causedBy(code Code) bool {
54
if err.code() == code {
57
if cause, ok := err.cause.(*gooseError); ok {
58
return cause.code() == code
63
// Error fulfills the error interface, taking account of any caused by error.
64
func (err *gooseError) Error() string {
66
return fmt.Sprintf("%v\ncaused by: %v", err.error, err.cause)
68
return err.error.Error()
71
func IsNotFound(err error) bool {
72
if e, ok := err.(*gooseError); ok {
73
return e.causedBy(NotFoundError)
78
func IsDuplicateValue(err error) bool {
79
if e, ok := err.(*gooseError); ok {
80
return e.causedBy(DuplicateValueError)
85
func IsTimeout(err error) bool {
86
if e, ok := err.(*gooseError); ok {
87
return e.causedBy(TimeoutError)
92
func IsUnauthorised(err error) bool {
93
if e, ok := err.(*gooseError); ok {
94
return e.causedBy(UnauthorisedError)
99
func IsNotImplemented(err error) bool {
100
if e, ok := err.(*gooseError); ok {
101
return e.causedBy(NotImplementedError)
106
// makeErrorf creates a new Error instance with the specified cause.
107
func makeErrorf(code Code, cause error, format string, args ...interface{}) Error {
110
error: fmt.Errorf(format, args...),
115
// Newf creates a new Unspecified Error instance with the specified cause.
116
func Newf(cause error, format string, args ...interface{}) Error {
117
return makeErrorf(UnspecifiedError, cause, format, args...)
120
// NewNotFoundf creates a new NotFound Error instance with the specified cause.
121
func NewNotFoundf(cause error, context interface{}, format string, args ...interface{}) Error {
123
format = fmt.Sprintf("Not found: %s", context)
125
return makeErrorf(NotFoundError, cause, format, args...)
128
// NewDuplicateValuef creates a new DuplicateValue Error instance with the specified cause.
129
func NewDuplicateValuef(cause error, context interface{}, format string, args ...interface{}) Error {
131
format = fmt.Sprintf("Duplicate: %s", context)
133
return makeErrorf(DuplicateValueError, cause, format, args...)
136
// NewTimeoutf creates a new Timeout Error instance with the specified cause.
137
func NewTimeoutf(cause error, context interface{}, format string, args ...interface{}) Error {
139
format = fmt.Sprintf("Timeout: %s", context)
141
return makeErrorf(TimeoutError, cause, format, args...)
144
// NewUnauthorisedf creates a new Unauthorised Error instance with the specified cause.
145
func NewUnauthorisedf(cause error, context interface{}, format string, args ...interface{}) Error {
147
format = fmt.Sprintf("Unauthorised: %s", context)
149
return makeErrorf(UnauthorisedError, cause, format, args...)
152
// NewNotImplementedf creates a new NotImplemented Error instance with the specified cause.
153
func NewNotImplementedf(cause error, context interface{}, format string, args ...interface{}) Error {
155
format = fmt.Sprintf("Not implemented: %s", context)
157
return makeErrorf(NotImplementedError, cause, format, args...)