~ubuntu-branches/ubuntu/wily/juju-core/wily

« back to all changes in this revision

Viewing changes to src/gopkg.in/goose.v1/errors/errors.go

  • Committer: Package Import Robot
  • Author(s): Curtis C. Hovey
  • Date: 2015-09-22 15:27:01 UTC
  • mfrom: (1.1.36)
  • Revision ID: package-import@ubuntu.com-20150922152701-lzq2yhn2uaahrdqu
Tags: 1.24.6-0ubuntu1
* New upstream release (LP: #1481556).
* d/copyright updated for Juju 1.24.6 (Last verified commit changes).
* d/tests/* Run tests with upstart when Juju version before 1.23.
* Prefer gccgo-5 for ppc64el and arm64 in build-deps.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// This package provides an Error implementation which knows about types of error, and which has support
 
2
// for error causes.
 
3
 
 
4
package errors
 
5
 
 
6
import "fmt"
 
7
 
 
8
type Code string
 
9
 
 
10
const (
 
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")
 
19
)
 
20
 
 
21
// Error instances store an optional error cause.
 
22
type Error interface {
 
23
        error
 
24
        Cause() error
 
25
}
 
26
 
 
27
type gooseError struct {
 
28
        error
 
29
        errcode Code
 
30
        cause   error
 
31
}
 
32
 
 
33
// Type checks.
 
34
var _ Error = (*gooseError)(nil)
 
35
 
 
36
// Code returns the error code.
 
37
func (err *gooseError) code() Code {
 
38
        if err.errcode != UnspecifiedError {
 
39
                return err.errcode
 
40
        }
 
41
        if e, ok := err.cause.(*gooseError); ok {
 
42
                return e.code()
 
43
        }
 
44
        return UnspecifiedError
 
45
}
 
46
 
 
47
// Cause returns the error cause.
 
48
func (err *gooseError) Cause() error {
 
49
        return err.cause
 
50
}
 
51
 
 
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 {
 
55
                return true
 
56
        }
 
57
        if cause, ok := err.cause.(*gooseError); ok {
 
58
                return cause.code() == code
 
59
        }
 
60
        return false
 
61
}
 
62
 
 
63
// Error fulfills the error interface, taking account of any caused by error.
 
64
func (err *gooseError) Error() string {
 
65
        if err.cause != nil {
 
66
                return fmt.Sprintf("%v\ncaused by: %v", err.error, err.cause)
 
67
        }
 
68
        return err.error.Error()
 
69
}
 
70
 
 
71
func IsNotFound(err error) bool {
 
72
        if e, ok := err.(*gooseError); ok {
 
73
                return e.causedBy(NotFoundError)
 
74
        }
 
75
        return false
 
76
}
 
77
 
 
78
func IsDuplicateValue(err error) bool {
 
79
        if e, ok := err.(*gooseError); ok {
 
80
                return e.causedBy(DuplicateValueError)
 
81
        }
 
82
        return false
 
83
}
 
84
 
 
85
func IsTimeout(err error) bool {
 
86
        if e, ok := err.(*gooseError); ok {
 
87
                return e.causedBy(TimeoutError)
 
88
        }
 
89
        return false
 
90
}
 
91
 
 
92
func IsUnauthorised(err error) bool {
 
93
        if e, ok := err.(*gooseError); ok {
 
94
                return e.causedBy(UnauthorisedError)
 
95
        }
 
96
        return false
 
97
}
 
98
 
 
99
func IsNotImplemented(err error) bool {
 
100
        if e, ok := err.(*gooseError); ok {
 
101
                return e.causedBy(NotImplementedError)
 
102
        }
 
103
        return false
 
104
}
 
105
 
 
106
// makeErrorf creates a new Error instance with the specified cause.
 
107
func makeErrorf(code Code, cause error, format string, args ...interface{}) Error {
 
108
        return &gooseError{
 
109
                errcode: code,
 
110
                error:   fmt.Errorf(format, args...),
 
111
                cause:   cause,
 
112
        }
 
113
}
 
114
 
 
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...)
 
118
}
 
119
 
 
120
// NewNotFoundf creates a new NotFound Error instance with the specified cause.
 
121
func NewNotFoundf(cause error, context interface{}, format string, args ...interface{}) Error {
 
122
        if format == "" {
 
123
                format = fmt.Sprintf("Not found: %s", context)
 
124
        }
 
125
        return makeErrorf(NotFoundError, cause, format, args...)
 
126
}
 
127
 
 
128
// NewDuplicateValuef creates a new DuplicateValue Error instance with the specified cause.
 
129
func NewDuplicateValuef(cause error, context interface{}, format string, args ...interface{}) Error {
 
130
        if format == "" {
 
131
                format = fmt.Sprintf("Duplicate: %s", context)
 
132
        }
 
133
        return makeErrorf(DuplicateValueError, cause, format, args...)
 
134
}
 
135
 
 
136
// NewTimeoutf creates a new Timeout Error instance with the specified cause.
 
137
func NewTimeoutf(cause error, context interface{}, format string, args ...interface{}) Error {
 
138
        if format == "" {
 
139
                format = fmt.Sprintf("Timeout: %s", context)
 
140
        }
 
141
        return makeErrorf(TimeoutError, cause, format, args...)
 
142
}
 
143
 
 
144
// NewUnauthorisedf creates a new Unauthorised Error instance with the specified cause.
 
145
func NewUnauthorisedf(cause error, context interface{}, format string, args ...interface{}) Error {
 
146
        if format == "" {
 
147
                format = fmt.Sprintf("Unauthorised: %s", context)
 
148
        }
 
149
        return makeErrorf(UnauthorisedError, cause, format, args...)
 
150
}
 
151
 
 
152
// NewNotImplementedf creates a new NotImplemented Error instance with the specified cause.
 
153
func NewNotImplementedf(cause error, context interface{}, format string, args ...interface{}) Error {
 
154
        if format == "" {
 
155
                format = fmt.Sprintf("Not implemented: %s", context)
 
156
        }
 
157
        return makeErrorf(NotImplementedError, cause, format, args...)
 
158
}