Skip to content

Commit

Permalink
refactor: Implement backtrace for Error correctly (#2871)
Browse files Browse the repository at this point in the history
Signed-off-by: Xuanwo <[email protected]>
  • Loading branch information
Xuanwo authored Aug 16, 2023
1 parent 2be8e65 commit 64ddcb6
Show file tree
Hide file tree
Showing 14 changed files with 130 additions and 47 deletions.
6 changes: 4 additions & 2 deletions bin/oay/src/services/webdav/webdav_dir_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
// specific language governing permissions and limitations
// under the License.

use dav_server::fs::{DavDirEntry, DavMetaData};
use dav_server::fs::DavDirEntry;
use dav_server::fs::DavMetaData;
use futures::FutureExt;
use opendal::{Entry, Operator};
use opendal::Entry;
use opendal::Operator;

use super::webdav_metadata::WebdavMetaData;

Expand Down
3 changes: 2 additions & 1 deletion bin/oay/src/services/webdav/webdav_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
// specific language governing permissions and limitations
// under the License.

use dav_server::fs::{DavMetaData, FsError};
use dav_server::fs::DavMetaData;
use dav_server::fs::FsError;
use opendal::Metadata;

#[derive(Debug, Clone)]
Expand Down
9 changes: 5 additions & 4 deletions bin/oay/src/services/webdav/webdavfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

use std::path::Path;
use std::pin::Pin;
use std::task::Poll::{Pending, Ready};
use std::task::Poll::Pending;
use std::task::Poll::Ready;

use dav_server::davpath::DavPath;
use dav_server::fs::DavDirEntry;
Expand All @@ -30,10 +31,10 @@ use futures_util::StreamExt;
use opendal::Lister;
use opendal::Operator;

use crate::services::webdav::webdav_dir_entry::WebDAVDirEntry;

use super::webdav_file::{convert_error, WebdavFile};
use super::webdav_file::convert_error;
use super::webdav_file::WebdavFile;
use super::webdav_metadata::WebdavMetaData;
use crate::services::webdav::webdav_dir_entry::WebDAVDirEntry;

#[derive(Clone)]
pub struct WebdavFs {
Expand Down
3 changes: 2 additions & 1 deletion bindings/haskell/src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
// specific language governing permissions and limitations
// under the License.

use std::ffi::{c_char, CString};
use std::ffi::c_char;
use std::ffi::CString;

pub struct HsLogger {
pub callback: extern "C" fn(u32, *const c_char),
Expand Down
6 changes: 4 additions & 2 deletions bindings/ocaml/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
// specific language governing permissions and limitations
// under the License.

use ::opendal as od;
use std::collections::{BTreeMap, HashMap};
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::str::FromStr;

use ::opendal as od;

mod operator;

pub fn new_operator(
Expand Down
5 changes: 3 additions & 2 deletions bindings/php/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@
// specific language governing permissions and limitations
// under the License.

use std::collections::HashMap;
use std::str::FromStr;

use ::opendal as od;
use ext_php_rs::binary::Binary;
use ext_php_rs::convert::FromZval;
use ext_php_rs::exception::PhpException;
use ext_php_rs::flags::DataType;
use ext_php_rs::prelude::*;
use ext_php_rs::types::Zval;
use std::collections::HashMap;
use std::str::FromStr;

#[php_class(name = "OpenDAL\\Operator")]
pub struct Operator(od::BlockingOperator);
Expand Down
7 changes: 3 additions & 4 deletions core/fuzz/fuzz_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@ use bytes::Bytes;
use libfuzzer_sys::arbitrary::Arbitrary;
use libfuzzer_sys::arbitrary::Unstructured;
use libfuzzer_sys::fuzz_target;
use rand::prelude::*;
use sha2::Digest;
use sha2::Sha256;

use opendal::raw::oio::ReadExt;
use opendal::raw::BytesRange;
use opendal::Operator;
use opendal::Result;
use rand::prelude::*;
use sha2::Digest;
use sha2::Sha256;

mod utils;

Expand Down
8 changes: 4 additions & 4 deletions core/fuzz/fuzz_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@

#![no_main]

use bytes::{Bytes, BytesMut};
use bytes::Bytes;
use bytes::BytesMut;
use libfuzzer_sys::arbitrary::Arbitrary;
use libfuzzer_sys::arbitrary::Unstructured;
use libfuzzer_sys::fuzz_target;
use opendal::Operator;
use opendal::Result;
use rand::prelude::*;
use sha2::Digest;
use sha2::Sha256;

use opendal::Operator;
use opendal::Result;

mod utils;

const MAX_DATA_SIZE: usize = 16 * 1024 * 1024;
Expand Down
3 changes: 1 addition & 2 deletions core/src/layers/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,8 @@ impl<I: oio::Page> oio::BlockingPage for BlockingWrapper<I> {
mod tests {
use once_cell::sync::Lazy;

use crate::types::Result;

use super::*;
use crate::types::Result;

static RUNTIME: Lazy<tokio::runtime::Runtime> = Lazy::new(|| {
tokio::runtime::Builder::new_multi_thread()
Expand Down
17 changes: 11 additions & 6 deletions core/src/services/postgresql/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,21 @@
// specific language governing permissions and limitations
// under the License.

use crate::raw::adapters::kv;
use crate::raw::*;
use crate::*;
use async_trait::async_trait;
use std::collections::HashMap;
use std::fmt::{Debug, Formatter};
use std::fmt::Debug;
use std::fmt::Formatter;
use std::str::FromStr;
use std::sync::Arc;

use async_trait::async_trait;
use tokio::sync::OnceCell;
use tokio_postgres::{Client, Config, Statement};
use tokio_postgres::Client;
use tokio_postgres::Config;
use tokio_postgres::Statement;

use crate::raw::adapters::kv;
use crate::raw::*;
use crate::*;

/// [Postgresql](https://www.postgresql.org/) services support.
#[doc = include_str!("docs.md")]
Expand Down
6 changes: 1 addition & 5 deletions core/src/types/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,7 @@ impl Entry {
/// let (path, meta) = entry.into_parts();
/// match meta.mode() {
/// EntryMode::FILE => {
/// println!(
/// "Handling file {} with size {}",
/// path,
/// meta.content_length()
/// )
/// println!("Handling file {} with size {}", path, meta.content_length())
/// }
/// EntryMode::DIR => {
/// println!("Handling dir {}", path)
Expand Down
87 changes: 81 additions & 6 deletions core/src/types/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
//! # }
//! ```

use std::backtrace::Backtrace;
use std::backtrace::BacktraceStatus;
use std::fmt;
use std::fmt::Debug;
use std::fmt::Display;
Expand Down Expand Up @@ -172,6 +174,65 @@ impl Display for ErrorStatus {
}

/// Error is the error struct returned by all opendal functions.
///
/// ## Display
///
/// Error can be displayed in two ways:
///
/// - Via `Display`: like `err.to_string()` or `format!("{err}")`
///
/// Error will be printed in a single line:
///
/// ```shell
/// Unexpected, context: { path: /path/to/file, called: send_async } => something wrong happened, source: networking error"
/// ```
///
/// - Via `Debug`: like `format!("{err:?}")`
///
/// Error will be printed in multi lines with more details and backtraces (if captured):
///
/// ```shell
/// Unexpected => something wrong happened
///
/// Context:
/// path: /path/to/file
/// called: send_async
///
/// Source: networking error
///
/// Backtrace:
/// 0: opendal::error::Error::new
/// at ./src/error.rs:197:24
/// 1: opendal::error::tests::generate_error
/// at ./src/error.rs:241:9
/// 2: opendal::error::tests::test_error_debug_with_backtrace::{{closure}}
/// at ./src/error.rs:305:41
/// ...
/// ```
///
/// - For conventional struct-style Debug representation, like `format!("{err:#?}")`:
///
/// ```shell
/// Error {
/// kind: Unexpected,
/// message: "something wrong happened",
/// status: Permanent,
/// operation: "Read",
/// context: [
/// (
/// "path",
/// "/path/to/file",
/// ),
/// (
/// "called",
/// "send_async",
/// ),
/// ],
/// source: Some(
/// "networking error",
/// ),
/// }
/// ```
pub struct Error {
kind: ErrorKind,
message: String,
Expand All @@ -180,6 +241,7 @@ pub struct Error {
operation: &'static str,
context: Vec<(&'static str, String)>,
source: Option<anyhow::Error>,
backtrace: Backtrace,
}

impl Display for Error {
Expand Down Expand Up @@ -236,12 +298,18 @@ impl Debug for Error {
writeln!(f)?;
writeln!(f, "Context:")?;
for (k, v) in self.context.iter() {
writeln!(f, " {k}: {v}")?;
writeln!(f, " {k}: {v}")?;
}
}
if let Some(source) = &self.source {
writeln!(f)?;
writeln!(f, "Source: {source:?}")?;
writeln!(f, "Source:")?;
writeln!(f, " {source:#}")?;
}
if self.backtrace.status() == BacktraceStatus::Captured {
writeln!(f)?;
writeln!(f, "Backtrace:")?;
writeln!(f, "{}", self.backtrace)?;
}

Ok(())
Expand All @@ -265,6 +333,9 @@ impl Error {
operation: "",
context: Vec::default(),
source: None,
// `Backtrace::capture()` will check if backtrace has been enabled
// internally. It's zero cost if backtrace is disabled.
backtrace: Backtrace::capture(),
}
}

Expand Down Expand Up @@ -359,6 +430,7 @@ impl From<Error> for io::Error {
mod tests {
use anyhow::anyhow;
use once_cell::sync::Lazy;
use pretty_assertions::assert_eq;

use super::*;

Expand All @@ -372,6 +444,7 @@ mod tests {
("called", "send_async".to_string()),
],
source: Some(anyhow!("networking error")),
backtrace: Backtrace::disabled(),
});

#[test]
Expand All @@ -380,7 +453,8 @@ mod tests {
assert_eq!(
s,
r#"Unexpected (permanent) at Read, context: { path: /path/to/file, called: send_async } => something wrong happened, source: networking error"#
)
);
println!("{:#?}", Lazy::force(&TEST_ERROR));
}

#[test]
Expand All @@ -391,10 +465,11 @@ mod tests {
r#"Unexpected (permanent) at Read => something wrong happened
Context:
path: /path/to/file
called: send_async
path: /path/to/file
called: send_async
Source: networking error
Source:
networking error
"#
)
}
Expand Down
Loading

0 comments on commit 64ddcb6

Please sign in to comment.