-
Notifications
You must be signed in to change notification settings - Fork 14
/
error.go
94 lines (77 loc) · 2.43 KB
/
error.go
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
package anthropic
import (
"errors"
"fmt"
)
type ErrType string
const (
// ErrTypeInvalidRequest There was an issue with the format or content of your request.
ErrTypeInvalidRequest ErrType = "invalid_request_error"
// ErrTypeAuthentication There's an issue with your API key.
ErrTypeAuthentication ErrType = "authentication_error"
// ErrTypePermission Your API key does not have permission to use the specified resource.
ErrTypePermission ErrType = "permission_error"
// ErrTypeNotFound The requested resource was not found.
ErrTypeNotFound ErrType = "not_found_error"
// ErrTypeTooLarge Request exceeds the maximum allowed number of bytes.
ErrTypeTooLarge ErrType = "request_too_large"
// ErrTypeRateLimit Your account has hit a rate limit.
ErrTypeRateLimit ErrType = "rate_limit_error"
// ErrTypeApi An unexpected error has occurred internal to Anthropic's systems.
ErrTypeApi ErrType = "api_error"
// ErrTypeOverloaded Anthropic's API is temporarily overloaded.
ErrTypeOverloaded ErrType = "overloaded_error"
)
var (
ErrSteamingNotSupportTools = errors.New("streaming is not yet supported tools")
)
// APIError provides error information returned by the Anthropic API.
type APIError struct {
Type ErrType `json:"type"`
Message string `json:"message"`
}
func (e *APIError) IsInvalidRequestErr() bool {
return e.Type == ErrTypeInvalidRequest
}
func (e *APIError) IsAuthenticationErr() bool {
return e.Type == ErrTypeAuthentication
}
func (e *APIError) IsPermissionErr() bool {
return e.Type == ErrTypePermission
}
func (e *APIError) IsNotFoundErr() bool {
return e.Type == ErrTypeNotFound
}
func (e *APIError) IsTooLargeErr() bool {
return e.Type == ErrTypeTooLarge
}
func (e *APIError) IsRateLimitErr() bool {
return e.Type == ErrTypeRateLimit
}
func (e *APIError) IsApiErr() bool {
return e.Type == ErrTypeApi
}
func (e *APIError) IsOverloadedErr() bool {
return e.Type == ErrTypeOverloaded
}
// RequestError provides information about generic request errors.
type RequestError struct {
StatusCode int
Err error
Body []byte
}
type ErrorResponse struct {
Type string `json:"type"`
Error *APIError `json:"error,omitempty"`
}
func (e *APIError) Error() string {
return fmt.Sprintf("anthropic api error type: %s, message: %s", e.Type, e.Message)
}
func (e *RequestError) Error() string {
return fmt.Sprintf(
"anthropic request error status code: %d, err: %s, body: %s",
e.StatusCode,
e.Err,
e.Body,
)
}