baracuda_core/error.rs
1//! Error types shared across the baracuda crates.
2
3use std::path::PathBuf;
4
5use baracuda_types::{CudaStatus, CudaVersion};
6use thiserror::Error;
7
8/// An error raised by the dynamic loader.
9///
10/// These surface whenever an NVIDIA shared library or one of its symbols
11/// cannot be resolved at runtime — typically because CUDA is not installed,
12/// the installed driver is older than what baracuda was built against, or
13/// the user is on a platform NVIDIA doesn't support.
14///
15/// `#[non_exhaustive]` — new loader failure modes may land as CUDA
16/// adds entry points (`cuGetProcAddress` v2, the per-library minor-
17/// version checks). Match arms must include a `_ =>` catch-all.
18#[derive(Debug, Error)]
19#[non_exhaustive]
20pub enum LoaderError {
21 /// None of the candidate library filenames resolved anywhere on the
22 /// library search path.
23 #[error("could not load {library}: tried {candidates:?} across {search_paths} path(s)")]
24 LibraryNotFound {
25 /// Value field.
26 library: &'static str,
27 /// Value field.
28 candidates: Vec<&'static str>,
29 /// Value field.
30 search_paths: usize,
31 },
32
33 /// The library was loaded but did not export the requested symbol.
34 #[error("library '{library}' is missing symbol '{symbol}'")]
35 SymbolNotFound {
36 /// Value field.
37 library: &'static str,
38 /// Value field.
39 symbol: &'static str,
40 },
41
42 /// `cuGetProcAddress` returned `CU_GET_PROC_ADDRESS_VERSION_NOT_SUFFICIENT`
43 /// for `symbol`: the installed driver does not provide it at the version
44 /// baracuda asked for.
45 #[error("symbol '{symbol}' requires {required} but baracuda's driver loader sees {installed}")]
46 VersionTooOld {
47 /// Value field.
48 symbol: &'static str,
49 /// Value field.
50 required: CudaVersion,
51 /// Value field.
52 installed: CudaVersion,
53 },
54
55 /// Raw `libloading` error — kept for platform-specific diagnostics that
56 /// the other variants can't express (e.g. a missing dependency on a
57 /// chained `.so`).
58 #[error("{0}")]
59 Libloading(#[from] libloading::Error),
60
61 /// baracuda does not target this platform (e.g. macOS).
62 #[error("baracuda does not support {platform}; NVIDIA driver is only available on Linux and Windows")]
63 UnsupportedPlatform {
64 /// Name of the unsupported platform (e.g. "macOS").
65 platform: &'static str,
66 },
67}
68
69impl LoaderError {
70 /// Convenience constructor for the common case of "tried these names,
71 /// none worked".
72 pub fn library_not_found(library: &'static str, candidates: &[&'static str]) -> Self {
73 Self::LibraryNotFound {
74 library,
75 candidates: candidates.to_vec(),
76 search_paths: 0,
77 }
78 }
79
80 /// As above, but records how many directories were searched.
81 pub fn library_not_found_with_search(
82 library: &'static str,
83 candidates: &[&'static str],
84 search_path_count: usize,
85 ) -> Self {
86 Self::LibraryNotFound {
87 library,
88 candidates: candidates.to_vec(),
89 search_paths: search_path_count,
90 }
91 }
92}
93
94/// A generic error enum for any safe wrapper crate over a single NVIDIA
95/// library. Safe crates may use this directly or compose their own richer
96/// `Error` enum out of its variants.
97///
98/// `#[non_exhaustive]` — new error variants may land as new failure modes
99/// are surfaced by NVIDIA libraries. Match arms must include a `_ =>`
100/// catch-all.
101#[derive(Debug, Error)]
102#[non_exhaustive]
103pub enum Error<S>
104where
105 S: CudaStatus + Send + Sync + 'static,
106{
107 /// The library returned a non-success status code.
108 #[error("{} returned {} ({}): {}", .status.library(), .status.name(), .status.code(), .status.description())]
109 Status {
110 /// The non-success status code returned by the underlying library.
111 status: S,
112 },
113
114 /// The dynamic loader failed.
115 #[error(transparent)]
116 Loader(#[from] LoaderError),
117
118 /// The requested API is newer than the installed driver supports.
119 #[error("{api} requires {since}; install a newer driver to use it")]
120 FeatureNotSupported {
121 /// Value field.
122 api: &'static str,
123 /// Value field.
124 since: CudaVersion,
125 },
126}
127
128impl<S> Error<S>
129where
130 S: CudaStatus + Send + Sync + 'static,
131{
132 /// Treat a raw status code as a `Result`. Success codes yield `Ok(())`,
133 /// all others yield `Err(Error::Status { .. })`.
134 pub fn check(status: S) -> Result<(), Self> {
135 if status.is_success() {
136 Ok(())
137 } else {
138 Err(Self::Status { status })
139 }
140 }
141}
142
143/// A library-erased error, useful at process boundaries where the caller
144/// doesn't want to parameterize over every NVIDIA library's status enum.
145///
146/// `#[non_exhaustive]` — new error categories may land as the workspace
147/// adds backends. Match arms must include a `_ =>` catch-all.
148#[derive(Debug, Error)]
149#[non_exhaustive]
150pub enum BaracudaError {
151 /// A status code from any NVIDIA library.
152 #[error("{library} returned {name} ({code}): {description}")]
153 Status {
154 /// Value field.
155 library: &'static str,
156 /// Value field.
157 name: &'static str,
158 /// Value field.
159 description: &'static str,
160 /// Value field.
161 code: i32,
162 },
163
164 /// The dynamic loader failed.
165 #[error(transparent)]
166 Loader(#[from] LoaderError),
167
168 /// The requested API is newer than the installed driver supports.
169 #[error("{api} requires {since}; install a newer driver to use it")]
170 FeatureNotSupported {
171 /// Value field.
172 api: &'static str,
173 /// Value field.
174 since: CudaVersion,
175 },
176
177 /// For sources that want to attach a path or other context (e.g. a
178 /// missing PTX file).
179 #[error("{context}")]
180 Context {
181 /// Free-form context attached by the failing call site.
182 context: &'static str,
183 },
184}
185
186impl<S> From<Error<S>> for BaracudaError
187where
188 S: CudaStatus + Send + Sync + 'static,
189{
190 fn from(err: Error<S>) -> Self {
191 match err {
192 Error::Status { status } => BaracudaError::Status {
193 library: status.library(),
194 name: status.name(),
195 description: status.description(),
196 code: status.code(),
197 },
198 Error::Loader(l) => BaracudaError::Loader(l),
199 Error::FeatureNotSupported { api, since } => {
200 BaracudaError::FeatureNotSupported { api, since }
201 }
202 }
203 }
204}
205
206/// Path-returning variant used by `find_library` probes. (Kept out of public
207/// API surface for now — re-exported here so doc-links work.)
208#[allow(dead_code)]
209pub(crate) type PathList = Vec<PathBuf>;