1package fantasy
2
3import (
4 "errors"
5 "fmt"
6 "net/http"
7 "strings"
8
9 "github.com/charmbracelet/x/exp/slice"
10)
11
12// Error is a custom error type for the fantasy package.
13type Error struct {
14 Message string
15 Title string
16 Cause error
17}
18
19func (err *Error) Error() string {
20 if err.Title == "" {
21 return err.Message
22 }
23 return fmt.Sprintf("%s: %s", err.Title, err.Message)
24}
25
26func (err Error) Unwrap() error {
27 return err.Cause
28}
29
30// ProviderError represents an error returned by an external provider.
31type ProviderError struct {
32 Message string
33 Title string
34 Cause error
35
36 URL string
37 StatusCode int
38 RequestBody []byte
39 ResponseHeaders map[string]string
40 ResponseBody []byte
41}
42
43func (m *ProviderError) Error() string {
44 if m.Title == "" {
45 return m.Message
46 }
47 return fmt.Sprintf("%s: %s", m.Title, m.Message)
48}
49
50// IsRetryable checks if the error is retryable based on the status code.
51func (m *ProviderError) IsRetryable() bool {
52 return m.StatusCode == http.StatusRequestTimeout || m.StatusCode == http.StatusConflict || m.StatusCode == http.StatusTooManyRequests
53}
54
55// RetryError represents an error that occurred during retry operations.
56type RetryError struct {
57 Errors []error
58}
59
60func (e *RetryError) Error() string {
61 if err, ok := slice.Last(e.Errors); ok {
62 return fmt.Sprintf("retry error: %v", err)
63 }
64 return "retry error: no underlying errors"
65}
66
67func (e RetryError) Unwrap() error {
68 if err, ok := slice.Last(e.Errors); ok {
69 return err
70 }
71 return nil
72}
73
74// ErrorTitleForStatusCode returns a human-readable title for a given HTTP status code.
75func ErrorTitleForStatusCode(statusCode int) string {
76 return strings.ToLower(http.StatusText(statusCode))
77}
78
79// NoObjectGeneratedError is returned when object generation fails
80// due to parsing errors, validation errors, or model failures.
81type NoObjectGeneratedError struct {
82 RawText string
83 ParseError error
84 ValidationError error
85 Usage Usage
86 FinishReason FinishReason
87}
88
89// Error implements the error interface.
90func (e *NoObjectGeneratedError) Error() string {
91 if e.ValidationError != nil {
92 return fmt.Sprintf("object validation failed: %v", e.ValidationError)
93 }
94 if e.ParseError != nil {
95 return fmt.Sprintf("failed to parse object: %v", e.ParseError)
96 }
97 return "failed to generate object"
98}
99
100// IsNoObjectGeneratedError checks if an error is of type NoObjectGeneratedError.
101func IsNoObjectGeneratedError(err error) bool {
102 var target *NoObjectGeneratedError
103 return errors.As(err, &target)
104}