-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
181 lines (157 loc) · 4.58 KB
/
Copy patherrors.go
File metadata and controls
181 lines (157 loc) · 4.58 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
package e2b
import (
"errors"
"fmt"
)
// Sentinel errors for common error conditions.
var (
// ErrTimeout indicates that the code execution timed out.
ErrTimeout = errors.New("e2b: execution timeout")
// ErrRequestTimeout indicates that the HTTP request timed out.
ErrRequestTimeout = errors.New("e2b: request timeout")
// ErrNotFound indicates that a resource was not found.
ErrNotFound = errors.New("e2b: resource not found")
// ErrInvalidArgument indicates an invalid argument was provided.
ErrInvalidArgument = errors.New("e2b: invalid argument")
// ErrSandboxClosed indicates the sandbox has been closed.
ErrSandboxClosed = errors.New("e2b: sandbox is closed")
// ErrRateLimit indicates that the rate limit has been exceeded.
ErrRateLimit = errors.New("e2b: rate limit exceeded")
// ErrAuthentication indicates an authentication failure.
ErrAuthentication = errors.New("e2b: authentication error")
// ErrNotEnoughSpace indicates insufficient disk space in the sandbox.
ErrNotEnoughSpace = errors.New("e2b: not enough disk space")
)
// SandboxError represents an error returned by the sandbox API.
type SandboxError struct {
// StatusCode is the HTTP status code.
StatusCode int
// Message is the error message.
Message string
// Err is the underlying error, if any.
Err error
}
// Error implements the error interface.
func (e *SandboxError) Error() string {
if e.Err != nil {
return fmt.Sprintf("sandbox error status %d, %s: %v", e.StatusCode, e.Message, e.Err)
}
return fmt.Sprintf("sandbox error status %d, %s", e.StatusCode, e.Message)
}
// Unwrap returns the underlying error.
func (e *SandboxError) Unwrap() error {
return e.Err
}
// Is checks if the error matches the target.
func (e *SandboxError) Is(target error) bool {
switch {
case target == ErrInvalidArgument && e.StatusCode == 400:
return true
case target == ErrAuthentication && e.StatusCode == 401:
return true
case target == ErrNotFound && e.StatusCode == 404:
return true
case target == ErrRateLimit && e.StatusCode == 429:
return true
case target == ErrTimeout && e.StatusCode == 502:
return true
case target == ErrNotEnoughSpace && e.StatusCode == 507:
return true
default:
return false
}
}
// NewSandboxError creates a new SandboxError.
func NewSandboxError(statusCode int, message string) *SandboxError {
return &SandboxError{
StatusCode: statusCode,
Message: message,
}
}
// TimeoutError represents a timeout error with additional context.
type TimeoutError struct {
// Type indicates whether it's an execution or request timeout.
Type string
// Duration is the timeout duration that was exceeded.
Duration string
}
// Error implements the error interface.
func (e *TimeoutError) Error() string {
if e.Duration != "" {
return fmt.Sprintf("%s timeout exceeded after %s", e.Type, e.Duration)
}
return fmt.Sprintf("%s timeout exceeded", e.Type)
}
// Is checks if the error matches the target.
func (e *TimeoutError) Is(target error) bool {
switch e.Type {
case "execution":
return target == ErrTimeout
case "request":
return target == ErrRequestTimeout
default:
return false
}
}
// NewExecutionTimeoutError creates a new execution timeout error.
func NewExecutionTimeoutError() *TimeoutError {
return &TimeoutError{
Type: "execution",
}
}
// NewRequestTimeoutError creates a new request timeout error.
func NewRequestTimeoutError() *TimeoutError {
return &TimeoutError{
Type: "request",
}
}
// formatHTTPError converts an HTTP response to an appropriate error.
func formatHTTPError(statusCode int, body string) error {
message := body
if message == "" {
message = fmt.Sprintf("HTTP %d", statusCode)
}
switch statusCode {
case 400:
return &SandboxError{
StatusCode: statusCode,
Message: message,
Err: ErrInvalidArgument,
}
case 401:
return &SandboxError{
StatusCode: statusCode,
Message: message,
Err: ErrAuthentication,
}
case 404:
return &SandboxError{
StatusCode: statusCode,
Message: message,
Err: ErrNotFound,
}
case 429:
return &SandboxError{
StatusCode: statusCode,
Message: message,
Err: ErrRateLimit,
}
case 502:
return &SandboxError{
StatusCode: statusCode,
Message: fmt.Sprintf("%s: this error is likely due to sandbox timeout, you can modify the sandbox timeout by passing a timeout option when starting the sandbox", message),
Err: ErrTimeout,
}
case 507:
return &SandboxError{
StatusCode: statusCode,
Message: message,
Err: ErrNotEnoughSpace,
}
default:
return &SandboxError{
StatusCode: statusCode,
Message: message,
}
}
}