-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.go
More file actions
425 lines (369 loc) · 9.45 KB
/
Copy patherror.go
File metadata and controls
425 lines (369 loc) · 9.45 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
package lua
import (
"errors"
"fmt"
"runtime"
"strings"
"sync"
)
// StackFrame represents a single frame in the Lua stack (for error reporting).
type StackFrame struct {
Level int
Source string
CurrentLine int
Name string
FuncType string
}
func (sf StackFrame) String() string {
if sf.Name != "" {
return fmt.Sprintf("%s:%d (%s)", sf.Source, sf.CurrentLine, sf.Name)
}
return fmt.Sprintf("%s:%d", sf.Source, sf.CurrentLine)
}
// StackTrace represents a Lua stack trace.
type StackTrace struct {
ThreadID string
Frames []StackFrame
}
func (st StackTrace) String() string {
var sb strings.Builder
for _, frame := range st.Frames {
sb.WriteString(" ")
sb.WriteString(frame.String())
sb.WriteString("\n")
}
return sb.String()
}
// Kind categorizes errors semantically.
type Kind string
const (
Unknown Kind = ""
NotFound Kind = "NotFound"
AlreadyExists Kind = "AlreadyExists"
Invalid Kind = "Invalid"
PermissionDenied Kind = "PermissionDenied"
Unavailable Kind = "Unavailable"
Internal Kind = "Internal"
Canceled Kind = "Canceled"
Conflict Kind = "Conflict"
Timeout Kind = "Timeout"
RateLimited Kind = "RateLimited"
)
func (k Kind) String() string {
if k == "" {
return "Unknown"
}
return string(k)
}
// Ternary represents three-state logic for composable error handling.
type Ternary int8
const (
TernaryUnknown Ternary = 0
TernaryTrue Ternary = 1
TernaryFalse Ternary = 2
)
func (t Ternary) Bool() bool {
return t == TernaryTrue
}
func (t Ternary) String() string {
switch t {
case TernaryUnknown:
return "Unknown"
case TernaryTrue:
return "True"
case TernaryFalse:
return "False"
default:
return "Unknown"
}
}
// Error is the unified error type for go-lua.
// It implements both error and LValue interfaces.
// Behaves like a string for concatenation and tostring().
type Error struct {
Err error // Wrapped error (error chain)
Message string // Error message
LuaStack *StackTrace // Lua stack at wrap point
goStack []uintptr // Go stack at wrap point
Context string // Context description
kind Kind // Error category
retryable *bool // nil=unknown, true/false
details map[string]any // Structured metadata
}
// ErrorMetadata is portable metadata extracted from a Go error chain.
type ErrorMetadata struct {
Kind Kind
Retryable *bool
Details map[string]any
}
// ErrorMetadataExtractor extracts metadata from an error chain.
type ErrorMetadataExtractor func(err error) *ErrorMetadata
var (
errorMetadataExtractorMu sync.RWMutex
errorMetadataExtractor ErrorMetadataExtractor
errorMetadataExtractorOnce sync.Once
)
// ConfigureErrorMetadataExtractor sets the process-wide error metadata extractor once.
// Subsequent calls are ignored.
func ConfigureErrorMetadataExtractor(extractor ErrorMetadataExtractor) {
if extractor == nil {
return
}
errorMetadataExtractorOnce.Do(func() {
SetErrorMetadataExtractor(extractor)
})
}
// SetErrorMetadataExtractor overrides the process-wide extractor.
// This is primarily intended for tests.
func SetErrorMetadataExtractor(extractor ErrorMetadataExtractor) {
errorMetadataExtractorMu.Lock()
errorMetadataExtractor = extractor
errorMetadataExtractorMu.Unlock()
}
func currentErrorMetadataExtractor() ErrorMetadataExtractor {
errorMetadataExtractorMu.RLock()
extractor := errorMetadataExtractor
errorMetadataExtractorMu.RUnlock()
return extractor
}
// NewError creates a new error with the given message.
func NewError(message string) *Error {
e := &Error{
Message: message,
}
e.captureGoStack()
return e
}
// NewErrorf creates a new error with formatted message.
func NewErrorf(format string, args ...any) *Error {
return NewError(fmt.Sprintf(format, args...))
}
// WrapError wraps an existing Go error with context.
func WrapError(err error, context string) *Error {
return wrapError(err, context, currentErrorMetadataExtractor())
}
// WrapErrorWithMetadata wraps an error with an explicit metadata extractor.
func WrapErrorWithMetadata(err error, context string, extractor ErrorMetadataExtractor) *Error {
return wrapError(err, context, extractor)
}
func wrapError(err error, context string, extractor ErrorMetadataExtractor) *Error {
if err == nil {
return nil
}
e := &Error{
Err: err,
Message: err.Error(),
Context: context,
}
e.captureGoStack()
// Preserve metadata from wrapped *Error if it has any
var we *Error
if errors.As(err, &we) {
if we.kind != "" {
e.kind = we.kind
}
if we.retryable != nil {
b := *we.retryable
e.retryable = &b
}
if we.details != nil {
e.details = cloneDetails(we.details)
}
}
if extractor != nil && (e.kind == "" || e.retryable == nil || e.details == nil) {
if meta := extractor(err); meta != nil {
if e.kind == "" && meta.Kind != "" {
e.kind = meta.Kind
}
if e.retryable == nil && meta.Retryable != nil {
b := *meta.Retryable
e.retryable = &b
}
if e.details == nil && meta.Details != nil {
e.details = cloneDetails(meta.Details)
}
}
}
return e
}
func cloneDetails(m map[string]any) map[string]any {
if m == nil {
return nil
}
out := make(map[string]any, len(m))
for k, v := range m {
out[k] = v
}
return out
}
// WrapErrorWithLua wraps an error, captures Lua stack trace, and sets the error metatable.
func WrapErrorWithLua(l *LState, err error, context string) *Error {
e := wrapError(err, context, currentErrorMetadataExtractor())
if e != nil && l != nil {
e.LuaStack = captureStackTrace(l)
SetErrorMetatable(l, e)
}
return e
}
// captureStackTrace captures a Lua stack trace from an LState.
func captureStackTrace(l *LState) *StackTrace {
trace := &StackTrace{
ThreadID: fmt.Sprintf("%p", l),
}
for level := 0; ; level++ {
ar, ok := l.GetStack(level)
if !ok {
break
}
if _, err := l.GetInfo("nSluf", ar, nil); err != nil {
break
}
frame := StackFrame{
Level: level,
Source: ar.Source,
CurrentLine: ar.CurrentLine,
Name: ar.Name,
FuncType: ar.What,
}
trace.Frames = append(trace.Frames, frame)
}
return trace
}
func (e *Error) captureGoStack() {
const maxDepth = 32
e.goStack = make([]uintptr, maxDepth)
n := runtime.Callers(3, e.goStack) // skip Callers, captureGoStack, and caller
e.goStack = e.goStack[:n]
}
// Error implements the error interface.
func (e *Error) Error() string {
if e.Context != "" {
return e.Context + ": " + e.Message
}
return e.Message
}
// Type implements LValue - returns LTUserData.
func (e *Error) Type() LValueType {
return LTUserData
}
// String implements LValue - returns the message for tostring() and concat.
func (e *Error) String() string {
return e.Message
}
// Kind returns the error category.
func (e *Error) Kind() Kind {
if e.kind == "" {
return Unknown
}
return e.kind
}
// Retryable returns whether the operation should be retried.
func (e *Error) Retryable() Ternary {
if e.retryable == nil {
return TernaryUnknown
}
if *e.retryable {
return TernaryTrue
}
return TernaryFalse
}
// Details returns structured metadata about the error.
func (e *Error) Details() map[string]any {
return e.details
}
// Unwrap returns the underlying error for errors.Is/As support.
func (e *Error) Unwrap() error {
return e.Err
}
// GetError extracts a *Error from an error chain using errors.As.
// Returns nil if no *Error is found in the chain.
func GetError(err error) *Error {
var e *Error
if errors.As(err, &e) {
return e
}
return nil
}
// WithKind sets the error category.
func (e *Error) WithKind(k Kind) *Error {
e.kind = k
return e
}
// WithRetryable sets the retry status.
func (e *Error) WithRetryable(r bool) *Error {
e.retryable = &r
return e
}
// WithDetails sets structured metadata.
func (e *Error) WithDetails(d map[string]any) *Error {
e.details = d
return e
}
// WithContext adds context description.
func (e *Error) WithContext(ctx string) *Error {
e.Context = ctx
return e
}
// Stack returns a formatted stack trace including both Lua and Go stacks.
func (e *Error) Stack() string {
var sb strings.Builder
// Walk error chain
current := e
for current != nil {
if current.Context != "" {
sb.WriteString(current.Context)
sb.WriteString("\n")
}
// Lua stack
if current.LuaStack != nil && len(current.LuaStack.Frames) > 0 {
for _, frame := range current.LuaStack.Frames {
_, _ = fmt.Fprintf(&sb, " %s:%d (%s)\n", frame.Source, frame.CurrentLine, frame.Name)
}
}
// Go stack
if len(current.goStack) > 0 {
frames := runtime.CallersFrames(current.goStack)
for {
frame, more := frames.Next()
// Skip runtime internals
if !strings.Contains(frame.File, "runtime/") {
_, _ = fmt.Fprintf(&sb, " %s:%d (%s)\n", frame.File, frame.Line, frame.Function)
}
if !more {
break
}
}
}
// Move to wrapped error
var we *Error
if errors.As(current.Err, &we) {
current = we
} else {
break
}
}
return sb.String()
}
// IsErrorKind checks if an error matches a specific kind.
func IsErrorKind(err error, kind Kind) bool {
var e *Error
if errors.As(err, &e) {
return e.Kind() == kind
}
return false
}
// GetErrorKind extracts the kind from an error.
func GetErrorKind(err error) Kind {
var e *Error
if errors.As(err, &e) {
return e.Kind()
}
return Unknown
}
// AsError extracts an Error from an LValue if possible.
func AsError(v LValue) (*Error, bool) {
if e, ok := v.(*Error); ok {
return e, true
}
return nil, false
}