Repository navigation
Expand file tree
/
Copy patherrors.go
More file actions
263 lines (234 loc) · 7.13 KB
/
Copy patherrors.go
File metadata and controls
263 lines (234 loc) · 7.13 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
package guardy
import (
"context"
"errors"
"fmt"
)
// callbackCancellation checks context before interpreting a synchronous callback's result.
// A wrapped cancellation from a child operation remains a fault even with a live parent.
func callbackCancellation(ctx context.Context, callbackErr error) error {
if err := ctx.Err(); err != nil {
if callbackErr != nil {
return errors.Join(err, callbackErr)
}
return err
}
if errors.Is(callbackErr, context.Canceled) || errors.Is(callbackErr, context.DeadlineExceeded) {
return callbackErr
}
return nil
}
// cancellationOnly prevents sibling cancellation from hiding an independently failed check.
func cancellationOnly(err error) bool {
// A nested pipeline marks its cancellation as a fault for its own caller.
// The category marker is not an independent failure of this sibling check.
//nolint:errorlint // Inspect this node only; errors.As could skip independent joined causes.
switch fault := err.(type) {
case *ValidatorPanicError:
// Panic is an independent failure even if its value unwraps to cancellation.
return false
case *ValidatorFaultError:
return cancellationOnly(fault.Failure.Cause)
}
if joined, ok := err.(interface{ Unwrap() []error }); ok {
for _, cause := range joined.Unwrap() {
if !cancellationOnly(cause) {
return false
}
}
return len(joined.Unwrap()) > 0
}
if wrapped, ok := err.(interface{ Unwrap() error }); ok {
return cancellationOnly(wrapped.Unwrap())
}
return errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded)
}
var (
// ErrBlocked is returned when the pipeline result is Block (e.g. by StreamProcessor, WrapInput, WrapOutput).
// Match with [errors.Is] against err and ErrBlocked for quick checks; use [errors.As] into [*PolicyFailure] for routing.
ErrBlocked = errors.New("guardy: input blocked")
// ErrRetryRequested is returned when the pipeline result is Retry; the orchestrator should retry with Feedback.
// Match with [errors.Is] against err and ErrRetryRequested for quick checks; use [errors.As] into [*PolicyFailure] for routing.
ErrRetryRequested = errors.New("guardy: retry requested")
// ErrValidatorFailed is returned when a validator returns a system error.
// Use [errors.As] into [*PolicyFailure] for the canonical system-fault decision.
ErrValidatorFailed = errors.New("guardy: validator failed")
)
// BlockError carries a terminal policy failure from WrapInput or WrapOutput.
type BlockError struct {
Message string
Failure PolicyFailure
report Report
}
// Error implements error.
func (e *BlockError) Error() string {
if e == nil {
return ErrBlocked.Error()
}
if e.Message != "" {
return fmt.Sprintf("%s: %s", ErrBlocked.Error(), e.Message)
}
return ErrBlocked.Error()
}
// Unwrap returns ErrBlocked so [errors.Is] matches *BlockError.
func (e *BlockError) Unwrap() error {
return ErrBlocked
}
// As exposes the canonical policy failure contract.
func (e *BlockError) As(target any) bool {
if e == nil {
return false
}
return asPolicyFailure(target, &e.Failure)
}
// ReportSnapshot returns a validator report snapshot for telemetry.
func (e *BlockError) ReportSnapshot() Report {
if e == nil {
return Report{}
}
return e.report
}
// RetryError carries a retryable policy failure from WrapInput, WrapOutput, or typed argument validation.
type RetryError struct {
Feedback string
Failure PolicyFailure
report Report
}
// Error implements error without exposing correction feedback.
func (e *RetryError) Error() string {
return ErrRetryRequested.Error()
}
// Unwrap returns ErrRetryRequested so [errors.Is] matches *RetryError when the second argument is ErrRetryRequested.
func (e *RetryError) Unwrap() error {
return ErrRetryRequested
}
// As exposes the canonical policy failure contract.
func (e *RetryError) As(target any) bool {
if e == nil {
return false
}
return asPolicyFailure(target, &e.Failure)
}
// ReportSnapshot returns a validator report snapshot for telemetry.
func (e *RetryError) ReportSnapshot() Report {
if e == nil {
return Report{}
}
return e.report
}
// ValidatorFaultError carries system-fault metadata when a validator or pipeline infrastructure fails.
type ValidatorFaultError struct {
Cause error
Failure PolicyFailure
report Report
}
// Error implements error without exposing diagnostic cause text.
func (e *ValidatorFaultError) Error() string {
return ErrValidatorFailed.Error()
}
// Unwrap preserves both the fault category and its original typed cause.
func (e *ValidatorFaultError) Unwrap() error {
return errors.Join(ErrValidatorFailed, e.Failure.Cause)
}
// As exposes the canonical policy failure contract.
func (e *ValidatorFaultError) As(target any) bool {
if e == nil {
return false
}
return asPolicyFailure(target, &e.Failure)
}
// ReportSnapshot returns a validator report snapshot for telemetry.
func (e *ValidatorFaultError) ReportSnapshot() Report {
if e == nil {
return Report{}
}
return e.report
}
func asPolicyFailure(target any, failure *PolicyFailure) bool {
pf, ok := target.(**PolicyFailure)
if !ok {
return false
}
*pf = failure
return true
}
func blockErrorFromReport(rep *Report) error {
if rep == nil {
return ErrBlocked
}
normalized := normalizeReport(rep)
cloned := &normalized
return &BlockError{
Message: cloned.PublicMessage(),
Failure: *policyFailureFromReport(cloned, ErrBlocked),
report: *cloned,
}
}
// errorFromDecision maps a pipeline decision to BlockError, RetryError, or nil (pass/redact).
// Control flow uses Disposition, not Action.
func errorFromDecision(rep *Report) error {
if rep == nil {
return blockErrorFromReport(rep)
}
if rep.IsSystemFault() {
cloned := rep.Clone()
return &ValidatorFaultError{
Cause: ErrValidatorFailed,
Failure: *policyFailureFromReport(cloned, ErrValidatorFailed),
report: *cloned,
}
}
if rep.IsRetryableCorrection() {
return retryErrorFromReport(rep)
}
if rep.IsTerminalDeny() {
return blockErrorFromReport(rep)
}
if rep.Action == ActionPass || rep.Action == ActionRedact {
return nil
}
return fmt.Errorf(
"%w: unsupported pipeline action %s",
ErrValidatorFailed,
rep.Action.String(),
)
}
func validatorFaultReport(cause error) Report {
reason := "validator failed"
if cause != nil {
reason = cause.Error()
}
return Report{
Validator: "pipeline",
Code: CodeValidatorFailed,
Reason: reason,
Disposition: DispositionSystemFault,
}
}
func validatorFaultError(cause error) error {
report := validatorFaultReport(cause)
return validatorFaultErrorFromReport(&report, cause)
}
func validatorFaultErrorFromReport(report *Report, cause error) error {
normalized := normalizeReport(report)
return &ValidatorFaultError{
Cause: cause,
Failure: *policyFailureFromReport(&normalized, causeOrDefault(cause, ErrValidatorFailed)),
report: normalized,
}
}
func retryErrorFromReport(rep *Report) error {
normalized := normalizeReport(rep)
cloned := &normalized
return &RetryError{
Feedback: cloned.OrchestratorMessage(),
Failure: *policyFailureFromReport(cloned, ErrRetryRequested),
report: *cloned,
}
}
func causeOrDefault(cause error, fallback error) error {
if cause != nil {
return cause
}
return fallback
}