encore-dev 0.1.0

Rust SDK for Encore applications
Documentation
//! The SDK's public error type, mirroring `encore_sdk_core::api::ErrCode` but
//! owned and free of the core's `PValues`/axum internals.

use std::fmt::{self, Display};

use encore_sdk_core::api;

/// A gRPC-style error code, the same set the rest of Encore uses.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ErrCode {
    Canceled,
    Unknown,
    InvalidArgument,
    DeadlineExceeded,
    NotFound,
    AlreadyExists,
    PermissionDenied,
    ResourceExhausted,
    FailedPrecondition,
    Aborted,
    OutOfRange,
    Unimplemented,
    Internal,
    Unavailable,
    DataLoss,
    Unauthenticated,
}

/// An error returned by an Encore SDK operation or an API endpoint handler.
#[derive(Debug, Clone)]
pub struct Error {
    pub code: ErrCode,
    pub message: String,
}

impl Error {
    pub fn new(code: ErrCode, message: impl Into<String>) -> Self {
        Self {
            code,
            message: message.into(),
        }
    }

    pub fn internal(message: impl Into<String>) -> Self {
        Self::new(ErrCode::Internal, message)
    }

    pub fn invalid_argument(message: impl Into<String>) -> Self {
        Self::new(ErrCode::InvalidArgument, message)
    }

    pub fn not_found(message: impl Into<String>) -> Self {
        Self::new(ErrCode::NotFound, message)
    }

    pub fn unavailable(message: impl Into<String>) -> Self {
        Self::new(ErrCode::Unavailable, message)
    }

    pub fn unauthenticated(message: impl Into<String>) -> Self {
        Self::new(ErrCode::Unauthenticated, message)
    }

    /// Wrap any displayable error under `code`, used to bridge core/infra errors
    /// into the SDK's error type.
    pub(crate) fn wrap(code: ErrCode, cause: impl Display) -> Self {
        Self::new(code, cause.to_string())
    }
}

impl Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:?}: {}", self.code, self.message)
    }
}

impl std::error::Error for Error {}

impl From<api::ErrCode> for ErrCode {
    fn from(c: api::ErrCode) -> Self {
        match c {
            api::ErrCode::Canceled => ErrCode::Canceled,
            api::ErrCode::Unknown => ErrCode::Unknown,
            api::ErrCode::InvalidArgument => ErrCode::InvalidArgument,
            api::ErrCode::DeadlineExceeded => ErrCode::DeadlineExceeded,
            api::ErrCode::NotFound => ErrCode::NotFound,
            api::ErrCode::AlreadyExists => ErrCode::AlreadyExists,
            api::ErrCode::PermissionDenied => ErrCode::PermissionDenied,
            api::ErrCode::ResourceExhausted => ErrCode::ResourceExhausted,
            api::ErrCode::FailedPrecondition => ErrCode::FailedPrecondition,
            api::ErrCode::Aborted => ErrCode::Aborted,
            api::ErrCode::OutOfRange => ErrCode::OutOfRange,
            api::ErrCode::Unimplemented => ErrCode::Unimplemented,
            api::ErrCode::Internal => ErrCode::Internal,
            api::ErrCode::Unavailable => ErrCode::Unavailable,
            api::ErrCode::DataLoss => ErrCode::DataLoss,
            api::ErrCode::Unauthenticated => ErrCode::Unauthenticated,
        }
    }
}

impl From<api::Error> for Error {
    fn from(e: api::Error) -> Self {
        Self {
            code: e.code.into(),
            message: e.message,
        }
    }
}