-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
278 lines (220 loc) · 9.73 KB
/
Copy patherrors.go
File metadata and controls
278 lines (220 loc) · 9.73 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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package gogenfilter
import (
"fmt"
"strings"
)
// errorPrefixFmt is the branded prefix format for all gogenfilter errors.
const errorPrefixFmt = "[gogenfilter:%s] "
// ErrorCode identifies a specific error condition in the gogenfilter library.
// Codes use snake_case naming and can be used for programmatic error handling.
type ErrorCode string
// String returns the error code as a string.
func (c ErrorCode) String() string { return string(c) }
// SQLCOperation describes the operation being performed when a sqlc config error occurred.
type SQLCOperation string
// String returns the operation as a string.
func (o SQLCOperation) String() string { return string(o) }
// SQLC operations identify what step failed during sqlc config processing.
const (
OpSQLCFind SQLCOperation = "find" // finding sqlc config files
OpSQLCWalk SQLCOperation = "walk" // walking directories for configs
OpSQLCRead SQLCOperation = "read" // reading a config file
OpSQLCCollect SQLCOperation = "collect-output-dirs" // processing config output dirs
OpSQLCParse SQLCOperation = "parse" // parsing YAML content
)
// Error codes identify specific error conditions for programmatic handling.
const (
CodeProjectRootNotFound ErrorCode = "project_root_not_found" // project root not found from start path
CodeProjectRootInvalidPath ErrorCode = "project_root_invalid_path" // start path could not be resolved
CodeInvalidFilterOption ErrorCode = "invalid_filter_option" // filter option is not valid
CodeFileRead ErrorCode = "file_read" // file could not be read during detection
CodeSQLCConfigRead ErrorCode = "sqlc_config_read" // sqlc config file could not be read
CodeSQLCConfigParse ErrorCode = "sqlc_config_parse" // sqlc config file has invalid YAML
CodeSQLCConfigWalk ErrorCode = "sqlc_config_walk" // directory walk for sqlc configs failed
CodeSQLCConfigCollect ErrorCode = "sqlc_config_collect" // collecting output dirs from sqlc configs failed
CodeSQLCConfigFind ErrorCode = "sqlc_config_find" // finding sqlc config files failed
CodeScanConfig ErrorCode = "scan_config" // scan filter configuration failed
CodeScanWalk ErrorCode = "scan_walk" // filesystem walk during scan failed
)
// ErrorCoder is implemented by all gogenfilter errors for programmatic code access.
type ErrorCoder interface {
ErrorCode() ErrorCode
}
// Sentinel errors for use with errors.Is.
// These have zero-value domain fields; matching is by ErrorCode only.
//
//nolint:exhaustruct
var (
// ErrProjectRootNotFound is returned when no project root marker file is found.
ErrProjectRootNotFound = &ProjectRootError{Code: CodeProjectRootNotFound}
// ErrProjectRootInvalidPath is returned when the start path cannot be resolved.
ErrProjectRootInvalidPath = &ProjectRootError{Code: CodeProjectRootInvalidPath}
// ErrInvalidFilterOption is returned when an unrecognized FilterOption is passed.
ErrInvalidFilterOption = &FilterConfigError{Code: CodeInvalidFilterOption}
// ErrFileRead is returned when a file cannot be read during detection.
ErrFileRead = &FileReadError{Code: CodeFileRead}
// ErrSQLCConfigRead is returned when a sqlc config file cannot be read.
ErrSQLCConfigRead = &SQLCConfigError{Code: CodeSQLCConfigRead}
// ErrSQLCConfigParse is returned when sqlc config YAML is invalid.
ErrSQLCConfigParse = &SQLCConfigError{Code: CodeSQLCConfigParse}
// ErrSQLCConfigWalk is returned when walking directories for sqlc configs fails.
ErrSQLCConfigWalk = &SQLCConfigError{Code: CodeSQLCConfigWalk}
// ErrSQLCConfigCollect is returned when collecting output dirs from sqlc configs fails.
ErrSQLCConfigCollect = &SQLCConfigError{Code: CodeSQLCConfigCollect}
// ErrSQLCConfigFind is returned when finding sqlc config files fails.
ErrSQLCConfigFind = &SQLCConfigError{Code: CodeSQLCConfigFind}
// ErrScanConfig is returned when scan filter configuration fails.
ErrScanConfig = &ScanError{Code: CodeScanConfig}
// ErrScanWalk is returned when the filesystem walk during scanning fails.
ErrScanWalk = &ScanError{Code: CodeScanWalk}
)
// ProjectRootError is returned when the project root cannot be found.
type ProjectRootError struct {
Code ErrorCode // classifies the failure (e.g., CodeProjectRootNotFound)
StartPath string // directory where the search started
Markers []string // root marker files searched for (e.g., go.mod)
Err error // underlying error, if any
}
func (e *ProjectRootError) Error() string {
if e.Err != nil {
return fmt.Sprintf(
errorPrefixFmt+"project root not found from %q: %v",
e.Code,
e.StartPath,
e.Err,
)
}
return fmt.Sprintf(errorPrefixFmt+"project root not found from %q (searched for: %s)",
e.Code, e.StartPath, strings.Join(e.Markers, ", "))
}
func (e *ProjectRootError) Unwrap() error { return e.Err }
// Is supports errors.Is by comparing error codes with sentinel errors.
func (e *ProjectRootError) Is(target error) bool {
return errorCodeMatches(e.Code, target)
}
// ErrorCode returns the error code for programmatic matching.
func (e *ProjectRootError) ErrorCode() ErrorCode { return e.Code }
// FilterConfigError is returned when a filter configuration is invalid.
type FilterConfigError struct {
Code ErrorCode // classifies the failure (e.g., CodeInvalidFilterOption)
Option FilterOption // the option that caused the error
Err error // underlying error, if any
}
func (e *FilterConfigError) Error() string {
if e.Err != nil {
if e.Option != "" {
return fmt.Sprintf(
errorPrefixFmt+"invalid filter option %q: %v",
e.Code,
e.Option,
e.Err,
)
}
return fmt.Sprintf(errorPrefixFmt+"invalid filter configuration: %v", e.Code, e.Err)
}
if e.Option != "" {
return fmt.Sprintf(errorPrefixFmt+"invalid filter option %q", e.Code, e.Option)
}
return fmt.Sprintf(errorPrefixFmt+"invalid filter configuration", e.Code)
}
func (e *FilterConfigError) Unwrap() error { return e.Err }
// Is supports errors.Is by comparing error codes with sentinel errors.
func (e *FilterConfigError) Is(target error) bool {
return errorCodeMatches(e.Code, target)
}
// ErrorCode returns the error code for programmatic matching.
func (e *FilterConfigError) ErrorCode() ErrorCode { return e.Code }
// FileReadError is returned when a file cannot be read during content-based detection.
type FileReadError struct {
Code ErrorCode // classifies the failure (always CodeFileRead)
Path string // path of the file that could not be read
Err error // underlying I/O error
}
func (e *FileReadError) Error() string {
if e.Err != nil {
return fmt.Sprintf(errorPrefixFmt+"failed to read %q: %v", e.Code, e.Path, e.Err)
}
return fmt.Sprintf(errorPrefixFmt+"failed to read %q", e.Code, e.Path)
}
func (e *FileReadError) Unwrap() error { return e.Err }
// Is supports errors.Is by comparing error codes with sentinel errors.
func (e *FileReadError) Is(target error) bool {
return errorCodeMatches(e.Code, target)
}
// ErrorCode returns the error code for programmatic matching.
func (e *FileReadError) ErrorCode() ErrorCode { return e.Code }
// SQLCConfigError is returned when a sqlc configuration file cannot be processed.
type SQLCConfigError struct {
Code ErrorCode // classifies the failure (e.g., CodeSQLCConfigParse)
ConfigPath string // path to the config file being processed
Operation SQLCOperation // what was happening when the error occurred
Message string // human-readable description of the problem
Err error // underlying error, if any
}
func (e *SQLCConfigError) Error() string {
if e.ConfigPath != "" {
if e.Err != nil {
return fmt.Sprintf(
errorPrefixFmt+"sqlc config %s %q: %s: %v",
e.Code,
e.Operation,
e.ConfigPath,
e.Message,
e.Err,
)
}
return fmt.Sprintf(
errorPrefixFmt+"sqlc config %s %q: %s",
e.Code,
e.Operation,
e.ConfigPath,
e.Message,
)
}
if e.Err != nil {
return fmt.Sprintf(
errorPrefixFmt+"sqlc config %s: %s: %v",
e.Code,
e.Operation,
e.Message,
e.Err,
)
}
return fmt.Sprintf(errorPrefixFmt+"sqlc config %s: %s", e.Code, e.Operation, e.Message)
}
func (e *SQLCConfigError) Unwrap() error { return e.Err }
// Is supports errors.Is by comparing error codes with sentinel errors.
func (e *SQLCConfigError) Is(target error) bool {
return errorCodeMatches(e.Code, target)
}
// ErrorCode returns the error code for programmatic matching.
func (e *SQLCConfigError) ErrorCode() ErrorCode { return e.Code }
// ScanError is returned when scanning a project for generated files fails.
type ScanError struct {
Code ErrorCode // classifies the failure (e.g., CodeScanConfig, CodeScanWalk)
Phase string // what scan phase failed (e.g., "configure", "walk", "collect")
Err error // underlying error
}
func (e *ScanError) Error() string {
if e.Phase != "" && e.Err != nil {
return fmt.Sprintf(errorPrefixFmt+"scan %s failed: %v", e.Code, e.Phase, e.Err)
}
if e.Phase != "" {
return fmt.Sprintf(errorPrefixFmt+"scan %s failed", e.Code, e.Phase)
}
if e.Err != nil {
return fmt.Sprintf(errorPrefixFmt+"scan failed: %v", e.Code, e.Err)
}
return fmt.Sprintf(errorPrefixFmt+"scan failed", e.Code)
}
func (e *ScanError) Unwrap() error { return e.Err }
// Is supports errors.Is by comparing error codes with sentinel errors.
func (e *ScanError) Is(target error) bool {
return errorCodeMatches(e.Code, target)
}
// ErrorCode returns the error code for programmatic matching.
func (e *ScanError) ErrorCode() ErrorCode { return e.Code }
func errorCodeMatches(code ErrorCode, target error) bool {
targetError, ok := target.(ErrorCoder)
return ok && code == targetError.ErrorCode()
}