-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstructured_error.go
More file actions
143 lines (124 loc) · 3.93 KB
/
Copy pathstructured_error.go
File metadata and controls
143 lines (124 loc) · 3.93 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
/*
Copyright 2025 BubuStack.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package sdk
import (
"encoding/json"
runsv1alpha1 "github.com/bubustack/bobrapet/api/runs/v1alpha1"
"github.com/bubustack/bobrapet/pkg/enums"
k8sruntime "k8s.io/apimachinery/pkg/runtime"
)
// StructuredErrorProvider allows errors to supply a versioned StructuredError payload.
type StructuredErrorProvider interface {
StructuredError() runsv1alpha1.StructuredError
}
type structuredError struct {
structured runsv1alpha1.StructuredError
cause error
}
// StructuredError returns the versioned error payload to attach to StepRun.status.error.
func (e *structuredError) StructuredError() runsv1alpha1.StructuredError {
return e.structured
}
// Error implements the error interface.
func (e *structuredError) Error() string {
if e == nil {
return "engram failed"
}
if e.structured.Message != "" {
return e.structured.Message
}
if e.cause != nil {
return e.cause.Error()
}
return "engram failed"
}
// Unwrap exposes the underlying cause.
func (e *structuredError) Unwrap() error {
if e == nil {
return nil
}
return e.cause
}
// StructuredErrorOption mutates a structured error before it is returned.
type StructuredErrorOption func(*structuredError)
// WithStructuredErrorRetryable annotates the error as retryable/terminal.
func WithStructuredErrorRetryable(retryable bool) StructuredErrorOption {
return func(e *structuredError) {
e.structured.Retryable = &retryable
}
}
// WithStructuredErrorExitClass sets the desired exit class ("retry", "terminal", etc.).
func WithStructuredErrorExitClass(exitClass enums.ExitClass) StructuredErrorOption {
return func(e *structuredError) {
e.structured.ExitClass = runsv1alpha1.StructuredErrorExitClass(exitClass)
}
}
// WithStructuredErrorCode sets a component-specific error code.
func WithStructuredErrorCode(code string) StructuredErrorOption {
return func(e *structuredError) {
e.structured.Code = code
}
}
// WithStructuredErrorDetails attaches structured metadata for diagnostics.
func WithStructuredErrorDetails(details map[string]any) StructuredErrorOption {
return func(e *structuredError) {
if details == nil {
e.structured.Details = nil
return
}
raw, err := json.Marshal(details)
if err != nil {
e.structured.Details = fallbackStructuredErrorDetails(details, err)
return
}
e.structured.Details = &k8sruntime.RawExtension{Raw: raw}
}
}
func fallbackStructuredErrorDetails(details map[string]any, marshalErr error) *k8sruntime.RawExtension {
fallback := map[string]any{
"unserializable": true,
"marshalError": marshalErr.Error(),
}
kind, summary := signalTypeSummary(details)
fallback["type"] = kind
if len(summary) > 0 {
fallback["details"] = summary
}
raw, err := json.Marshal(fallback)
if err != nil {
return nil
}
return &k8sruntime.RawExtension{Raw: raw}
}
// WithStructuredErrorCause preserves the underlying error for wrapping/unwrapping.
func WithStructuredErrorCause(cause error) StructuredErrorOption {
return func(e *structuredError) {
e.cause = cause
}
}
// NewStructuredError returns an error that carries a StructuredError payload.
func NewStructuredError(typ runsv1alpha1.StructuredErrorType, message string, opts ...StructuredErrorOption) error {
serr := &structuredError{
structured: runsv1alpha1.StructuredError{
Version: runsv1alpha1.StructuredErrorVersionV1,
Type: typ,
Message: message,
},
}
for _, opt := range opts {
if opt != nil {
opt(serr)
}
}
return serr
}