Repository navigation
Expand file tree
/
Copy pathrunerror.go
More file actions
117 lines (107 loc) · 3.4 KB
/
Copy pathrunerror.go
File metadata and controls
117 lines (107 loc) · 3.4 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
package starbox
import (
"errors"
"fmt"
"github.com/1set/starlet"
"go.starlark.net/resolve"
"go.starlark.net/starlark"
"go.starlark.net/syntax"
)
// RunErrorKind classifies why a Run/Call failed. The zero value
// RunErrorUnknown is intentional and fail-loud: an unrecognised error is never
// silently treated as a known kind.
type RunErrorKind int
const (
// RunErrorUnknown is an error that could not be classified.
RunErrorUnknown RunErrorKind = iota
// RunErrorSyntax is a parse failure (the script is not valid Starlark).
RunErrorSyntax
// RunErrorCompile is a resolve failure: an undefined name, a bad load
// binding, and similar pre-execution problems.
RunErrorCompile
// RunErrorModuleWithheld is load() of a real module the active set withholds.
RunErrorModuleWithheld
// RunErrorMaxSteps is the execution step budget being exceeded.
RunErrorMaxSteps
// RunErrorOutputLimit is the result exceeding the configured output limit.
RunErrorOutputLimit
// RunErrorEval is a runtime evaluation error (the script raised or failed
// while executing).
RunErrorEval
)
// String returns a short, stable name for the kind.
func (k RunErrorKind) String() string {
switch k {
case RunErrorSyntax:
return "syntax"
case RunErrorCompile:
return "compile"
case RunErrorModuleWithheld:
return "module_withheld"
case RunErrorMaxSteps:
return "max_steps"
case RunErrorOutputLimit:
return "output_limit"
case RunErrorEval:
return "eval"
default:
return "unknown"
}
}
// RunError classifies a failed run by Kind while preserving the original error
// chain: errors.As / errors.Is against the underlying typed errors
// (ModuleWithheldError, starlet.MaxStepsExceededError, OutputLimitExceededError,
// *starlark.EvalError, …) keep working through it via Unwrap.
type RunError struct {
Kind RunErrorKind
Err error // the underlying error
}
// Error implements error.
func (e *RunError) Error() string {
if e == nil || e.Err == nil {
return "run error: <nil>"
}
return fmt.Sprintf("run error [%s]: %v", e.Kind, e.Err)
}
// Unwrap returns the underlying error so the original chain stays reachable
// (errors.As / errors.Is pass through a *RunError unchanged).
func (e *RunError) Unwrap() error { return e.Err }
// ClassifyRunError wraps a Run/Call error in a *RunError tagged with its Kind,
// classifying by the most specific typed error first (a withheld-module or
// budget error also presents as a Starlark eval error, so those are matched
// before RunErrorEval). It returns nil for a nil error and preserves the
// original error via Unwrap, so callers can both switch on Kind and errors.As
// to the specific typed error for details.
func ClassifyRunError(err error) *RunError {
if err == nil {
return nil
}
return &RunError{Kind: classifyRunErrorKind(err), Err: err}
}
func classifyRunErrorKind(err error) RunErrorKind {
var withheld ModuleWithheldError
if errors.As(err, &withheld) {
return RunErrorModuleWithheld
}
var maxSteps starlet.MaxStepsExceededError
if errors.As(err, &maxSteps) {
return RunErrorMaxSteps
}
var outLimit OutputLimitExceededError
if errors.As(err, &outLimit) {
return RunErrorOutputLimit
}
var synErr syntax.Error
if errors.As(err, &synErr) {
return RunErrorSyntax
}
var resErr resolve.ErrorList
if errors.As(err, &resErr) {
return RunErrorCompile
}
var evalErr *starlark.EvalError
if errors.As(err, &evalErr) {
return RunErrorEval
}
return RunErrorUnknown
}