errors.go

  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	ContextUsedTokens  int
 43	ContextMaxTokens   int
 44	ContextTooLargeErr bool
 45}
 46
 47func (m *ProviderError) Error() string {
 48	if m.Title == "" {
 49		return m.Message
 50	}
 51	return fmt.Sprintf("%s: %s", m.Title, m.Message)
 52}
 53
 54// IsRetryable checks if the error is retryable based on the status code.
 55func (m *ProviderError) IsRetryable() bool {
 56	return m.StatusCode == http.StatusRequestTimeout || m.StatusCode == http.StatusConflict || m.StatusCode == http.StatusTooManyRequests
 57}
 58
 59// IsContextTooLarge checks if the error is due to the context exceeding the model's limit.
 60func (m *ProviderError) IsContextTooLarge() bool {
 61	return m.ContextTooLargeErr || m.ContextMaxTokens > 0 || m.ContextUsedTokens > 0
 62}
 63
 64// RetryError represents an error that occurred during retry operations.
 65type RetryError struct {
 66	Errors []error
 67}
 68
 69func (e *RetryError) Error() string {
 70	if err, ok := slice.Last(e.Errors); ok {
 71		return fmt.Sprintf("retry error: %v", err)
 72	}
 73	return "retry error: no underlying errors"
 74}
 75
 76func (e RetryError) Unwrap() error {
 77	if err, ok := slice.Last(e.Errors); ok {
 78		return err
 79	}
 80	return nil
 81}
 82
 83// ErrorTitleForStatusCode returns a human-readable title for a given HTTP status code.
 84func ErrorTitleForStatusCode(statusCode int) string {
 85	return strings.ToLower(http.StatusText(statusCode))
 86}
 87
 88// NoObjectGeneratedError is returned when object generation fails
 89// due to parsing errors, validation errors, or model failures.
 90type NoObjectGeneratedError struct {
 91	RawText         string
 92	ParseError      error
 93	ValidationError error
 94	Usage           Usage
 95	FinishReason    FinishReason
 96}
 97
 98// Error implements the error interface.
 99func (e *NoObjectGeneratedError) Error() string {
100	if e.ValidationError != nil {
101		return fmt.Sprintf("object validation failed: %v", e.ValidationError)
102	}
103	if e.ParseError != nil {
104		return fmt.Sprintf("failed to parse object: %v", e.ParseError)
105	}
106	return "failed to generate object"
107}
108
109// IsNoObjectGeneratedError checks if an error is of type NoObjectGeneratedError.
110func IsNoObjectGeneratedError(err error) bool {
111	var target *NoObjectGeneratedError
112	return errors.As(err, &target)
113}