AiFlow.Ollama.Error exception (AiFlow v0.1.0)
View SourceDefines an error structure and helper functions for handling errors in the AiFlow.Ollama module.
This module provides a consistent way to represent and handle errors that occur during interactions
with the Ollama API, such as HTTP request failures, file operation errors, invalid inputs, or server
issues. Errors are categorized by type (e.g., :http, :network, :file, :invalid), and each
error includes a human-readable message for better debugging and user feedback.
Error Structure
The %Error{} struct contains:
type: An atom indicating the error category (:http,:network,:file,:invalid,:pull,:server,:client,:unknown).reason: The underlying cause of the error (e.g., HTTP status, file error code, or custom term).message: A human-readable description of the error.status: An optional HTTP status code (for:httperrors).
Examples
iex> AiFlow.Ollama.Error.invalid("Digest must be a valid SHA256 hash")
%AiFlow.Ollama.Error{
type: :invalid,
reason: :invalid_input,
message: "Digest must be a valid SHA256 hash",
status: nil
}
iex> AiFlow.Ollama.Error.http(404, "Not found")
%AiFlow.Ollama.Error{
type: :http,
reason: :not_found,
message: "HTTP request failed with status 404: Not found",
status: 404
}
iex> error = AiFlow.Ollama.Error.file(:enoent)
iex> AiFlow.Ollama.Error.to_string(error)
"File operation failed: file not found"
Summary
Functions
Creates an error for client-related issues.
Creates an error for file-related issues.
Creates an error for HTTP-related issues.
Creates an error for invalid input parameters.
Creates an error for network-related issues.
Creates an error for pull operation issues (e.g., pulling a model from the Ollama API).
Raises an error with a formatted message based on the operation and error struct.
Creates an error for server-related issues.
Converts an error struct to a string for logging or display.
Creates an error for unknown or uncategorized issues.
Types
@type error_type() ::
:http | :network | :file | :invalid | :pull | :server | :client | :unknown
@type t() :: %AiFlow.Ollama.Error{ __exception__: true, message: String.t(), reason: error_reason(), status: integer() | nil, type: error_type() }
Functions
Creates an error for client-related issues.
Parameters
reason: A term describing the client error.
Returns
%Error{}withtype: :clientand a formattedmessage.
Examples
iex> AiFlow.Ollama.Error.client("Internal client error")
%AiFlow.Ollama.Error{
type: :client,
reason: "Internal client error",
message: "Client error: Internal client error",
status: nil
}
Creates an error for file-related issues.
Parameters
reason: An atom or term representing the file error (e.g.,:enoent,:eacces).
Returns
%Error{}withtype: :fileand a formattedmessage.
Examples
iex> AiFlow.Ollama.Error.file(:enoent)
%AiFlow.Ollama.Error{
type: :file,
reason: :enoent,
message: "File operation failed: file not found",
status: nil
}
Creates an error for HTTP-related issues.
Parameters
status: An integer representing the HTTP status code (e.g., 404, 500).reason: A term describing the HTTP error (e.g., "Not found",:bad_request).
Returns
%Error{}withtype: :http, the providedstatus, and a formattedmessage.
Examples
iex> AiFlow.Ollama.Error.http(404, "Not found")
%AiFlow.Ollama.Error{
type: :http,
reason: :not_found,
message: "HTTP request failed with status 404: Not found",
status: 404
}
Creates an error for invalid input parameters.
Parameters
message: A string describing the invalid input.
Returns
%Error{}withtype: :invalid,reason: :invalid_input, and the providedmessage.
Examples
iex> AiFlow.Ollama.Error.invalid("Digest must be a valid SHA256 hash")
%AiFlow.Ollama.Error{
type: :invalid,
reason: :invalid_input,
message: "Digest must be a valid SHA256 hash",
status: nil
}
Creates an error for network-related issues.
Parameters
reason: A term describing the network error (e.g.,:timeout,:conn_refused).
Returns
%Error{}withtype: :networkand a formattedmessage.
Examples
iex> AiFlow.Ollama.Error.network(:timeout)
%AiFlow.Ollama.Error{
type: :network,
reason: :timeout,
message: "Network error: timeout",
status: nil
}
Creates an error for pull operation issues (e.g., pulling a model from the Ollama API).
Parameters
reason: A term describing the pull error.
Returns
%Error{}withtype: :pulland a formattedmessage.
Examples
iex> AiFlow.Ollama.Error.pull("Model not found")
%AiFlow.Ollama.Error{
type: :pull,
reason: "Model not found",
message: "Pull operation failed: Model not found",
status: nil
}
Raises an error with a formatted message based on the operation and error struct.
Parameters
operation: An atom representing the operation that failed (e.g.,:check_blob,:create_blob).error: An%Error{}struct containing error details.
Raises
- Raises an
%AiFlow.Ollama.Error{}with a formatted message.
Examples
iex> error = AiFlow.Ollama.Error.invalid("Invalid digest")
iex> AiFlow.Ollama.Error.raise_error(:check_blob, error)
** (AiFlow.Ollama.Error) check_blob failed: Invalid digest
iex> error = AiFlow.Ollama.Error.http(500, "Server error")
iex> AiFlow.Ollama.Error.raise_error(:create_blob, error)
** (AiFlow.Ollama.Error) create_blob failed: HTTP request failed with status 500: Server error
Creates an error for server-related issues.
Parameters
reason: A term describing the server error.
Returns
%Error{}withtype: :serverand a formattedmessage.
Examples
iex> AiFlow.Ollama.Error.server("Internal server error")
%AiFlow.Ollama.Error{
type: :server,
reason: "Internal server error",
message: "Server error: Internal server error",
status: nil
}
Converts an error struct to a string for logging or display.
Parameters
error: An%Error{}struct.
Returns
- A string representing the error's message.
Examples
iex> error = AiFlow.Ollama.Error.file(:enoent)
iex> AiFlow.Ollama.Error.to_string(error)
"File operation failed: file not found"
Creates an error for unknown or uncategorized issues.
Parameters
reason: A term describing the unknown error.
Returns
%Error{}withtype: :unknownand a formattedmessage.
Examples
iex> AiFlow.Ollama.Error.unknown(:unexpected_error)
%AiFlow.Ollama.Error{
type: :unknown,
reason: :unexpected_error,
message: "Unknown error: unexpected_error",
status: nil
}