Skip to main content

CompletionError

Enum CompletionError 

Source
#[non_exhaustive]
pub enum CompletionError { HttpError(Error), JsonError(Error), UrlError(ParseError), RequestError(Box<dyn Error + Send + Sync + 'static>), ResponseError(String), ProviderError(String), ProviderResponse(ProviderResponseError), }
Expand description

Errors returned by completion models.

Inspect provider failures with Self::provider_response_body, Self::provider_response_json, and Self::provider_response_status. These recover the provider’s raw HTTP status and response body so you can branch on a provider error code or surface a precise diagnostic. The same helpers are available on EmbeddingError, ImageGenerationError, AudioGenerationError, TranscriptionError, RerankError, and (forwarded) PromptError.

use rig_core::completion::CompletionError;

/// Log the provider's raw error response when a completion fails.
fn report(error: &CompletionError) {
    if let Some(status) = error.provider_response_status() {
        // Note: this can be a 2xx status for providers that return an error
        // envelope alongside a success status — the error itself means failure.
        eprintln!("provider returned HTTP {status}");
    }
    match error.provider_response_json() {
        Ok(Some(json)) => eprintln!("provider error payload: {json}"),
        Ok(None) => eprintln!("no provider response body (e.g. a transport error)"),
        Err(_) => eprintln!(
            "provider response body was not valid JSON: {:?}",
            error.provider_response_body(),
        ),
    }
}

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

HttpError(Error)

Http error (e.g.: connection error, timeout, etc.)

§

JsonError(Error)

Json error (e.g.: serialization, deserialization)

§

UrlError(ParseError)

Url error (e.g.: invalid URL)

§

RequestError(Box<dyn Error + Send + Sync + 'static>)

Available on non-target_family=wasm only.

Error building the completion request

§

ResponseError(String)

Error parsing the completion response

§

ProviderError(String)

Error returned by the completion model provider

§

ProviderResponse(ProviderResponseError)

Raw error response preserved from the completion model provider

Implementations§

Source§

impl CompletionError

Source

pub fn from_http_response(status: StatusCode, body: impl Into<String>) -> Self

Builds an error from a captured HTTP status and raw response body, routing it so the provider_response_* helpers stay useful.

This is the single funnel every HTTP-error path should use instead of flattening a status and body into a ProviderError(String):

Either way the raw body is kept verbatim and the status stays recoverable through Self::provider_response_status. Read the response body exactly once and hand it here for both branches.

Source

pub fn from_provider_body(body: impl Into<String>) -> Self

Preserves a raw provider error body that has no HTTP status.

Use this for non-HTTP transports (gRPC / SDK clients such as AWS Bedrock, Vertex AI, or the gRPC Gemini client) where the provider returns an error payload but no http::StatusCode is available. The body is preserved as Self::ProviderResponse with status == None, so Self::provider_response_body still surfaces it while Self::provider_response_status returns None.

Source

pub fn provider_response_body(&self) -> Option<&str>

Returns the raw provider response body when available.

This is available for:

  • Self::ProviderResponse using its preserved body.
  • Self::HttpError when it wraps an HTTP non-success response that carries a body.

Returns None for any other variant — for example a Rig-generated ProviderError diagnostic, or a failure from a transport with no provider response body to preserve. An empty preserved body is reported as Some("") (the provider returned no payload), which is distinct from None; note that Self::provider_response_json maps that same empty body to Ok(None).

Source

pub fn provider_response_json(&self) -> Result<Option<Value>, Error>

Parses the provider response body as JSON.

Returns:

  • Ok(Some(value)) when a body is present and valid JSON.
  • Ok(None) when no provider response body is available.
  • Err(error) when a body is present but isn’t valid JSON.
Source

pub fn provider_response_status(&self) -> Option<StatusCode>

Returns the HTTP status code when this error preserves one, either from a non-success HTTP response, from a preserved provider response, or from a 2xx error envelope.

Warning: this can return a 2xx status. Some providers send an error envelope alongside a success status, which Rig preserves via Self::ProviderResponse. Callers must not infer failure from the status code alone — the existence of this error already means the call failed. Returns None for non-HTTP transports (gRPC / SDK clients) and for variants that carry no provider response.

Trait Implementations§

Source§

impl Debug for CompletionError

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for CompletionError

Source§

fn fmt(&self, __formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Error for CompletionError

Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0:

use the Display impl or to_string()

1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0:

replaced by Error::source, which can support downcasting

Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl From<Box<dyn Error + Sync + Send>> for CompletionError

Source§

fn from(source: Box<dyn Error + Send + Sync + 'static>) -> Self

Converts to this type from the input type.
Source§

impl From<CompletionError> for StreamingError

Source§

fn from(source: CompletionError) -> Self

Converts to this type from the input type.
Source§

impl From<CompletionError> for PromptError

Source§

fn from(source: CompletionError) -> Self

Converts to this type from the input type.
Source§

impl From<CompletionError> for ExtractionError

Source§

fn from(source: CompletionError) -> Self

Converts to this type from the input type.
Source§

impl From<Error> for CompletionError

Source§

fn from(source: Error) -> Self

Converts to this type from the input type.
Source§

impl From<Error> for CompletionError

Source§

fn from(source: Error) -> Self

Converts to this type from the input type.
Source§

impl From<MessageError> for CompletionError

Source§

fn from(error: MessageError) -> Self

Converts to this type from the input type.
Source§

impl From<ParseError> for CompletionError

Source§

fn from(source: ParseError) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> DebuggableStorage for T
where T: Any + Send + Sync + Debug,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T> ToStringFallible for T
where T: Display,

Source§

fn try_to_string(&self) -> Result<String, TryReserveError>

ToString::to_string, but without panic on OOM.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WasmCompatSend for T
where T: Send,

Source§

impl<T> WasmCompatSync for T
where T: Sync,

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more