-
Notifications
You must be signed in to change notification settings - Fork 7
/
errors.go
219 lines (175 loc) · 6.11 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package presenters
import (
"errors"
"fmt"
"strings"
"github.com/recode-sh/cli/internal/constants"
"github.com/recode-sh/cli/internal/exceptions"
"github.com/recode-sh/cli/internal/system"
"github.com/recode-sh/recode/entities"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type ViewableError struct {
Title string
Message string
}
type ViewableErrorBuilder interface {
Build(error) *ViewableError
}
type RecodeViewableErrorBuilder struct{}
func NewRecodeViewableErrorBuilder() RecodeViewableErrorBuilder {
return RecodeViewableErrorBuilder{}
}
func (RecodeViewableErrorBuilder) Build(err error) (viewableError *ViewableError) {
viewableError = &ViewableError{}
if typedError, ok := err.(entities.ErrClusterNotExists); ok {
viewableError.Title = "Cluster not found"
viewableError.Message = fmt.Sprintf(
"The cluster \"%s\" was not found.",
typedError.ClusterName,
)
return
}
if typedError, ok := err.(entities.ErrClusterAlreadyExists); ok {
viewableError.Title = "Cluster already exists"
viewableError.Message = fmt.Sprintf(
"The cluster \"%s\" already exists.",
typedError.ClusterName,
)
return
}
if typedError, ok := err.(entities.ErrDevEnvNotExists); ok {
viewableError.Title = "Development environment not found"
if typedError.ClusterName != entities.DefaultClusterName {
viewableError.Message = fmt.Sprintf(
"The development environment \"%s\" was not found in the cluster \"%s\".",
typedError.DevEnvName,
typedError.ClusterName,
)
return
}
viewableError.Message = fmt.Sprintf(
"The development environment \"%s\" was not found.",
typedError.DevEnvName,
)
return
}
if errors.Is(err, exceptions.ErrUserNotLoggedIn) {
viewableError.Title = "GitHub account not connected"
viewableError.Message = fmt.Sprintf(
"You must first connect your GitHub account using the command \"recode login\".\n\n"+
"Recode requires the following permissions:\n\n"+
" - \"Public SSH keys\" and \"Repositories\" to let you access your repositories from your development environments\n\n"+
" - \"GPG Keys\" and \"Personal user data\" to configure Git and sign your commits (verified badge)\n\n"+
"All your data (including the OAuth access token) will only be stored locally (in \"%s\").",
system.UserConfigFilePath(),
)
return
}
if typedError, ok := err.(entities.ErrInvalidDevEnvUserConfig); ok {
viewableError.Title = "Invalid user configuration"
bold := constants.Bold
viewableError.Message = fmt.Sprintf(
"The repository \"%s/%s\" contains an invalid configuration.\n\n"+bold("Reason:")+" %s",
typedError.RepoOwner,
entities.DevEnvUserConfigRepoName,
typedError.Reason,
)
return
}
if typedError, ok := err.(entities.ErrDevEnvRepositoryNotFound); ok {
viewableError.Title = "Repository not found"
viewableError.Message = fmt.Sprintf(
"The repository \"%s/%s\" was not found.\n\n"+
"Please double check that this repository exists and that you can access it.",
typedError.RepoOwner,
typedError.RepoName,
)
return
}
if typedError, ok := err.(entities.ErrStartRemovingDevEnv); ok {
viewableError.Title = "Invalid development environment state"
viewableError.Message = fmt.Sprintf(
"The development environment \"%s\" cannot be started because it is currently removing.\n\n"+
"You must wait for the removing process to terminate.",
typedError.DevEnvName,
)
return
}
if typedError, ok := err.(entities.ErrStartStoppingDevEnv); ok {
viewableError.Title = "Invalid development environment state"
viewableError.Message = fmt.Sprintf(
"The development environment \"%s\" cannot be started because it is currently stopping.\n\n"+
"You must wait for the stopping process to terminate.",
typedError.DevEnvName,
)
return
}
if typedError, ok := err.(entities.ErrStopRemovingDevEnv); ok {
viewableError.Title = "Invalid development environment state"
viewableError.Message = fmt.Sprintf(
"The development environment \"%s\" cannot be stopped because it is currently removing.\n\n"+
"You must wait for the removing process to terminate.",
typedError.DevEnvName,
)
return
}
if typedError, ok := err.(entities.ErrStopCreatingDevEnv); ok {
viewableError.Title = "Invalid development environment state"
viewableError.Message = fmt.Sprintf(
"The development environment \"%s\" cannot be stopped because it is currently creating.\n\n"+
"You must wait for the creation process to terminate.",
typedError.DevEnvName,
)
return
}
if typedError, ok := err.(entities.ErrStopStartingDevEnv); ok {
viewableError.Title = "Invalid development environment state"
viewableError.Message = fmt.Sprintf(
"The development environment \"%s\" cannot be stopped because it is currently starting.\n\n"+
"You must wait for the starting process to terminate.",
typedError.DevEnvName,
)
return
}
if typedError, ok := err.(exceptions.ErrLoginError); ok {
viewableError.Title = "GitHub connection error"
viewableError.Message = fmt.Sprintf(
"An error has occured during the authorization of the Recode application.\n\n%s",
typedError.Reason,
)
return
}
if typedError, ok := err.(exceptions.ErrMissingRequirements); ok {
viewableError.Title = "Missing requirements"
viewableError.Message = fmt.Sprintf(
"The following requirements are missing:\n\n - %s",
strings.Join(typedError.MissingRequirements, "\n\n - "),
)
return
}
bold := constants.Bold
if status, ok := status.FromError(err); ok {
viewableError.Title = "Recode agent error"
errorMessage := status.Message()
if len(errorMessage) >= 2 {
errorMessage = strings.ToTitle(errorMessage[0:1]) + errorMessage[1:] + "."
}
viewableError.Message = errorMessage
if status.Code() != codes.Unknown {
viewableError.Message += "\n\n" +
bold("Error code: ") +
status.Code().String()
}
return
}
viewableError.Title = "Unknown error"
viewableError.Message = fmt.Sprintf(
"An unknown error occurred.\n\n"+
"You could try to fix it (using the details below) or open a new issue at: https://github.com/recode-sh/cli/issues/new\n\n"+
bold("%s"),
err.Error(),
)
return
}