Skip to content

Commit

Permalink
api: introduce feature error-backtrace
Browse files Browse the repository at this point in the history
Intoduce feature `error-backtrace` to reduce dependency of the
nydus-service crate.

Signed-off-by: Jiang Liu <gerry@linux.alibaba.com>
  • Loading branch information
jiangliu committed Jun 17, 2023
1 parent 9d89b8d commit 066863b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ openssl = { version = "0.10.48", features = ["vendored"] }
# pin openssl-src to bring in fix for https://rustsec.org/advisories/RUSTSEC-2022-0032
#openssl-src = { version = "111.22" }

nydus-api = { version = "0.3.0", path = "api", features = ["handler"] }
nydus-api = { version = "0.3.0", path = "api", features = ["error-backtrace", "handler"] }
nydus-builder = { version = "0.1.0", path = "builder" }
nydus-rafs = { version = "0.3.1", path = "rafs" }
nydus-service = { version = "0.3.0", path = "service", features = ["block-device"] }
Expand Down
12 changes: 7 additions & 5 deletions api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@ repository = "https://github.com/dragonflyoss/image-service"
edition = "2018"

[dependencies]
backtrace = "0.3"
libc = "0.2"
log = "0.4.8"
serde_json = "1.0.53"
toml = "0.5"

backtrace = { version = "0.3", optional = true }
dbs-uhttp = { version = "0.3.0", optional = true }
http = { version = "0.2.1", optional = true }
lazy_static = { version = "1.4.0", optional = true }
libc = "0.2"
log = "0.4.8"
mio = { version = "0.8", features = ["os-poll", "os-ext"], optional = true }
serde = { version = "1.0.110", features = ["rc", "serde_derive"] }
serde_json = "1.0.53"
toml = "0.5"
url = { version = "2.1.1", optional = true }

[dev-dependencies]
vmm-sys-util = { version = "0.10" }

[features]
handler = ["dbs-uhttp", "http", "lazy_static", "mio", "url"]
error-backtrace = ["backtrace"]
27 changes: 15 additions & 12 deletions api/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,29 @@
//
// SPDX-License-Identifier: Apache-2.0

use std::env;
use std::fmt::Debug;

use log::error;

/// Display error messages with line number, file path and optional backtrace.
pub fn make_error(err: std::io::Error, raw: impl Debug, file: &str, line: u32) -> std::io::Error {
if cfg!(debug_assertions) {
if let Ok(val) = env::var("RUST_BACKTRACE") {
pub fn make_error(
err: std::io::Error,
_raw: impl Debug,
_file: &str,
_line: u32,
) -> std::io::Error {
#[cfg(all(debug_assertions, feature = "error-backtrace"))]
{
if let Ok(val) = std::env::var("RUST_BACKTRACE") {
if val.trim() != "0" {
error!("Stack:\n{:?}", backtrace::Backtrace::new());
error!("Error:\n\t{:?}\n\tat {}:{}", raw, file, line);
log::error!("Stack:\n{:?}", backtrace::Backtrace::new());
log::error!("Error:\n\t{:?}\n\tat {}:{}", _raw, _file, _line);
return err;
}
}
log::error!(
"Error:\n\t{:?}\n\tat {}:{}\n\tnote: enable `RUST_BACKTRACE=1` env to display a backtrace",
_raw, _file, _line
);
}
error!(
"Error:\n\t{:?}\n\tat {}:{}\n\tnote: enable `RUST_BACKTRACE=1` env to display a backtrace",
raw, file, line
);
err
}

Expand Down

0 comments on commit 066863b

Please sign in to comment.