diff --git a/include/dwarfs/error.h b/include/dwarfs/error.h index 88e366f29..96650fe11 100644 --- a/include/dwarfs/error.h +++ b/include/dwarfs/error.h @@ -36,10 +36,7 @@ class error : public std::exception { int line() const { return line_; } protected: - error(std::string const& s, char const* file, int line) noexcept - : what_(s) - , file_(file) - , line_(line) {} + error(std::string const& s, char const* file, int line) noexcept; private: std::string what_; diff --git a/src/dwarfs/error.cpp b/src/dwarfs/error.cpp index 26f0fe408..f01a58f9d 100644 --- a/src/dwarfs/error.cpp +++ b/src/dwarfs/error.cpp @@ -24,6 +24,8 @@ #include #include +#include + #include #ifdef DWARFS_USE_EXCEPTION_TRACER @@ -47,6 +49,11 @@ namespace { } // namespace +error::error(std::string const& s, char const* file, int line) noexcept + : what_{fmt::format("{} [{}:{}]", s, file, line)} + , file_{file} + , line_{line} {} + system_error::system_error(char const* file, int line) noexcept : system_error(errno, file, line) {} diff --git a/src/dwarfs/filesystem_v2.cpp b/src/dwarfs/filesystem_v2.cpp index 9603fcfb1..835ec913f 100644 --- a/src/dwarfs/filesystem_v2.cpp +++ b/src/dwarfs/filesystem_v2.cpp @@ -775,11 +775,8 @@ int filesystem_::check(filesystem_check_level level, DWARFS_THROW(runtime_error, "duplicate section: " + s.name()); } } - } catch (error const& e) { - LOG_ERROR << e.what() << " [" << e.file() << ":" << e.line() << "]"; - ++errors; - } catch (...) { - LOG_ERROR << folly::exceptionStr(std::current_exception()); + } catch (std::exception const& e) { + LOG_ERROR << folly::exceptionStr(e); ++errors; } } @@ -787,11 +784,8 @@ int filesystem_::check(filesystem_check_level level, if (level == filesystem_check_level::FULL) { try { meta_.check_consistency(); - } catch (error const& e) { - LOG_ERROR << e.what() << " [" << e.file() << ":" << e.line() << "]"; - ++errors; - } catch (...) { - LOG_ERROR << folly::exceptionStr(std::current_exception()); + } catch (std::exception const& e) { + LOG_ERROR << folly::exceptionStr(e); ++errors; } } diff --git a/src/dwarfs/inode_reader_v2.cpp b/src/dwarfs/inode_reader_v2.cpp index 0d8e86f9f..16ed3cf84 100644 --- a/src/dwarfs/inode_reader_v2.cpp +++ b/src/dwarfs/inode_reader_v2.cpp @@ -279,8 +279,6 @@ inode_reader_::read_internal(uint32_t inode, size_t size, num_read += br.size(); } return num_read; - } catch (runtime_error const& e) { - LOG_ERROR << e.what(); } catch (...) { LOG_ERROR << folly::exceptionStr(std::current_exception()); } diff --git a/src/dwarfs/safe_main.cpp b/src/dwarfs/safe_main.cpp index aa68c7834..6a6070014 100644 --- a/src/dwarfs/safe_main.cpp +++ b/src/dwarfs/safe_main.cpp @@ -42,18 +42,9 @@ int safe_main(std::function fn) { terminal::setup(); return fn(); - } catch (system_error const& e) { - std::cerr << "ERROR: " << folly::exceptionStr(e) << " [" << e.file() << ":" - << e.line() << "]\n"; - dump_exceptions(); - } catch (error const& e) { - std::cerr << "ERROR: " << folly::exceptionStr(e) << " [" << e.file() << ":" - << e.line() << "]\n"; - dump_exceptions(); - } catch (std::exception const& e) { - std::cerr << "ERROR: " << folly::exceptionStr(e) << "\n"; - dump_exceptions(); } catch (...) { + std::cerr << "ERROR: " << folly::exceptionStr(std::current_exception()) + << "\n"; dump_exceptions(); } return 1; diff --git a/src/dwarfs/scanner.cpp b/src/dwarfs/scanner.cpp index 1e3320dd4..6420b6533 100644 --- a/src/dwarfs/scanner.cpp +++ b/src/dwarfs/scanner.cpp @@ -421,7 +421,7 @@ scanner_::add_entry(std::filesystem::path const& name, return pe; } catch (const std::system_error& e) { - LOG_ERROR << "error reading entry: " << e.what(); + LOG_ERROR << "error reading entry: " << folly::exceptionStr(e); prog.errors++; } diff --git a/src/dwarfs_main.cpp b/src/dwarfs_main.cpp index b59e2b76c..234e5c5e6 100644 --- a/src/dwarfs_main.cpp +++ b/src/dwarfs_main.cpp @@ -273,10 +273,10 @@ void op_lookup(fuse_req_t req, fuse_ino_t parent, char const* name) { } } } catch (dwarfs::system_error const& e) { - LOG_ERROR << e.what(); + LOG_ERROR << folly::exceptionStr(e); err = e.get_errno(); } catch (std::exception const& e) { - LOG_ERROR << e.what(); + LOG_ERROR << folly::exceptionStr(e); err = EIO; } @@ -303,10 +303,10 @@ int op_getattr_common(LogProxy& log_, dwarfs_userdata& userdata, } } } catch (dwarfs::system_error const& e) { - LOG_ERROR << e.what(); + LOG_ERROR << folly::exceptionStr(e); err = e.get_errno(); } catch (std::exception const& e) { - LOG_ERROR << e.what(); + LOG_ERROR << folly::exceptionStr(e); err = EIO; } @@ -357,10 +357,10 @@ int op_access_common(LogProxy& log_, dwarfs_userdata& userdata, int mode, err = userdata.fs.access(*entry, mode, uid, gid); } } catch (dwarfs::system_error const& e) { - LOG_ERROR << e.what(); + LOG_ERROR << folly::exceptionStr(e); err = e.get_errno(); } catch (std::exception const& e) { - LOG_ERROR << e.what(); + LOG_ERROR << folly::exceptionStr(e); err = EIO; } @@ -411,10 +411,10 @@ int op_readlink_common(LogProxy& log_, dwarfs_userdata& userdata, err = userdata.fs.readlink(*entry, str, readlink_mode::unix); } } catch (dwarfs::system_error const& e) { - LOG_ERROR << e.what(); + LOG_ERROR << folly::exceptionStr(e); err = e.get_errno(); } catch (std::exception const& e) { - LOG_ERROR << e.what(); + LOG_ERROR << folly::exceptionStr(e); err = EIO; } @@ -491,10 +491,10 @@ int op_open_common(LogProxy& log_, dwarfs_userdata& userdata, } } } catch (dwarfs::system_error const& e) { - LOG_ERROR << e.what(); + LOG_ERROR << folly::exceptionStr(e); err = e.get_errno(); } catch (std::exception const& e) { - LOG_ERROR << e.what(); + LOG_ERROR << folly::exceptionStr(e); err = EIO; } @@ -569,10 +569,10 @@ void op_read(fuse_req_t req, fuse_ino_t ino, size_t size, file_off_t off, err = EIO; } } catch (dwarfs::system_error const& e) { - LOG_ERROR << e.what(); + LOG_ERROR << folly::exceptionStr(e); err = e.get_errno(); } catch (std::exception const& e) { - LOG_ERROR << e.what(); + LOG_ERROR << folly::exceptionStr(e); err = EIO; } @@ -602,10 +602,10 @@ int op_read(char const* path, char* buf, size_t size, native_off_t off, err = rv; } catch (dwarfs::system_error const& e) { - LOG_ERROR << e.what(); + LOG_ERROR << folly::exceptionStr(e); err = -e.get_errno(); } catch (std::exception const& e) { - LOG_ERROR << e.what(); + LOG_ERROR << folly::exceptionStr(e); err = -EIO; } @@ -672,10 +672,10 @@ void op_readdir(fuse_req_t req, fuse_ino_t ino, size_t size, file_off_t off, err = ENOTDIR; } } catch (dwarfs::system_error const& e) { - LOG_ERROR << e.what(); + LOG_ERROR << folly::exceptionStr(e); err = e.get_errno(); } catch (std::exception const& e) { - LOG_ERROR << e.what(); + LOG_ERROR << folly::exceptionStr(e); err = EIO; } @@ -731,10 +731,10 @@ int op_readdir(char const* path, void* buf, fuse_fill_dir_t filler, err = -ENOTDIR; } } catch (dwarfs::system_error const& e) { - LOG_ERROR << e.what(); + LOG_ERROR << folly::exceptionStr(e); err = -e.get_errno(); } catch (std::exception const& e) { - LOG_ERROR << e.what(); + LOG_ERROR << folly::exceptionStr(e); err = -EIO; } @@ -763,10 +763,10 @@ int op_statfs_common(LogProxy& log_, dwarfs_userdata& userdata, #endif } } catch (dwarfs::system_error const& e) { - LOG_ERROR << e.what(); + LOG_ERROR << folly::exceptionStr(e); err = e.get_errno(); } catch (std::exception const& e) { - LOG_ERROR << e.what(); + LOG_ERROR << folly::exceptionStr(e); err = EIO; } @@ -867,10 +867,10 @@ void op_getxattr(fuse_req_t req, fuse_ino_t ino, char const* name, } } } catch (dwarfs::system_error const& e) { - LOG_ERROR << e.what(); + LOG_ERROR << folly::exceptionStr(e); err = e.get_errno(); } catch (std::exception const& e) { - LOG_ERROR << e.what(); + LOG_ERROR << folly::exceptionStr(e); err = EIO; } @@ -922,10 +922,10 @@ void op_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size) { return; } } catch (dwarfs::system_error const& e) { - LOG_ERROR << e.what(); + LOG_ERROR << folly::exceptionStr(e); err = e.get_errno(); } catch (std::exception const& e) { - LOG_ERROR << e.what(); + LOG_ERROR << folly::exceptionStr(e); err = EIO; } @@ -1329,11 +1329,11 @@ int dwarfs_main(int argc, sys_char** argv, iolayer const& iol) { parse_time_with_unit(opts.cache_tidy_max_age_str); } } - } catch (runtime_error const& e) { - iol.err << "error: " << e.what() << "\n"; - return 1; } catch (std::filesystem::filesystem_error const& e) { - iol.err << e.what() << "\n"; + iol.err << folly::exceptionStr(e) << "\n"; + return 1; + } catch (std::exception const& e) { + iol.err << "error: " << folly::exceptionStr(e) << "\n"; return 1; } @@ -1359,7 +1359,7 @@ int dwarfs_main(int argc, sys_char** argv, iolayer const& iol) { load_filesystem(userdata); } } catch (std::exception const& e) { - LOG_ERROR << "error initializing file system: " << e.what(); + LOG_ERROR << "error initializing file system: " << folly::exceptionStr(e); return 1; } diff --git a/src/dwarfsbench_main.cpp b/src/dwarfsbench_main.cpp index da11572b9..92a746876 100644 --- a/src/dwarfsbench_main.cpp +++ b/src/dwarfsbench_main.cpp @@ -117,8 +117,8 @@ int dwarfsbench_main(int argc, sys_char** argv, iolayer const& iol) { int fh = fs.open(inode_data); fs.read(fh, buf.data(), buf.size()); } - } catch (runtime_error const& e) { - iol.err << "error: " << e.what() << "\n"; + } catch (std::exception const& e) { + iol.err << "error: " << folly::exceptionStr(e) << "\n"; } catch (...) { iol.err << "error: " << folly::exceptionStr(std::current_exception()) << "\n"; @@ -129,8 +129,8 @@ int dwarfsbench_main(int argc, sys_char** argv, iolayer const& iol) { }); wg.wait(); - } catch (runtime_error const& e) { - iol.err << "error: " << e.what() << "\n"; + } catch (std::exception const& e) { + iol.err << "error: " << folly::exceptionStr(e) << "\n"; return 1; } diff --git a/src/dwarfsck_main.cpp b/src/dwarfsck_main.cpp index 24bd4d1b0..199fde41c 100644 --- a/src/dwarfsck_main.cpp +++ b/src/dwarfsck_main.cpp @@ -42,6 +42,7 @@ #include "dwarfs/options.h" #include "dwarfs/os_access.h" #include "dwarfs/tool.h" +#include "dwarfs/util.h" #include "dwarfs_tool_main.h" namespace dwarfs { @@ -189,13 +190,7 @@ int dwarfsck_main(int argc, sys_char** argv, iolayer const& iol) { } } } - } catch (system_error const& e) { - iol.err << folly::exceptionStr(e) << "\n"; - return 1; - } catch (runtime_error const& e) { - iol.err << folly::exceptionStr(e) << "\n"; - return 1; - } catch (std::system_error const& e) { + } catch (std::exception const& e) { iol.err << folly::exceptionStr(e) << "\n"; return 1; } diff --git a/src/dwarfsextract_main.cpp b/src/dwarfsextract_main.cpp index 969bfabea..d1895e4dc 100644 --- a/src/dwarfsextract_main.cpp +++ b/src/dwarfsextract_main.cpp @@ -192,13 +192,7 @@ int dwarfsextract_main(int argc, sys_char** argv, iolayer const& iol) { if (perfmon) { perfmon->summarize(iol.err); } - } catch (runtime_error const& e) { - iol.err << folly::exceptionStr(e) << "\n"; - return 1; - } catch (system_error const& e) { - iol.err << folly::exceptionStr(e) << "\n"; - return 1; - } catch (std::system_error const& e) { + } catch (std::exception const& e) { iol.err << folly::exceptionStr(e) << "\n"; return 1; } diff --git a/src/mkdwarfs_main.cpp b/src/mkdwarfs_main.cpp index d0ff564ed..68a03ae2d 100644 --- a/src/mkdwarfs_main.cpp +++ b/src/mkdwarfs_main.cpp @@ -1296,11 +1296,8 @@ int mkdwarfs_main(int argc, sys_char** argv, iolayer const& iol) { options.inode.categorizer_mgr.reset(); } - } catch (runtime_error const& e) { - LOG_ERROR << e.what() << " [" << e.file() << ":" << e.line() << "]"; - return 1; - } catch (system_error const& e) { - LOG_ERROR << e.what() << " [" << e.file() << ":" << e.line() << "]"; + } catch (std::exception const& e) { + LOG_ERROR << folly::exceptionStr(e); return 1; } diff --git a/test/block_cache_test.cpp b/test/block_cache_test.cpp index 6cdc602c6..232231d44 100644 --- a/test/block_cache_test.cpp +++ b/test/block_cache_test.cpp @@ -81,7 +81,7 @@ TEST(block_range, uncompressed) { EXPECT_THAT([] { block_range range(nullptr, 0, 0); }, ::testing::ThrowsMessage( - "block_range: block data is null")); + ::testing::HasSubstr("block_range: block data is null"))); } TEST(block_range, compressed) { @@ -109,7 +109,7 @@ TEST(block_range, compressed) { block_range range(block, 0, 0); }, ::testing::ThrowsMessage( - "block_range: block data is null")); + ::testing::HasSubstr("block_range: block data is null"))); EXPECT_THAT( [&] { @@ -117,5 +117,5 @@ TEST(block_range, compressed) { block_range range(block, 100, 1); }, ::testing::ThrowsMessage( - "block_range: size out of range (101 > 100)")); + ::testing::HasSubstr("block_range: size out of range (101 > 100)"))); } diff --git a/test/error_test.cpp b/test/error_test.cpp index b202a95c9..879480a90 100644 --- a/test/error_test.cpp +++ b/test/error_test.cpp @@ -22,6 +22,8 @@ #include #include +#include + #include #include @@ -57,7 +59,8 @@ TEST(error_test, runtime_error) { test_throw_runtime_error(true); FAIL() << "expected runtime_error to be thrown"; } catch (const runtime_error& e) { - EXPECT_EQ("my test error", std::string(e.what())); + EXPECT_EQ(fmt::format("my test error [{}:{}]", e.file(), e.line()), + std::string(e.what())); EXPECT_EQ("error_test.cpp", std::filesystem::path(e.file()).filename().string()); EXPECT_EQ(expected_line, e.line());