Skip to content

Commit fe0cda7

Browse files
committed
feat compile-time feature-gated Rust backtrace for JNI error diagnostics
1 parent 75b8d2a commit fe0cda7

5 files changed

Lines changed: 189 additions & 11 deletions

File tree

java/lance-jni/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ crate-type = ["cdylib"]
1616
default = []
1717

1818
[dependencies]
19-
lance = { path = "../../rust/lance", features = ["substrait"] }
19+
lance = { path = "../../rust/lance", features = ["substrait", "backtrace"] }
2020
lance-datafusion = { path = "../../rust/lance-datafusion" }
2121
lance-encoding = { path = "../../rust/lance-encoding" }
2222
lance-linalg = { path = "../../rust/lance-linalg" }
2323
lance-index = { path = "../../rust/lance-index" }
2424
lance-io = { path = "../../rust/lance-io" }
2525
lance-namespace = { path = "../../rust/lance-namespace" }
2626
lance-namespace-impls = { path = "../../rust/lance-namespace-impls", features = ["rest", "rest-adapter"] }
27-
lance-core = { path = "../../rust/lance-core" }
27+
lance-core = { path = "../../rust/lance-core", features = ["backtrace"] }
2828
lance-file = { path = "../../rust/lance-file" }
2929
lance-table = { path = "../../rust/lance-table" }
3030
arrow = { version = "57.1", features = ["ffi"] }
@@ -51,7 +51,7 @@ prost-types = "0.14.1"
5151
chrono = "0.4.41"
5252

5353
[profile.dev]
54-
debug = "line-tables-only"
54+
debug = true
5555
incremental = false
5656

5757
# This rule applies to every package except workspace members (dependencies

java/lance-jni/src/error.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,28 +175,36 @@ impl std::fmt::Display for Error {
175175

176176
impl From<LanceError> for Error {
177177
fn from(err: LanceError) -> Self {
178+
let backtrace_suffix = err
179+
.backtrace()
180+
.map(|bt| format!("\n\nRust backtrace:\n{}", bt))
181+
.unwrap_or_default();
182+
let message = format!("{}{}", err, backtrace_suffix);
183+
178184
match &err {
179185
LanceError::DatasetNotFound { .. }
180186
| LanceError::DatasetAlreadyExists { .. }
181187
| LanceError::CommitConflict { .. }
182-
| LanceError::InvalidInput { .. } => Self::input_error(err.to_string()),
183-
LanceError::IO { .. } => Self::io_error(err.to_string()),
184-
LanceError::NotSupported { .. } => Self::unsupported_error(err.to_string()),
185-
LanceError::NotFound { .. } => Self::io_error(err.to_string()),
188+
| LanceError::InvalidInput { .. } => Self::input_error(message),
189+
LanceError::IO { .. } => Self::io_error(message),
190+
LanceError::NotSupported { .. } => Self::unsupported_error(message),
191+
LanceError::NotFound { .. } => Self::io_error(message),
186192
LanceError::Namespace { source, .. } => {
187193
// Try to downcast to NamespaceError and get the error code
188194
if let Some(ns_err) = source.downcast_ref::<NamespaceError>() {
189-
Self::namespace_error(ns_err.code().as_u32(), ns_err.to_string())
195+
let ns_message =
196+
format!("{}{}", ns_err, backtrace_suffix);
197+
Self::namespace_error(ns_err.code().as_u32(), ns_message)
190198
} else {
191199
log::warn!(
192200
"Failed to downcast NamespaceError source, falling back to runtime error. \
193201
This may indicate a version mismatch. Source type: {:?}",
194202
source
195203
);
196-
Self::runtime_error(err.to_string())
204+
Self::runtime_error(message)
197205
}
198206
}
199-
_ => Self::runtime_error(err.to_string()),
207+
_ => Self::runtime_error(message),
200208
}
201209
}
202210
}

rust/lance-core/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ proptest.workspace = true
5555
rstest.workspace = true
5656

5757
[features]
58+
# Capture Rust backtraces in error types. When disabled (the default),
59+
# the backtrace field is zero-sized with no overhead. At runtime, capture
60+
# is still gated by RUST_BACKTRACE=1.
61+
backtrace = []
5862
datafusion = ["dep:datafusion-common", "dep:datafusion-sql"]
5963

6064
[lints]

0 commit comments

Comments
 (0)