-
Notifications
You must be signed in to change notification settings - Fork 36
/
errors.go
169 lines (143 loc) · 5.18 KB
/
errors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright 2016 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package gomaasapi
import (
"fmt"
"github.com/juju/errors"
)
// NoMatchError is returned when the requested action cannot be performed
// due to being unable to service due to no entities available that match the
// request.
type NoMatchError struct {
errors.Err
}
// NewNoMatchError constructs a new NoMatchError and sets the location.
func NewNoMatchError(message string) error {
err := &NoMatchError{Err: errors.NewErr(message)}
err.SetLocation(1)
return err
}
// IsNoMatchError returns true if err is a NoMatchError.
func IsNoMatchError(err error) bool {
_, ok := errors.Cause(err).(*NoMatchError)
return ok
}
// UnexpectedError is an error for a condition that hasn't been determined.
type UnexpectedError struct {
errors.Err
}
// NewUnexpectedError constructs a new UnexpectedError and sets the location.
func NewUnexpectedError(err error) error {
uerr := &UnexpectedError{Err: errors.NewErr("unexpected: %v", err)}
uerr.SetLocation(1)
return errors.Wrap(err, uerr)
}
// IsUnexpectedError returns true if err is an UnexpectedError.
func IsUnexpectedError(err error) bool {
_, ok := errors.Cause(err).(*UnexpectedError)
return ok
}
// UnsupportedVersionError refers to calls made to an unsupported api version.
type UnsupportedVersionError struct {
errors.Err
}
// NewUnsupportedVersionError constructs a new UnsupportedVersionError and sets the location.
func NewUnsupportedVersionError(format string, args ...interface{}) error {
err := &UnsupportedVersionError{Err: errors.NewErr(format, args...)}
err.SetLocation(1)
return err
}
// IsUnsupportedVersionError returns true if err is an UnsupportedVersionError.
func IsUnsupportedVersionError(err error) bool {
_, ok := errors.Cause(err).(*UnsupportedVersionError)
return ok
}
// WrapWithUnsupportedVersionError constructs a new
// UnsupportedVersionError wrapping the passed error.
func WrapWithUnsupportedVersionError(err error) error {
uerr := &UnsupportedVersionError{Err: errors.NewErr("unsupported version: %v", err)}
uerr.SetLocation(1)
return errors.Wrap(err, uerr)
}
// DeserializationError types are returned when the returned JSON data from
// the controller doesn't match the code's expectations.
type DeserializationError struct {
errors.Err
}
// NewDeserializationError constructs a new DeserializationError and sets the location.
func NewDeserializationError(format string, args ...interface{}) error {
err := &DeserializationError{Err: errors.NewErr(format, args...)}
err.SetLocation(1)
return err
}
// WrapWithDeserializationError constructs a new DeserializationError with the
// specified message, and sets the location and returns a new error with the
// full error stack set including the error passed in.
func WrapWithDeserializationError(err error, format string, args ...interface{}) error {
message := fmt.Sprintf(format, args...)
// We want the deserialization error message to include the error text of the
// previous error, but wrap it in the new type.
derr := &DeserializationError{Err: errors.NewErr(message + ": " + err.Error())}
derr.SetLocation(1)
wrapped := errors.Wrap(err, derr)
// We want the location of the wrapped error to be the caller of this function,
// not the line above.
if errType, ok := wrapped.(*errors.Err); ok {
// We know it is because that is what Wrap returns.
errType.SetLocation(1)
}
return wrapped
}
// IsDeserializationError returns true if err is a DeserializationError.
func IsDeserializationError(err error) bool {
_, ok := errors.Cause(err).(*DeserializationError)
return ok
}
// BadRequestError is returned when the requested action cannot be performed
// due to bad or incorrect parameters passed to the server.
type BadRequestError struct {
errors.Err
}
// NewBadRequestError constructs a new BadRequestError and sets the location.
func NewBadRequestError(message string) error {
err := &BadRequestError{Err: errors.NewErr(message)}
err.SetLocation(1)
return err
}
// IsBadRequestError returns true if err is a NoMatchError.
func IsBadRequestError(err error) bool {
_, ok := errors.Cause(err).(*BadRequestError)
return ok
}
// PermissionError is returned when the user does not have permission to do the
// requested action.
type PermissionError struct {
errors.Err
}
// NewPermissionError constructs a new PermissionError and sets the location.
func NewPermissionError(message string) error {
err := &PermissionError{Err: errors.NewErr(message)}
err.SetLocation(1)
return err
}
// IsPermissionError returns true if err is a NoMatchError.
func IsPermissionError(err error) bool {
_, ok := errors.Cause(err).(*PermissionError)
return ok
}
// CannotCompleteError is returned when the requested action is unable to
// complete for some server side reason.
type CannotCompleteError struct {
errors.Err
}
// NewCannotCompleteError constructs a new CannotCompleteError and sets the location.
func NewCannotCompleteError(message string) error {
err := &CannotCompleteError{Err: errors.NewErr(message)}
err.SetLocation(1)
return err
}
// IsCannotCompleteError returns true if err is a NoMatchError.
func IsCannotCompleteError(err error) bool {
_, ok := errors.Cause(err).(*CannotCompleteError)
return ok
}