Skip to main content

ruxla_pjrt/
error.rs

1//! Error types.
2
3use std::path::PathBuf;
4
5/// A specialized [`Result`](std::result::Result) for PJRT operations.
6pub type Result<T, E = Error> = std::result::Result<T, E>;
7
8/// Errors returned by this crate.
9#[derive(Debug, thiserror::Error)]
10#[non_exhaustive]
11pub enum Error {
12    /// The plugin's shared library could not be loaded.
13    #[error("failed to load PJRT plugin `{}`", path.display())]
14    Load {
15        /// The path of the plugin.
16        path: PathBuf,
17        /// The underlying error from the dynamic loader.
18        #[source]
19        source: libloading::Error,
20    },
21
22    /// The plugin does not provide a function this crate needs.
23    ///
24    /// This happens when the plugin implements an older version of the PJRT
25    /// C API than the one this crate was built against.
26    #[error("the PJRT plugin does not provide `{0}`")]
27    MissingFunction(&'static str),
28
29    /// A PJRT function returned an error.
30    #[error("PJRT error ({code:?}): {message}")]
31    Pjrt {
32        /// The error code.
33        code: ErrorCode,
34        /// The error message from the plugin.
35        message: String,
36    },
37
38    /// A host buffer does not match the shape and element type it is used with.
39    #[error("host buffer mismatch: {0}")]
40    HostBuffer(String),
41}
42
43/// Error codes of the PJRT C API.
44///
45/// These follow the canonical error codes of Abseil (`absl::StatusCode`).
46#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
47#[non_exhaustive]
48// The variants are Abseil's status codes, documented there.
49#[allow(missing_docs)]
50pub enum ErrorCode {
51    Cancelled,
52    Unknown,
53    InvalidArgument,
54    DeadlineExceeded,
55    NotFound,
56    AlreadyExists,
57    PermissionDenied,
58    ResourceExhausted,
59    FailedPrecondition,
60    Aborted,
61    OutOfRange,
62    Unimplemented,
63    Internal,
64    Unavailable,
65    DataLoss,
66    Unauthenticated,
67    /// A code that this crate does not know.
68    Other(u32),
69}
70
71impl ErrorCode {
72    pub(crate) fn from_raw(code: u32) -> Self {
73        match code {
74            1 => Self::Cancelled,
75            2 => Self::Unknown,
76            3 => Self::InvalidArgument,
77            4 => Self::DeadlineExceeded,
78            5 => Self::NotFound,
79            6 => Self::AlreadyExists,
80            7 => Self::PermissionDenied,
81            8 => Self::ResourceExhausted,
82            9 => Self::FailedPrecondition,
83            10 => Self::Aborted,
84            11 => Self::OutOfRange,
85            12 => Self::Unimplemented,
86            13 => Self::Internal,
87            14 => Self::Unavailable,
88            15 => Self::DataLoss,
89            16 => Self::Unauthenticated,
90            other => Self::Other(other),
91        }
92    }
93}
94
95#[cfg(test)]
96mod tests {
97    use super::*;
98
99    #[test]
100    fn error_code_from_raw_matches_absl() {
101        assert_eq!(ErrorCode::from_raw(3), ErrorCode::InvalidArgument);
102        assert_eq!(ErrorCode::from_raw(16), ErrorCode::Unauthenticated);
103        assert_eq!(ErrorCode::from_raw(99), ErrorCode::Other(99));
104    }
105}