-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherrors.go
More file actions
643 lines (513 loc) · 17.1 KB
/
errors.go
File metadata and controls
643 lines (513 loc) · 17.1 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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
package core
import (
"bytes"
"database/sql"
"encoding/json"
"errors"
"fmt"
"net"
"os"
"path/filepath"
"runtime"
"slices"
"strings"
"syscall"
"github.com/actionforge/actrun-cli/utils"
"github.com/fatih/color"
)
var (
errEmoji = "❌"
hintEmoji = "💡"
stackEmoji = "🛠️"
numberEmoji = "🔢"
errorColor = color.New(color.FgRed).SprintFunc()
hintColor = color.New(color.FgYellow).SprintFunc()
contextColor = color.New(color.FgCyan).SprintFunc()
stackTraceColor = color.New(color.FgMagenta).SprintFunc()
bold = color.New(color.Bold).SprintFunc()
customErrNoOutputValue = &ErrNoOutputValue{}
customErrNoInputValue = &ErrNoInputValue{}
)
const HINT_INTERNAL_ERROR = "This is an internal error. Please report it via email or a GitHub issue."
type ErrNoOutputValue struct {
Message string
}
type ErrNoInputValue struct {
Message string
}
type CauseError struct {
Message string
}
func (e *CauseError) Error() string {
return e.Message
}
type LeafError struct {
Message string
GoStack []uintptr
ErrorStack []error
Cause error
Context *ExecutionState
Hint string
}
func (e *LeafError) Error() string {
return e.Message
}
func (e *LeafError) ErrorWithCauses() string {
var lines []string
// top level error (no prefix)
// iterate backwards for high-level first
for i := len(e.ErrorStack) - 1; i >= 0; i-- {
prefix := ""
if len(lines) > 0 {
// add indentation based on depth
prefix = strings.Repeat(" ", len(lines)) + "↳ "
}
lines = append(lines, prefix+e.ErrorStack[i].Error())
}
// leaf message
if e.Message != "" {
prefix := ""
if len(lines) > 0 {
prefix = strings.Repeat(" ", len(lines)) + "↳ "
}
lines = append(lines, prefix+e.Message)
}
// root cause
if e.Cause != nil {
causeMsg := e.Cause.Error()
if causeMsg != "" {
p := strings.Repeat(" ", len(lines)) + "↳ "
lines = append(lines, p+e.Cause.Error())
}
}
return strings.Join(lines, "\n")
}
func (e *LeafError) Unwrap() error {
return e.Cause
}
func (e *LeafError) SetHint(hint string, formatArgs ...any) *LeafError {
e.Hint = fmt.Sprintf(hint, formatArgs...)
return e
}
func CreateErr(c *ExecutionState, cause error, formatAndArgs ...any) *LeafError {
var (
message string
leafError *LeafError
)
if len(formatAndArgs) > 0 {
format, args := formatAndArgs[0].(string), formatAndArgs[1:]
message = fmt.Sprintf(format, args...)
}
// if cause is a LeafError or contains a LeafError errors.As will
// recursively unwrap the cause error and loo for a LeafError.
// If found, it assigns it to 'leafError' and returns true.
if cause != nil && errors.As(cause, &leafError) {
// found an existing LeafError, append the new message to its stack.
// also note, we preserve the original stack trace and root cause.
leafError.ErrorStack = append(leafError.ErrorStack, &CauseError{
Message: message,
})
// unlikely but in case the original error
// has no context add the one if we have one now
if leafError.Context == nil {
leafError.Context = c
}
} else {
// else case here means no cause or cause is a non leaf error.
// it also contains no LeafError, so now we need to create it
stack := make([]uintptr, 64)
leafError = &LeafError{
GoStack: stack[:runtime.Callers(2, stack)],
Message: message,
Context: c,
Cause: cause,
ErrorStack: make([]error, 0),
}
}
return leafError
}
func indentString(input string, indentSpaces int, numbering bool) string {
if input == "" {
return ""
}
lines := strings.Split(input, "\n")
indent := strings.Repeat(" ", indentSpaces)
const numberWidth = 2
for i, line := range lines {
if numbering {
if strings.HasPrefix(line, " ") || strings.HasPrefix(line, "\t") {
lines[i] = indent + strings.Repeat(" ", numberWidth) + " " + line
} else {
lines[i] = fmt.Sprintf("%s%*d: %s", indent, numberWidth, i+1, line)
}
} else {
lines[i] = indent + line
}
}
return strings.Join(lines, "\n")
}
const (
// web-safe hex equivalents for the frontend
WebColorRed = "#FF5555" // for color.FgRed
WebColorYellow = "#FFFF55" // for color.FgYellow
WebColorCyan = "#8BE9FD" // for color.FgCyan
WebColorMagenta = "#FF79C6" // for color.FgMagenta
)
type CtxMsg struct {
Message string `json:"msg"`
Depth int `json:"level"`
FullPath string `json:"fullPath"`
}
type WebErrorPayload struct {
IsLeafError bool `json:"isLeafError"`
Context []CtxMsg `json:"context,omitempty"`
Error string `json:"error"`
ErrorColor string `json:"errorColor"`
Hint string `json:"hint,omitempty"`
HintColor string `json:"hintColor,omitempty"`
StackTrace []string `json:"stackTrace,omitempty"`
}
func (e *LeafError) Format(f fmt.State, c rune) {
switch c {
case 'v':
// Below is the JSON output formatting for the web editor console
if f.Flag('#') {
payload := WebErrorPayload{
IsLeafError: true,
Error: e.ErrorWithCauses(),
ErrorColor: "#FF5555",
}
if e.Context != nil && len(e.Context.Visited) > 0 {
var previousNode NodeBaseInterface
for _, item := range e.Context.Visited {
currentNode := item.Node
// TODO: (Seb) improve this
// In the logs, group nodes appear twice, once when entered, and once
// when the group-outputs node comes back and executes the group node again.
// While this is expected for the execution flow, we don't want to have that
// in the logs, it looks very confusing.
if previousNode != nil &&
strings.HasPrefix(previousNode.GetNodeTypeId(), "core/group-outputs@") &&
strings.HasPrefix(currentNode.GetNodeTypeId(), "core/group@") {
previousNode = currentNode
continue
}
nodeNameOrLabel := currentNode.GetLabel()
if nodeNameOrLabel == "" {
nodeNameOrLabel = currentNode.GetName()
}
var msg string
if item.Execute {
msg = fmt.Sprintf("execute '%s'", nodeNameOrLabel)
} else {
msg = fmt.Sprintf("request input from '%s'", nodeNameOrLabel)
}
payload.Context = append(payload.Context, CtxMsg{
Message: msg,
Depth: strings.Count(currentNode.GetFullPath(), "/") + 1,
FullPath: currentNode.GetFullPath(),
})
previousNode = item.Node
}
}
payload.Hint = getErrorHint(e)
if payload.Hint != "" {
payload.HintColor = "#FFFF55"
}
if f.Flag('+') {
rawStack := e.StackTrace()
payload.StackTrace = strings.Split(rawStack, "\n")
}
jsonBytes, err := json.Marshal(payload)
if err != nil {
// Fallback in case of JSON error
fmt.Fprint(f, e.Error())
return
}
fmt.Fprint(f, string(jsonBytes))
return
}
// This below is the regular terminal output formatting
var (
tmpErrEmoji string
tmpHintEmoji string
tmpStackEmoji string
)
if !color.NoColor {
tmpErrEmoji = errEmoji + " "
tmpHintEmoji = hintEmoji + " "
tmpStackEmoji = stackEmoji + " "
}
var output string
// print ❌ error
if e.Context != nil && len(e.Context.Visited) > 0 {
var coloredLines []string
for _, item := range e.Context.Visited {
var msg string
if item.Execute {
msg = fmt.Sprintf("execute '%s' (%s)", item.Node.GetName(), item.Node.GetId())
} else {
msg = fmt.Sprintf("request input from '%s' (%s)", item.Node.GetName(), item.NodeID)
}
coloredLines = append(coloredLines, contextColor(msg))
}
callstackBody := strings.Join(coloredLines, "\n")
callstackBlock := indentString(callstackBody, 2, true)
output += fmt.Sprintf("%s%s\n%s\n", tmpErrEmoji, bold("error:"), callstackBlock)
errorBlock := indentString(e.ErrorWithCauses(), 6, false)
output += fmt.Sprintf("%s\n\n", errorBlock)
} else {
// if error block is printed without prior context, then enumerate lines
errorBlock := indentString(e.ErrorWithCauses(), 2, true)
output += fmt.Sprintf("%s%s\n%s", tmpErrEmoji, bold("error:"), errorColor(errorBlock))
}
// print 💡 hint
hint := indentString(getErrorHint(e), 2, false)
if hint != "" {
output += fmt.Sprintf("\n\n%s%s\n%s", tmpHintEmoji, bold("hint:"), hintColor(hint))
}
// print 🛠️ stack trace
if f.Flag('+') {
rawStack := e.StackTrace()
lines := strings.Split(rawStack, "\n")
var coloredLines []string
for _, line := range lines {
coloredLines = append(coloredLines, stackTraceColor(line))
}
output += fmt.Sprintf("\n\n%s%s\n%s",
tmpStackEmoji,
stackTraceColor(bold("stack trace:")),
strings.Join(coloredLines, "\n"),
)
}
fmt.Fprint(f, output)
return
case 's':
fmt.Fprint(f, e.Error())
}
}
func (e *LeafError) StackTrace() string {
return GetStacktrace(e.GoStack)
}
func (e *LeafError) Is(target error) bool {
_, ok := target.(*LeafError)
return ok
}
func (e *CauseError) Is(target error) bool {
_, ok := target.(*CauseError)
return ok
}
func (e *ErrNoInputValue) Is(target error) bool {
_, ok := target.(*ErrNoInputValue)
return ok
}
func (e *ErrNoOutputValue) Is(target error) bool {
_, ok := target.(*ErrNoOutputValue)
return ok
}
func (e *ErrNoOutputValue) GetMessage() string {
return e.Message
}
func (m *ErrNoOutputValue) Error() string {
return m.Message
}
func (e *ErrNoInputValue) GetMessage() string {
return e.Message
}
func (m *ErrNoInputValue) Error() string {
return m.Message
}
func isCombinedError(err error) bool {
joinedError, ok := err.(interface {
Unwrap() []error
})
if !ok {
return false
}
return len(joinedError.Unwrap()) > 0
}
type errorIterator struct {
err error
idx int
}
func iterateCombinedError(err error) <-chan errorIterator {
joinedError := err.(interface {
Unwrap() []error
})
ch := make(chan errorIterator)
go func() {
defer close(ch)
for i, e := range joinedError.Unwrap() {
ch <- errorIterator{err: e, idx: i}
}
}()
return ch
}
func PrintError(graphFile string, err error) {
if isCombinedError(err) {
for e := range iterateCombinedError(err) {
printError(graphFile, e.err, e.idx)
}
return
}
printError(graphFile, err, -1)
}
func printError(graphFile string, err error, index int) {
output := ""
if index >= 0 {
output += fmt.Sprintf("%s %s\n %s\n\n", numberEmoji, bold("concurrent error index:"), fmt.Sprintf("%d", index))
}
switch utils.GetLogLevel() {
case utils.LogLevelNormal:
output += fmt.Sprintf("%v\n", err)
default: // debug or verbose is fully detailed
output += fmt.Sprintf("%+v\n", err)
}
if graphFile != "" {
utils.LogErr.Errorf("actrun: %s\n\n", graphFile)
}
utils.LogErr.Error(output)
}
func getErrorHint(leafError *LeafError) string {
if leafError == nil {
return "No error."
}
if leafError.Hint != "" {
return leafError.Hint
}
err := leafError.Cause
if err == nil {
return ""
}
switch {
case errors.Is(err, customErrNoOutputValue):
// check if the node that hasn't provided any values, has been executed before
if leafError.Context != nil || len(leafError.Context.Visited) > 0 {
lastVisited := leafError.Context.Visited[len(leafError.Context.Visited)-1]
if lastVisited.Node.IsExecutionNode() {
nodeWasExecuted := false
visits := slices.Clone(leafError.Context.Visited)
slices.Reverse(visits)
for _, visit := range visits {
if visit.Node.GetId() == lastVisited.Node.GetId() && visit.Execute {
nodeWasExecuted = true
break
}
}
if !nodeWasExecuted {
return fmt.Sprintf("The node '%s' (%s) needs to be executed before it can provide values. It appears this node hasn't been executed yet, or its execution input is not connected.", lastVisited.Node.GetName(), lastVisited.Node.GetId())
}
}
return fmt.Sprintf("No output value provided. Check the settings of '%s' (%s) node", lastVisited.Node.GetName(), lastVisited.Node.GetId())
}
// should never happen here
return ""
case errors.Is(err, customErrNoInputValue):
return "No input value provided. Set a value or connect the input with a node"
// OS errors
case errors.Is(err, os.ErrNotExist):
return "The specified file or directory does not exist. Check the path and try again."
case errors.Is(err, os.ErrPermission):
return "You do not have the necessary permissions to perform this action. Try running the command with elevated privileges."
case errors.Is(err, os.ErrExist):
return "The file or directory already exists. Consider renaming or removing the existing one."
case errors.Is(err, os.ErrClosed):
return "The file is closed. Ensure the file is open before performing this action."
// Database errors
case errors.Is(err, sql.ErrNoRows):
return "No matching records found in the database. Verify your query or ensure the data exists."
case errors.Is(err, sql.ErrConnDone):
return "The database connection is closed. Check your database connection settings."
case errors.Is(err, sql.ErrTxDone):
return "The transaction has already been committed or rolled back. Ensure you're using a valid transaction."
// Network errors
case errors.Is(err, net.ErrClosed):
return "The network connection is closed. Verify your network settings and try reconnecting."
case errors.Is(err, syscall.ECONNREFUSED):
return "Connection refused. Ensure the server is running and accepting connections."
case errors.Is(err, syscall.ECONNRESET):
return "Connection reset by peer. The remote server might be down. Try reconnecting later."
case errors.Is(err, syscall.ETIMEDOUT):
return "Connection timed out. Check your network connection and try again."
case errors.Is(err, syscall.EADDRINUSE):
return "Address already in use. Ensure the address/port is not being used by another application."
case errors.Is(err, syscall.EHOSTUNREACH):
return "Host unreachable. Verify the network configuration and try again."
// File errors
case errors.Is(err, syscall.EIO):
return "Input/output error. Check the device or file system for issues."
case errors.Is(err, syscall.ENOSPC):
return "No space left on device. Free up some space and try again."
case errors.Is(err, syscall.ENOTDIR):
return "A component of the path is not a directory. Check the path and try again."
case errors.Is(err, syscall.EISDIR):
return "The specified path is a directory, not a file. Provide a valid file path."
case errors.Is(err, syscall.ENOTEMPTY):
return "The directory is not empty. Ensure the directory is empty before performing this action."
case errors.Is(err, syscall.EINVAL):
return "Invalid argument. Check the inputs and try again."
case errors.Is(err, syscall.EPIPE):
return "Broken pipe. The connection was closed unexpectedly."
// Network address errors
case errors.Is(err, net.UnknownNetworkError("tcp")):
return "Unknown network. Ensure the network type is correct."
case errors.Is(err, net.InvalidAddrError("example")):
return "Invalid address. Verify the address format and try again."
// Authentication errors
case strings.Contains(err.Error(), "authentication failed"):
return "Authentication failed. Verify your credentials and try again."
case strings.Contains(err.Error(), "authorization failed"):
return "Authorization failed. Ensure you have the necessary permissions to perform this action."
// Parsing errors
case strings.Contains(err.Error(), "syntax error"):
return "Syntax error. Check the syntax of your input and try again."
case strings.Contains(err.Error(), "parsing error"):
return "Parsing error. Verify the input format and try again."
// Generic errors
case strings.Contains(err.Error(), "timeout"):
return "Operation timed out. Check your network connection or server status."
case strings.Contains(err.Error(), "connection refused"):
return "Connection refused. Ensure the server is running and accepting connections."
case strings.Contains(err.Error(), "no such file or directory"):
return "The specified file or directory does not exist. Check the path and try again."
}
return ""
}
func GetStacktrace(stack []uintptr) string {
var buffer bytes.Buffer
frames := runtime.CallersFrames(stack)
for {
frame, more := frames.Next()
file := frame.File
if IsTestE2eRunning() {
if strings.Contains(strings.ToLower(file), "go/") {
// Some tests print the stack trace, and we need
// to replace the path with a placeholder to make
// the tests deterministic on all platforms.
file = strings.ReplaceAll(file, "amd64", "{..}")
file = strings.ReplaceAll(file, "arm64", "{..}")
file = strings.ReplaceAll(file, "x86", "{..}")
file = strings.ReplaceAll(file, "x64", "{..}")
file = strings.ReplaceAll(file, "x86_64", "{..}")
file = strings.ReplaceAll(file, "darwin", "{..}")
file = strings.ReplaceAll(file, "linux", "{..}")
file = strings.ReplaceAll(file, "windows", "{..}")
file = strings.ReplaceAll(file, "win", "{..}")
file = strings.ReplaceAll(file, "win32", "{..}")
file = strings.ReplaceAll(file, "win64", "{..}")
file = strings.ReplaceAll(file, "libexec", "{..}")
// external dependencies have different lines for different platforms
// - .../go/1.22.4/{..}/src/runtime/asm_{..}.s:1222
// + .../go/1.22.4/{..}/src/runtime/asm_{..}.s:1695
frame.Line = -1
}
// For e2e tests, we need a deterministic stack trace
file = filepath.Base(file)
}
buffer.WriteString(fmt.Sprintf("%s\n\t%s:%d\n", frame.Function, file, frame.Line))
if !more {
break
}
}
return buffer.String()
}