Repository navigation
Expand file tree
/
Copy patherrors.go
More file actions
260 lines (203 loc) · 7.21 KB
/
Copy patherrors.go
File metadata and controls
260 lines (203 loc) · 7.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
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
package derrors
import (
"errors"
"fmt"
)
// Code is a domain-specific error code.
type Code string
// Error codes for different failure scenarios.
const (
// CodeAccessibilityDenied indicates accessibility permissions are not granted.
CodeAccessibilityDenied Code = "ACCESSIBILITY_DENIED"
// CodeAccessibilityFailed indicates an accessibility API call failed.
CodeAccessibilityFailed Code = "ACCESSIBILITY_FAILED"
// CodeElementNotFound indicates a UI element could not be found.
CodeElementNotFound Code = "ELEMENT_NOT_FOUND"
// CodeInvalidConfig indicates configuration validation failed.
CodeInvalidConfig Code = "INVALID_CONFIG"
// CodeInvalidInput indicates invalid input parameters.
CodeInvalidInput Code = "INVALID_INPUT"
// CodeIPCFailed indicates IPC communication failed.
CodeIPCFailed Code = "IPC_FAILED"
// CodeIPCAlreadyRunning indicates the IPC server is already running.
CodeIPCAlreadyRunning Code = "IPC_ALREADY_RUNNING"
// CodeIPCServerNotRunning indicates the IPC server is not running.
CodeIPCServerNotRunning Code = "IPC_SERVER_NOT_RUNNING"
// CodeOverlayFailed indicates overlay rendering failed.
CodeOverlayFailed Code = "OVERLAY_FAILED"
// CodeHintGenerationFailed indicates hint generation failed.
CodeHintGenerationFailed Code = "HINT_GENERATION_FAILED"
// CodeActionFailed indicates an action execution failed.
CodeActionFailed Code = "ACTION_FAILED"
// CodeContextCanceled indicates the operation was canceled.
CodeContextCanceled Code = "CONTEXT_CANCELED"
// CodeTimeout indicates the operation timed out.
CodeTimeout Code = "TIMEOUT"
// CodeInternal indicates an internal error occurred.
CodeInternal Code = "INTERNAL"
// CodeLoggingFailed indicates logger initialization or I/O failed.
CodeLoggingFailed Code = "LOGGING_FAILED"
// CodeConfigIOFailed indicates configuration file I/O failed.
CodeConfigIOFailed Code = "CONFIG_IO_FAILED"
// CodeVersionMismatch indicates a CLI/daemon build version mismatch.
CodeVersionMismatch Code = "VERSION_MISMATCH"
// CodeHotkeyRegisterFailed indicates a hotkey registration error.
CodeHotkeyRegisterFailed Code = "HOTKEY_REGISTER_FAILED"
// CodeExecFailed indicates a shell execution error.
CodeExecFailed Code = "EXEC_FAILED"
// CodeSerializationFailed indicates JSON/TOML serialization/deserialization failed.
CodeSerializationFailed Code = "SERIALIZATION_FAILED"
// CodeBridgeFailed indicates a failure in native bridge interactions.
CodeBridgeFailed Code = "BRIDGE_FAILED"
// CodeSecureInputEnabled indicates secure input mode is active on macOS.
CodeSecureInputEnabled Code = "SECURE_INPUT_ENABLED"
// CodeNotSupported indicates the operation is not supported on the current platform.
// Use this in stub implementations so callers can distinguish "not implemented yet"
// from "actually failed". Example:
//
// func (s *SystemAdapter) ScreenBounds(ctx context.Context) (image.Rectangle, error) {
// return image.Rectangle{}, derrors.New(derrors.CodeNotSupported,
// "screen bounds not yet implemented on linux")
// }
CodeNotSupported Code = "NOT_SUPPORTED"
// CodeChainBail indicates a chain action should abort (e.g. user canceled
// a mode without making a selection).
CodeChainBail Code = "CHAIN_BAIL"
)
// Error is a domain error with code, message, and optional cause.
type Error struct {
code Code
message string
cause error
context map[string]any
}
// New creates a new domain error with the given code and message.
func New(code Code, message string) *Error {
return &Error{
code: code,
message: message,
}
}
// Newf creates a new domain error with formatted message.
func Newf(code Code, format string, args ...any) *Error {
return &Error{
code: code,
message: fmt.Sprintf(format, args...),
}
}
// Code returns the error code.
func (e *Error) Code() Code {
return e.code
}
// Message returns the error message.
func (e *Error) Message() string {
return e.message
}
// Cause returns the underlying cause error.
func (e *Error) Cause() error {
return e.cause
}
// Context returns the error context.
func (e *Error) Context() map[string]any {
return e.context
}
// Error implements the error interface.
func (e *Error) Error() string {
if e.cause != nil {
return fmt.Sprintf("[%s] %s: %v", e.code, e.message, e.cause)
}
return fmt.Sprintf("[%s] %s", e.code, e.message)
}
// Unwrap returns the underlying cause error.
func (e *Error) Unwrap() error {
return e.cause
}
// Is implements error matching for errors.Is.
func (e *Error) Is(target error) bool {
targetError, ok := target.(*Error)
if !ok {
return false
}
return e.code == targetError.code
}
// WithContext adds context information to the error.
func (e *Error) WithContext(key string, value any) *Error {
if e.context == nil {
e.context = make(map[string]any)
}
e.context[key] = value
return e
}
// Wrap wraps an existing error with a domain error code and message.
func Wrap(err error, code Code, message string) *Error {
if err == nil {
return nil
}
return &Error{
code: code,
message: message,
cause: err,
}
}
// Wrapf wraps an existing error with formatted message.
func Wrapf(err error, code Code, format string, args ...any) *Error {
if err == nil {
return nil
}
return &Error{
code: code,
message: fmt.Sprintf(format, args...),
cause: err,
}
}
// IsCode checks if an error has the specified error code.
func IsCode(err error, code Code) bool {
if domainErr, ok := errors.AsType[*Error](err); ok {
return domainErr.code == code
}
return false
}
// Message returns the sentence an error says, without the code a domain error
// carries alongside it.
//
// The code is for a caller to branch on and travels in whatever field the
// caller answers with; the sentence is what a person reads. Every door that
// shows an error to someone — an IPC response, a configuration warning —
// shows it through this, so the same fault reads the same wherever it surfaces.
func Message(err error) string {
if err == nil {
return ""
}
if domainErr, ok := errors.AsType[*Error](err); ok {
return domainErr.Message()
}
return err.Error()
}
// GetCode extracts the error code from an error, or returns CodeInternal if not a domain error.
func GetCode(err error) Code {
if domainErr, ok := errors.AsType[*Error](err); ok {
return domainErr.code
}
return CodeInternal
}
// Is is a helper function that checks if an error is of a specific type.
func Is(err error, target error) bool {
return errors.Is(err, target)
}
// IsAccessibilityError checks if an error is accessibility-related.
func IsAccessibilityError(err error) bool {
return IsCode(err, CodeAccessibilityDenied) || IsCode(err, CodeAccessibilityFailed)
}
// IsUserError checks if an error is due to user input/configuration.
func IsUserError(err error) bool {
return IsCode(err, CodeInvalidConfig) || IsCode(err, CodeInvalidInput)
}
// IsTransient checks if an error is potentially transient (retryable).
func IsTransient(err error) bool {
return IsCode(err, CodeTimeout) || IsCode(err, CodeIPCFailed)
}
// IsNotSupported checks if an error indicates an unimplemented platform feature.
// Use this in callers that should degrade gracefully when a feature is unavailable.
func IsNotSupported(err error) bool {
return IsCode(err, CodeNotSupported)
}