-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
191 lines (153 loc) · 5.77 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
package core
import (
"errors"
"fmt"
"strings"
)
var (
ErrRepositoryNameIsMandatory = errors.New("repository name is mandatory")
ErrWorkspacePathDoesntExist = errors.New("workspace path doesn't exist")
ErrWorkspacePathIsExpectedToBeADirectory = errors.New("workspace path is expected to be a directory")
ErrNoTemplateAvailable = errors.New("not found as none available")
ErrNoRepositoryTemplateAvailable = fmt.Errorf("%s template %w", RepositoryTemplateType, ErrNoTemplateAvailable)
ErrNoBranchTemplateAvailable = fmt.Errorf("%s template %w", BranchTemplateType, ErrNoTemplateAvailable)
ErrNoBranchProtectionTemplateAvailable = fmt.Errorf(
"%s template %w",
BranchProtectionTemplateType,
ErrNoTemplateAvailable,
)
ErrTemplateNotFound = errors.New("not found")
ErrRepositoryTemplateNotFound = fmt.Errorf("%s template %w", RepositoryTemplateType, ErrTemplateNotFound)
ErrBranchTemplateNotFound = fmt.Errorf("%s template %w", BranchTemplateType, ErrTemplateNotFound)
ErrBranchProtectionTemplateNotFound = fmt.Errorf("%s template %w", BranchProtectionTemplateType, ErrTemplateNotFound)
ErrMaxTemplateCount = errors.New("maximum template count reached")
ErrMaxTemplateDepth = errors.New("maximum template depth reached")
ErrDuringWriteTerraformFiles = errors.New("error while writing terraform files")
ErrDuringFileGeneration = errors.New("error while generating files")
ErrDuringComputation = errors.New("error during computation")
ErrSchemaValidation = errors.New("schema validation error")
ErrEmptySchema = errors.New("empty schema")
ErrSchemaNotFound = errors.New("schema not found")
ErrSchemaIsNil = errors.New("schema is nil")
ErrDuringSchemaCompilation = errors.New("error during schema compilation")
ErrFileError = errors.New("file")
ErrBranchError = errors.New("branch")
ErrDefaultBranchError = errors.New("default branch")
ErrBranchProtectionError = errors.New("branch protection")
)
func BranchError(branch string, err error) error {
return fmt.Errorf("%w %s: %w", ErrBranchError, branch, err)
}
func DefaultBranchError(err error) error {
return fmt.Errorf("%w: %w", ErrDefaultBranchError, err)
}
func BranchProtectionError(index int, err error) error {
return fmt.Errorf("%w #%d: %w", ErrBranchProtectionError, index, err)
}
func FileError(filepath string, err error) error {
return fmt.Errorf("%w %s: %w", ErrFileError, filepath, err)
}
func FileGenerationError(msgList []string) error {
return fmt.Errorf("%w:\n\t - %s", ErrDuringFileGeneration, strings.Join(msgList, "\n\t - "))
}
func WorkspacePathDoesntExistError(path string) error {
return fmt.Errorf("%w: %s", ErrWorkspacePathDoesntExist, path)
}
func WorkspacePathIsExpectedToBeADirectoryError(path string) error {
return fmt.Errorf("%w: %s", ErrWorkspacePathIsExpectedToBeADirectory, path)
}
func ComputationError(errList []error) error {
return fmt.Errorf("%w:\n\t - %w", ErrDuringComputation, JoinErrors(errList, "\n\t - "))
}
func RepositoryNameIsMandatoryForConfigIndexError(index int) error {
return fmt.Errorf("config #%d: %w", index, ErrRepositoryNameIsMandatory)
}
func RepositoryNameIsMandatoryForRepoError(index int) error {
return fmt.Errorf("repo #%d: %w", index, ErrRepositoryNameIsMandatory)
}
func UnknownTemplateError(tplType string, tplName string) error {
var baseError error
switch tplType {
case RepositoryTemplateType:
baseError = ErrRepositoryTemplateNotFound
case BranchTemplateType:
baseError = ErrBranchTemplateNotFound
case BranchProtectionTemplateType:
baseError = ErrBranchProtectionTemplateNotFound
default:
return fmt.Errorf("\"%s\" %s template %w", tplName, tplType, ErrTemplateNotFound)
}
return fmt.Errorf("\"%s\" %w", tplName, baseError)
}
func NoTemplateAvailableError(tplType string) error {
switch tplType {
case RepositoryTemplateType:
return ErrNoRepositoryTemplateAvailable
case BranchTemplateType:
return ErrNoBranchTemplateAvailable
case BranchProtectionTemplateType:
return ErrNoBranchProtectionTemplateAvailable
default:
return fmt.Errorf("%s template %w", tplType, ErrNoTemplateAvailable)
}
}
func MaxTemplateCountReachedError(tplType string, path []string) error {
pathString := "ROOT"
if len(path) > 0 {
pathString = strings.Join(path, "->")
}
return fmt.Errorf(
"%w: more than %d %s template detected for %s",
ErrMaxTemplateCount,
TemplateMaxCount,
tplType,
pathString,
)
}
func MaxTemplateDepthReachedError(tplType string, path []string) error {
pathString := "ROOT"
if len(path) > 0 {
pathString = strings.Join(path, "->")
}
return fmt.Errorf(
"%w: more than %d levels of %s template detected for %s",
ErrMaxTemplateDepth,
TemplateMaxDepth,
tplType,
pathString,
)
}
func SchemaValidationError(path string, location string, msg string) error {
return fmt.Errorf(
"%w: file %s: %s %s",
ErrSchemaValidation,
path,
location,
msg,
)
}
func EmptySchemaError(url string) error {
return fmt.Errorf("%w: url %q", ErrEmptySchema, url)
}
func SchemaNotFoundError(url string) error {
return fmt.Errorf("%w: url %q", ErrSchemaNotFound, url)
}
func SchemaIsNilError(url string) error {
return fmt.Errorf("%w: url %q", ErrSchemaIsNil, url)
}
func SchemaCompilationError(url string, msg string) error {
return fmt.Errorf("%w: url %s / error %s", ErrDuringSchemaCompilation, url, msg)
}
func TerraformFileWritingErrors(errList []error) error {
return fmt.Errorf("%w:\n\t - %w", ErrDuringWriteTerraformFiles, JoinErrors(errList, "\n\t - "))
}
func JoinErrors(errList []error, separator string) error {
if separator == "\n" {
return errors.Join(errList...)
}
err := errList[0]
for _, subErr := range errList[1:] {
err = fmt.Errorf("%w%s%w", err, separator, subErr)
}
return err
}