-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
68 lines (59 loc) · 1.56 KB
/
Copy patherrors.go
File metadata and controls
68 lines (59 loc) · 1.56 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
package oneclaw
import (
"errors"
"fmt"
"github.com/1clawAI/1claw-go-sdk/internal/openapi"
)
// Common SDK errors. Use errors.Is to check.
var (
ErrUnauthorized = errors.New("1claw: unauthorized")
ErrValidation = errors.New("1claw: validation error")
ErrNotFound = errors.New("1claw: not found")
ErrRateLimited = errors.New("1claw: rate limited")
ErrPaymentRequired = errors.New("1claw: payment required")
ErrServerError = errors.New("1claw: server error")
)
// AuthError represents an authentication or authorization failure.
type AuthError struct {
StatusCode int
Message string
Detail string
Body []byte
}
func (e *AuthError) Error() string {
if e.Detail != "" {
return fmt.Sprintf("%s: %s (%s)", ErrUnauthorized, e.Message, e.Detail)
}
return fmt.Sprintf("%s: %s", ErrUnauthorized, e.Message)
}
func (e *AuthError) Is(target error) bool {
return target == ErrUnauthorized
}
// APIError wraps API response errors with status and body.
type APIError struct {
StatusCode int
Message string
Detail string
Body []byte
}
func (e *APIError) Error() string {
msg := e.Message
if e.Detail != "" {
msg += ": " + e.Detail
}
return fmt.Sprintf("1claw API error (status %d): %s", e.StatusCode, msg)
}
// wrapAPIError converts openapi errors to SDK errors.
func wrapAPIError(err error) error {
if err == nil {
return nil
}
if gerr, ok := err.(*openapi.GenericOpenAPIError); ok {
// Try to extract status from error string or body
return &APIError{
Message: gerr.Error(),
Body: gerr.Body(),
}
}
return err
}