-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
209 lines (169 loc) · 6.99 KB
/
Copy patherrors.go
File metadata and controls
209 lines (169 loc) · 6.99 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
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
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
)
// 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}
// 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}
)
// 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 {
t, ok := target.(*ProjectRootError)
if !ok {
return false
}
return e.Code == t.Code
}
// 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 {
return fmt.Sprintf(
errorPrefixFmt+"invalid filter option %q: %v",
e.Code,
e.Option,
e.Err,
)
}
return fmt.Sprintf(errorPrefixFmt+"invalid filter option %q", e.Code, e.Option)
}
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 {
t, ok := target.(*FilterConfigError)
if !ok {
return false
}
return e.Code == t.Code
}
// ErrorCode returns the error code for programmatic matching.
func (e *FilterConfigError) 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 {
t, ok := target.(*SQLCConfigError)
if !ok {
return false
}
return e.Code == t.Code
}
// ErrorCode returns the error code for programmatic matching.
func (e *SQLCConfigError) ErrorCode() ErrorCode { return e.Code }