Documentation
// Copyright (c) 2026, Salesforce, Inc.,
// All rights reserved.
// For full license text, see the LICENSE.txt file

use std::error::Error;

use crate::proxy_wasm::types::Status;

use super::GrpcStatus;

/// The Errors that may occur when processing a gRPC request before sending it to upstream.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum GrpcCallError {
    /// Proxy status problem.
    #[error(transparent)]
    Proxy(#[from] GrpcProxyError),

    /// Invalid upstream.
    #[error("Invalid upstream '{0}'")]
    InvalidUpstream(String),

    /// Message encoding problem.
    #[error(transparent)]
    Encode(Box<dyn Error + Send + Sync>),
}

/// Proxy status problem.
#[derive(Debug, thiserror::Error)]
#[error("Proxy status problem: {0:?}")]
pub struct GrpcProxyError(Status);

impl GrpcProxyError {
    pub(super) fn new(status: Status) -> Self {
        Self(status)
    }
}

/// The Errors that may occur when processing a gRPC request.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum GrpcClientError {
    /// The Errors that may occur when processing a gRPC request before sending it to upstream.
    #[error(transparent)]
    Call(#[from] GrpcCallError),

    /// Request awaited on create context event.
    #[error("Request awaited on create context event")]
    AwaitedOnCreateContext,

    /// Response with empty content.
    #[error("Empty content")]
    EmptyContent,

    /// Message decoding problem.
    #[error(transparent)]
    Decode(Box<dyn Error + Send + Sync>),

    /// Bad gRPC status.
    #[error("Bad gRPC status: {0}")]
    Status(GrpcStatus),
}