-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
131 lines (108 loc) · 3.79 KB
/
errors.go
File metadata and controls
131 lines (108 loc) · 3.79 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
package openrouter
import (
"errors"
"fmt"
)
// RequestError represents an error returned by the OpenRouter API.
type RequestError struct {
StatusCode int
Message string
Type string
Code string
}
// Error implements the error interface.
func (e *RequestError) Error() string {
if e.Type != "" {
return fmt.Sprintf("openrouter: %s (type: %s, status: %d)", e.Message, e.Type, e.StatusCode)
}
return fmt.Sprintf("openrouter: %s (status: %d)", e.Message, e.StatusCode)
}
// IsRateLimitError returns true if the error is a rate limit error.
func (e *RequestError) IsRateLimitError() bool {
return e.StatusCode == 429
}
// IsAuthenticationError returns true if the error is an authentication error.
func (e *RequestError) IsAuthenticationError() bool {
return e.StatusCode == 401
}
// IsPermissionError returns true if the error is a permission error.
func (e *RequestError) IsPermissionError() bool {
return e.StatusCode == 403
}
// IsNotFoundError returns true if the error is a not found error.
func (e *RequestError) IsNotFoundError() bool {
return e.StatusCode == 404
}
// IsServerError returns true if the error is a server error.
func (e *RequestError) IsServerError() bool {
return e.StatusCode >= 500
}
// StreamError represents an error that occurs during streaming.
type StreamError struct {
Err error
Message string
}
// Error implements the error interface.
func (e *StreamError) Error() string {
if e.Err != nil {
return fmt.Sprintf("stream error: %s: %v", e.Message, e.Err)
}
return fmt.Sprintf("stream error: %s", e.Message)
}
// Unwrap returns the underlying error.
func (e *StreamError) Unwrap() error {
return e.Err
}
// ValidationError represents a validation error for request parameters.
type ValidationError struct {
Field string
Message string
}
// Error implements the error interface.
func (e *ValidationError) Error() string {
if e.Field != "" {
return fmt.Sprintf("validation error for field '%s': %s", e.Field, e.Message)
}
return fmt.Sprintf("validation error: %s", e.Message)
}
// ErrNoAPIKey is returned when no API key is provided.
var ErrNoAPIKey = &ValidationError{Field: "apiKey", Message: "API key is required"}
// ErrNoModel is returned when no model is specified.
var ErrNoModel = &ValidationError{Field: "model", Message: "model is required"}
// ErrNoMessages is returned when no messages are provided for chat completion.
var ErrNoMessages = &ValidationError{Field: "messages", Message: "at least one message is required"}
// ErrNoPrompt is returned when no prompt is provided for completion.
var ErrNoPrompt = &ValidationError{Field: "prompt", Message: "prompt is required"}
// IsRequestError checks if an error is a RequestError and returns it.
func IsRequestError(err error) (*RequestError, bool) {
var reqErr *RequestError
ok := errors.As(err, &reqErr)
return reqErr, ok
}
// IsStreamError checks if an error is a StreamError and returns it.
func IsStreamError(err error) (*StreamError, bool) {
var streamErr *StreamError
ok := errors.As(err, &streamErr)
return streamErr, ok
}
// IsValidationError checks if an error is a ValidationError and returns it.
func IsValidationError(err error) (*ValidationError, bool) {
var valErr *ValidationError
ok := errors.As(err, &valErr)
return valErr, ok
}
// CircuitBreakerError is returned when a request is blocked by the circuit breaker.
type CircuitBreakerError struct {
State CircuitState
Message string
}
// Error implements the error interface.
func (e *CircuitBreakerError) Error() string {
return fmt.Sprintf("circuit breaker error (state: %s): %s", e.State, e.Message)
}
// IsCircuitBreakerError checks if an error is a CircuitBreakerError and returns it.
func IsCircuitBreakerError(err error) (*CircuitBreakerError, bool) {
var cbErr *CircuitBreakerError
ok := errors.As(err, &cbErr)
return cbErr, ok
}