forked from fabric8-services/fabric8-wit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
116 lines (93 loc) · 3.34 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
package errors
import "fmt"
const (
stBadParameterErrorMsg = "Bad value for parameter '%s': '%v'"
stBadParameterErrorExpectedMsg = "Bad value for parameter '%s': '%v' (expected: '%v')"
stNotFoundErrorMsg = "%s with id '%s' not found"
)
// Constants that can be used to identify internal server errors
const (
ErrInternalDatabase = "database_error"
)
type simpleError struct {
message string
}
func (err simpleError) Error() string {
return err.message
}
// NewInternalError returns the custom defined error of type InternalError.
func NewInternalError(msg string) InternalError {
return InternalError{simpleError{msg}}
}
// NewUnauthorizedError returns the custom defined error of type UnauthorizedError.
func NewUnauthorizedError(msg string) UnauthorizedError {
return UnauthorizedError{simpleError{msg}}
}
// NewForbiddenError returns the custom defined error of type ForbiddenError.
func NewForbiddenError(msg string) ForbiddenError {
return ForbiddenError{simpleError{msg}}
}
// InternalError means that the operation failed for some internal, unexpected reason
type InternalError struct {
simpleError
}
// UnauthorizedError means that the operation is unauthorized
type UnauthorizedError struct {
simpleError
}
// ForbiddenError means that the operation is forbidden
type ForbiddenError struct {
simpleError
}
// VersionConflictError means that the version was not as expected in an update operation
type VersionConflictError struct {
simpleError
}
// NewVersionConflictError returns the custom defined error of type VersionConflictError.
func NewVersionConflictError(msg string) VersionConflictError {
return VersionConflictError{simpleError{msg}}
}
// BadParameterError means that a parameter was not as required
type BadParameterError struct {
parameter string
value interface{}
expectedValue interface{}
hasExpectedValue bool
}
// Error implements the error interface
func (err BadParameterError) Error() string {
if err.hasExpectedValue {
return fmt.Sprintf(stBadParameterErrorExpectedMsg, err.parameter, err.value, err.expectedValue)
}
return fmt.Sprintf(stBadParameterErrorMsg, err.parameter, err.value)
}
// Expected sets the optional expectedValue parameter on the BadParameterError
func (err BadParameterError) Expected(expexcted interface{}) BadParameterError {
err.expectedValue = expexcted
err.hasExpectedValue = true
return err
}
// NewBadParameterError returns the custom defined error of type NewBadParameterError.
func NewBadParameterError(param string, actual interface{}) BadParameterError {
return BadParameterError{parameter: param, value: actual}
}
// NewConversionError returns the custom defined error of type NewConversionError.
func NewConversionError(msg string) ConversionError {
return ConversionError{simpleError{msg}}
}
// ConversionError error means something went wrong converting between different representations
type ConversionError struct {
simpleError
}
// NotFoundError means the object specified for the operation does not exist
type NotFoundError struct {
entity string
ID string
}
func (err NotFoundError) Error() string {
return fmt.Sprintf(stNotFoundErrorMsg, err.entity, err.ID)
}
// NewNotFoundError returns the custom defined error of type NewNotFoundError.
func NewNotFoundError(entity string, id string) NotFoundError {
return NotFoundError{entity: entity, ID: id}
}