errors

package
v0.1.5-rc.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 8, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package errors provides custom error types for the goai package.

Index

Constants

This section is empty.

Variables

View Source
var (
	Is     = errors.Is
	As     = errors.As
	New    = errors.New
	Unwrap = errors.Unwrap
	Join   = errors.Join
)

Re-export standard library error functions for convenience.

View Source
var (
	// ErrAPIError indicates an error from the provider's API.
	ErrAPIError = errors.New("api error")

	// ErrRateLimited indicates the request was rate limited.
	ErrRateLimited = errors.New("rate limited")

	// ErrAuthenticationFailed indicates authentication failed.
	ErrAuthenticationFailed = errors.New("authentication failed")

	// ErrInvalidRequest indicates the request was invalid.
	ErrInvalidRequest = errors.New("invalid request")

	// ErrModelNotFound indicates the requested model was not found.
	ErrModelNotFound = errors.New("model not found")

	// ErrContentFiltered indicates content was filtered.
	ErrContentFiltered = errors.New("content filtered")

	// ErrContextLengthExceeded indicates the context length was exceeded.
	ErrContextLengthExceeded = errors.New("context length exceeded")

	// ErrTimeout indicates the request timed out.
	ErrTimeout = errors.New("request timeout")

	// ErrCanceled indicates the request was canceled.
	ErrCanceled = errors.New("request canceled")

	// ErrUnsupported indicates an unsupported operation.
	ErrUnsupported = errors.New("unsupported operation")

	// ErrInvalidResponse indicates an invalid response from the provider.
	ErrInvalidResponse = errors.New("invalid response")

	// ErrToolExecutionFailed indicates a tool execution failure.
	ErrToolExecutionFailed = errors.New("tool execution failed")

	// ErrJSONParseFailed indicates JSON parsing failed.
	ErrJSONParseFailed = errors.New("json parse failed")

	// ErrSchemaMismatch indicates the response didn't match the expected schema.
	ErrSchemaMismatch = errors.New("schema mismatch")
)

Standard error types that can be checked with errors.Is.

View Source
var ErrInvalidToolInput = errors.New("invalid tool input")

ErrInvalidToolInput indicates the tool input doesn't match the expected schema.

View Source
var ErrMissingToolResults = errors.New("missing tool results")

ErrMissingToolResults indicates tool results are missing for tool calls.

View Source
var ErrNoSuchTool = errors.New("no such tool")

ErrNoSuchTool indicates the model tried to call a tool that doesn't exist.

View Source
var ErrToolCallRepair = errors.New("tool call repair failed")

ErrToolCallRepair indicates the tool call repair function failed.

Functions

func IsInvalidToolInputError

func IsInvalidToolInputError(err error) bool

IsInvalidToolInputError returns true if err is an InvalidToolInputError.

func IsMissingToolResultsError

func IsMissingToolResultsError(err error) bool

IsMissingToolResultsError returns true if err is a MissingToolResultsError.

func IsNoSuchToolError

func IsNoSuchToolError(err error) bool

IsNoSuchToolError returns true if err is a NoSuchToolError.

func IsRetryable

func IsRetryable(err error) bool

IsRetryable returns true if the error is retryable.

func IsToolCallRepairError

func IsToolCallRepairError(err error) bool

IsToolCallRepairError returns true if err is a ToolCallRepairError.

Types

type APICallError

type APICallError struct {
	// Message is the error message.
	Message string

	// URL is the request URL.
	URL string

	// RequestBodyValues contains the request body values for debugging.
	RequestBodyValues any

	// StatusCode is the HTTP status code (0 if not applicable).
	StatusCode int

	// ResponseHeaders are the response headers.
	ResponseHeaders map[string]string

	// ResponseBody is the raw response body.
	ResponseBody string

	// Data is the parsed error data from the provider.
	Data any

	// Cause is the underlying error.
	Cause error

	// IsRetryable indicates if the error is retryable.
	IsRetryable bool
}

APICallError represents an error from an API call with full context. This is a Go translation of ai-sdk/packages/provider/src/errors/api-call-error.ts

func NewAPICallError

func NewAPICallError(opts APICallErrorOptions) *APICallError

NewAPICallError creates a new APICallError with default retryability logic. By default, errors are retryable if the status code is: - 408 (Request Timeout) - 409 (Conflict) - 429 (Too Many Requests) - >= 500 (Server Error)

func (*APICallError) Error

func (e *APICallError) Error() string

Error implements the error interface.

func (*APICallError) Unwrap

func (e *APICallError) Unwrap() error

Unwrap returns the underlying cause for errors.Is/As compatibility.

type APICallErrorOptions

type APICallErrorOptions struct {
	Message           string
	URL               string
	RequestBodyValues any
	StatusCode        int
	ResponseHeaders   map[string]string
	ResponseBody      string
	Data              any
	Cause             error
	IsRetryable       bool
	IsRetryableSet    bool // true if IsRetryable was explicitly set
}

APICallErrorOptions contains options for creating an APICallError.

type APIError

type APIError struct {
	// Provider is the provider that returned the error.
	Provider string

	// StatusCode is the HTTP status code (0 if not applicable).
	StatusCode int

	// Code is the error code from the provider.
	Code string

	// Message is the error message.
	Message string

	// Param is the parameter that caused the error (if applicable).
	Param string

	// Type is the error type from the provider.
	Type string

	// RequestID is the request ID for debugging.
	RequestID string

	// Retryable indicates if the error is retryable.
	Retryable bool

	// RetryAfter is the number of seconds to wait before retrying (0 if not specified).
	RetryAfter int
}

APIError represents an error from a provider's API.

func (*APIError) Error

func (e *APIError) Error() string

Error implements the error interface.

func (*APIError) Unwrap

func (e *APIError) Unwrap() error

Unwrap returns the base error for errors.Is compatibility.

type InvalidToolInputError

type InvalidToolInputError struct {
	// ToolName is the name of the tool.
	ToolName string

	// ToolInput is the input that was invalid.
	ToolInput string

	// Cause is the underlying validation error.
	Cause error
}

InvalidToolInputError is returned when tool input doesn't match the expected schema.

func (*InvalidToolInputError) Error

func (e *InvalidToolInputError) Error() string

Error implements the error interface.

func (*InvalidToolInputError) Unwrap

func (e *InvalidToolInputError) Unwrap() error

Unwrap returns the base error for errors.Is compatibility.

type JSONParseError

type JSONParseError struct {
	// Text is the text that failed to parse.
	Text string

	// Cause is the underlying error.
	Cause error
}

JSONParseError represents a JSON parsing error.

func (*JSONParseError) Error

func (e *JSONParseError) Error() string

Error implements the error interface.

func (*JSONParseError) Unwrap

func (e *JSONParseError) Unwrap() error

Unwrap returns the underlying error.

type MissingToolResultsError

type MissingToolResultsError struct {
	// ToolCallIDs are the IDs of the tool calls that are missing results.
	ToolCallIDs []string
}

MissingToolResultsError is returned when tool results are missing for tool calls. Source: ai-sdk/packages/ai/src/error/missing-tool-result-error.ts

func (*MissingToolResultsError) Error

func (e *MissingToolResultsError) Error() string

Error implements the error interface.

func (*MissingToolResultsError) Unwrap

func (e *MissingToolResultsError) Unwrap() error

Unwrap returns the base error for errors.Is compatibility.

type NoSuchToolError

type NoSuchToolError struct {
	// ToolName is the name of the tool the model tried to call.
	ToolName string

	// AvailableTools is the list of available tool names (nil if no tools available).
	AvailableTools []string
}

NoSuchToolError is returned when the model tries to call a tool that doesn't exist.

func (*NoSuchToolError) Error

func (e *NoSuchToolError) Error() string

Error implements the error interface.

func (*NoSuchToolError) Unwrap

func (e *NoSuchToolError) Unwrap() error

Unwrap returns the base error for errors.Is compatibility.

type RetryInfo

type RetryInfo struct {
	// ShouldRetry indicates if the error is retryable.
	ShouldRetry bool

	// RetryAfter is the suggested wait time in seconds.
	RetryAfter int

	// Attempt is the current attempt number.
	Attempt int

	// MaxAttempts is the maximum number of attempts.
	MaxAttempts int
}

RetryInfo contains information about retry behavior.

func GetRetryInfo

func GetRetryInfo(err error) *RetryInfo

GetRetryInfo extracts retry information from an error.

type SchemaValidationError

type SchemaValidationError struct {
	// Schema is a description of the expected schema.
	Schema string

	// Value is the value that didn't match.
	Value any

	// Path is the JSON path to the invalid field.
	Path string

	// Message is the validation error message.
	Message string
}

SchemaValidationError represents a schema validation error.

func (*SchemaValidationError) Error

func (e *SchemaValidationError) Error() string

Error implements the error interface.

func (*SchemaValidationError) Unwrap

func (e *SchemaValidationError) Unwrap() error

Unwrap returns the base error.

type ToolCallRepairError

type ToolCallRepairError struct {
	// Cause is the error from the repair function.
	Cause error

	// OriginalError is the original error that triggered repair.
	OriginalError error
}

ToolCallRepairError is returned when the tool call repair function fails.

func (*ToolCallRepairError) Error

func (e *ToolCallRepairError) Error() string

Error implements the error interface.

func (*ToolCallRepairError) Unwrap

func (e *ToolCallRepairError) Unwrap() error

Unwrap returns the base error for errors.Is compatibility.

type ToolError

type ToolError struct {
	// ToolCallID is the ID of the tool call.
	ToolCallID string

	// ToolName is the name of the tool.
	ToolName string

	// Message is the error message.
	Message string

	// Cause is the underlying error.
	Cause error
}

ToolError represents a tool execution error.

func (*ToolError) Error

func (e *ToolError) Error() string

Error implements the error interface.

func (*ToolError) Unwrap

func (e *ToolError) Unwrap() error

Unwrap returns the underlying error.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL