This repository was archived by the owner on May 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathcode.go
More file actions
160 lines (131 loc) · 4.21 KB
/
Copy pathcode.go
File metadata and controls
160 lines (131 loc) · 4.21 KB
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
package e
import (
"errors"
"fmt"
"net/http"
"strings"
)
const (
// ErrorCodeConfigInvalid is that an error occurs when it parse the file.
ErrorCodeConfigInvalid ErrorCode = "config_parse_error"
// ErrorCodeConfigUndefinedEnv is that the environment is not defined in the configuration file.
ErrorCodeConfigUndefinedEnv ErrorCode = "config_undefined_env"
// ErrorCodeDeploymentConflict is the deployment number is conflicted.
ErrorCodeDeploymentConflict ErrorCode = "deployment_conflict"
// ErrorCodeDeploymentInvalid is the payload is invalid.
ErrorCodeDeploymentInvalid ErrorCode = "deployment_invalid"
// ErrorCodeDeploymentLocked is when the environment is locked.
ErrorCodeDeploymentLocked ErrorCode = "deployment_locked"
// ErrorCodeDeploymentFrozen is when the time in in the freeze window.
ErrorCodeDeploymentFrozen ErrorCode = "deployment_frozen"
// ErrorCodeDeploymentUnapproved is when the deployment is not approved.
ErrorCodeDeploymentNotApproved ErrorCode = "deployment_not_approved"
// ErrorCodeDeploymentSerialization is the serialization error.
ErrorCodeDeploymentSerialization ErrorCode = "deployment_serialization"
// ErrorCodeDeploymentStatusNotWaiting is the status must be 'waiting' to create a remote deployment.
ErrorCodeDeploymentStatusInvalid ErrorCode = "deployment_status_invalid"
// ErrorCodeEntityNotFound is the entity is not found.
// Entity is a resource of store or scm.
ErrorCodeEntityNotFound ErrorCode = "entity_not_found"
// ErrorCodeEntityUnprocessable is the entity is unprocessable.
ErrorCodeEntityUnprocessable ErrorCode = "entity_unprocessable"
// ErrorCodeInternalError is the internal error couldn't be handled.
ErrorCodeInternalError ErrorCode = "internal_error"
// ErrorCodeLockAlreadyExist is that the environment is already locked.
ErrorCodeLockAlreadyExist ErrorCode = "lock_already_exist"
// ErrorCodeLicenseDecode is the error when the license is decoded.
ErrorCodeLicenseDecode ErrorCode = "license_decode"
// ErrorCodeLicenseRequired is that the license is required.
ErrorCodeLicenseRequired ErrorCode = "license_required"
// ErrorCodeParameterInvalid is a parameter of a request is invalid.
ErrorCodeParameterInvalid ErrorCode = "parameter_invalid"
// ErrorPermissionRequired is the permission is required to access.
ErrorPermissionRequired ErrorCode = "permission_required"
// ErrorRepoUniqueName is the repository name must be unique.
ErrorRepoUniqueName ErrorCode = "repo_unique_name"
)
type (
ErrorStatus int
ErrorCode string
Error struct {
Code ErrorCode
Message string
Wrap error
httpCode int
}
)
func NewError(code ErrorCode, wrap error) *Error {
return &Error{
Code: code,
Message: GetMessage(code),
Wrap: wrap,
httpCode: mapHTTPCode(code),
}
}
// TODO: Migrate the other functions which
// constructs an error into this function.
func NError(code ErrorCode, message string) *Error {
return &Error{
Code: code,
Message: GetMessage(code),
httpCode: mapHTTPCode(code),
}
}
func NewErrorWithMessage(code ErrorCode, message string, wrap error) *Error {
return &Error{
Code: code,
Message: message,
Wrap: wrap,
httpCode: mapHTTPCode(code),
}
}
func (e *Error) Error() string {
msgs := []string{
fmt.Sprintf("Code: %s", e.Code),
fmt.Sprintf("Message: %s", e.Message),
}
if e.Wrap != nil {
msgs = append(msgs, fmt.Sprintf("Wrap: %s", e.Wrap))
}
return strings.Join(msgs, ", ")
}
// GetHTTPCode returns the HTTP code.
func (e *Error) GetHTTPCode() int {
return e.httpCode
}
// SetHTTPCode sets the HTTP code manually.
func (e *Error) SetHTTPCode(code int) *Error {
e.httpCode = code
return e
}
func (e *Error) Unwrap() error {
return e.Wrap
}
func (e *Error) SetWrap(wrap error) *Error {
e.Wrap = wrap
return e
}
func mapHTTPCode(code ErrorCode) int {
httpCode, ok := httpCodes[code]
if !ok {
return http.StatusInternalServerError
}
return httpCode
}
func IsError(err error) bool {
var ge *Error
return errors.As(err, &ge)
}
// HasErrorCode verify the type of error and the code.
func HasErrorCode(err error, codes ...ErrorCode) bool {
var ge *Error
if !errors.As(err, &ge) {
return false
}
for _, code := range codes {
if ge.Code == code {
return true
}
}
return false
}