Repository navigation
Expand file tree
/
Copy patherrors.go
More file actions
198 lines (167 loc) · 4.85 KB
/
Copy patherrors.go
File metadata and controls
198 lines (167 loc) · 4.85 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
package voker
import (
"errors"
"fmt"
"log/slog"
"reflect"
"runtime"
"strings"
)
// ErrorResponse represents a Lambda function error response
type ErrorResponse struct {
Type string `json:"errorType"`
Message string `json:"errorMessage"`
StackTrace []StackFrame `json:"stackTrace,omitempty"`
fatal bool
}
// Error implements the error interface for ErrorResponse
func (e *ErrorResponse) Error() string {
return e.Message
}
// LogValue implements the slog.LogValuer interface for structured logging
func (e *ErrorResponse) LogValue() slog.Value {
attrs := []slog.Attr{
slog.String("errorType", e.Type),
slog.String("errorMessage", e.Message),
}
if len(e.StackTrace) > 0 {
frameValues := make([]any, len(e.StackTrace))
for i, frame := range e.StackTrace {
frameValues[i] = map[string]any{
"path": frame.Path,
"line": frame.Line,
"label": frame.Label,
}
}
attrs = append(attrs, slog.Any("stackTrace", frameValues))
}
return slog.GroupValue(attrs...)
}
// StackFrame represents a single frame in a stack trace
type StackFrame struct {
Path string `json:"path"`
Line int `json:"line"`
Label string `json:"label"`
}
// newErrorResponse creates an ErrorResponse from a regular error. A wrapped
// *ErrorResponse anywhere in the chain is preserved verbatim so its Type,
// StackTrace, and fatality survive fmt.Errorf("...: %w", err) wrapping.
func newErrorResponse(err error) *ErrorResponse {
if typed, ok := errors.AsType[*ErrorResponse](err); ok {
return typed
}
return &ErrorResponse{
Message: err.Error(),
Type: getErrorType(err),
}
}
// getErrorType returns the errorType reported for a handler error: the Go
// type name of the error. Errors without a useful name — anonymous types and
// the generic types produced by errors.New, fmt.Errorf, and errors.Join —
// report the stable name HandlerError instead. Handlers that need a specific
// errorType should return a *ErrorResponse.
func getErrorType(err error) string {
if err == nil {
return "HandlerError"
}
t := reflect.TypeOf(err)
if t == nil {
return "HandlerError"
}
if t.Kind() == reflect.Pointer {
t = t.Elem()
}
// The errors and fmt packages produce unexported generic types
// (errorString, wrapError, joinError, ...) whose names carry no meaning
// for users, so they collapse to HandlerError.
if t.PkgPath() == "errors" || t.PkgPath() == "fmt" {
return "HandlerError"
}
if name := t.Name(); name != "" {
return name
}
return "HandlerError"
}
// newPanicResponse creates an ErrorResponse from a panic
func newPanicResponse(panicValue any) *ErrorResponse {
message := fmt.Sprintf("%v", panicValue)
errorType := getPanicType(panicValue)
return &ErrorResponse{
Message: message,
Type: errorType,
StackTrace: captureStackTrace(),
fatal: true,
}
}
// getPanicType returns the panic type in AWS recommended format
func getPanicType(panicValue any) string {
if panicValue == nil {
return "Runtime.Panic"
}
t := reflect.TypeOf(panicValue)
typeName := t.Name()
if t.Kind() == reflect.Pointer && t.Elem().Name() != "" {
typeName = t.Elem().Name()
}
// If we have a type name, use it
if typeName != "" {
return "Runtime.Panic." + typeName
}
// For anonymous types, use the type string
typeStr := fmt.Sprintf("%T", panicValue)
// Clean up the type string (remove package paths)
if idx := strings.LastIndex(typeStr, "."); idx >= 0 {
typeStr = typeStr[idx+1:]
}
if typeStr != "" {
return "Runtime.Panic." + typeStr
}
return "Runtime.Panic"
}
// captureStackTrace captures the current stack trace, skipping voker internal frames
func captureStackTrace() []StackFrame {
const maxFrames = 32
const framesToSkip = 4 // captureStackTrace -> newPanicResponse -> recover -> handler
pcs := make([]uintptr, maxFrames)
n := runtime.Callers(framesToSkip, pcs)
if n == 0 {
return []StackFrame{}
}
frames := runtime.CallersFrames(pcs[:n])
var stackFrames []StackFrame
for {
frame, more := frames.Next()
stackFrames = append(stackFrames, formatFrame(frame))
if !more {
break
}
}
return stackFrames
}
// formatFrame converts a runtime.Frame to a StackFrame
func formatFrame(frame runtime.Frame) StackFrame {
path := frame.File
label := frame.Function
// Strip GOPATH/module path from file path
// Count slashes in function name to determine how many path components to keep
slashCount := strings.Count(label, "/")
if slashCount > 0 {
parts := strings.Split(path, "/")
if len(parts) > slashCount+1 {
path = strings.Join(parts[len(parts)-slashCount-1:], "/")
}
}
// Strip package path from function name
if idx := strings.LastIndex(label, "/"); idx >= 0 {
label = label[idx+1:]
}
// Strip package name, keeping only type and method
if idx := strings.Index(label, "."); idx >= 0 {
label = label[idx+1:]
}
return StackFrame{
Path: path,
Line: frame.Line,
Label: label,
}
}