forked from charmbracelet/fantasy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
104 lines (88 loc) · 2.47 KB
/
errors.go
File metadata and controls
104 lines (88 loc) · 2.47 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
package fantasy
import (
"errors"
"fmt"
"net/http"
"strings"
"github.com/charmbracelet/x/exp/slice"
)
// Error is a custom error type for the fantasy package.
type Error struct {
Message string
Title string
Cause error
}
func (err *Error) Error() string {
if err.Title == "" {
return err.Message
}
return fmt.Sprintf("%s: %s", err.Title, err.Message)
}
func (err Error) Unwrap() error {
return err.Cause
}
// ProviderError represents an error returned by an external provider.
type ProviderError struct {
Message string
Title string
Cause error
URL string
StatusCode int
RequestBody []byte
ResponseHeaders map[string]string
ResponseBody []byte
}
func (m *ProviderError) Error() string {
if m.Title == "" {
return m.Message
}
return fmt.Sprintf("%s: %s", m.Title, m.Message)
}
// IsRetryable checks if the error is retryable based on the status code.
func (m *ProviderError) IsRetryable() bool {
return m.StatusCode == http.StatusRequestTimeout || m.StatusCode == http.StatusConflict || m.StatusCode == http.StatusTooManyRequests
}
// RetryError represents an error that occurred during retry operations.
type RetryError struct {
Errors []error
}
func (e *RetryError) Error() string {
if err, ok := slice.Last(e.Errors); ok {
return fmt.Sprintf("retry error: %v", err)
}
return "retry error: no underlying errors"
}
func (e RetryError) Unwrap() error {
if err, ok := slice.Last(e.Errors); ok {
return err
}
return nil
}
// ErrorTitleForStatusCode returns a human-readable title for a given HTTP status code.
func ErrorTitleForStatusCode(statusCode int) string {
return strings.ToLower(http.StatusText(statusCode))
}
// NoObjectGeneratedError is returned when object generation fails
// due to parsing errors, validation errors, or model failures.
type NoObjectGeneratedError struct {
RawText string
ParseError error
ValidationError error
Usage Usage
FinishReason FinishReason
}
// Error implements the error interface.
func (e *NoObjectGeneratedError) Error() string {
if e.ValidationError != nil {
return fmt.Sprintf("object validation failed: %v", e.ValidationError)
}
if e.ParseError != nil {
return fmt.Sprintf("failed to parse object: %v", e.ParseError)
}
return "failed to generate object"
}
// IsNoObjectGeneratedError checks if an error is of type NoObjectGeneratedError.
func IsNoObjectGeneratedError(err error) bool {
var target *NoObjectGeneratedError
return errors.As(err, &target)
}