utils: improve xapian exception handling / reporting

This commit is contained in:
Dirk-Jan C. Binnema
2023-01-29 11:24:29 +02:00
parent 29dc90486b
commit 40c3d3568e

View File

@ -1,5 +1,5 @@
/* /*
** Copyright (C) 2021 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl> ** Copyright (C) 2021-2023 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
** **
** This program is free software; you can redistribute it and/or modify it ** This program is free software; you can redistribute it and/or modify it
** under the terms of the GNU General Public License as published by the ** under the terms of the GNU General Public License as published by the
@ -29,34 +29,34 @@ namespace Mu {
// LCOV_EXCL_START // LCOV_EXCL_START
// avoid exception-handling boilerplate. // avoid exception-handling boilerplate.
template <typename Func> template <typename Func> void
void
xapian_try(Func&& func) noexcept xapian_try(Func&& func) noexcept
try { try {
func(); func();
} catch (const Xapian::Error& xerr) { } catch (const Xapian::Error& xerr) {
g_critical("%s: xapian error '%s'", __func__, xerr.get_msg().c_str()); g_critical("%s: xapian error '%s'", __func__, xerr.get_msg().c_str());
} catch (const std::runtime_error& re) { } catch (const std::runtime_error& re) {
g_critical("%s: error: %s", __func__, re.what()); g_critical("%s: runtime error: %s", __func__, re.what());
} catch (const std::exception& e) { } catch (const std::exception& e) {
g_critical("%s: caught exception: %s", __func__, e.what()); g_critical("%s: caught std::exception: %s", __func__, e.what());
} catch (...) { } catch (...) {
g_critical("%s: caught exception", __func__); g_critical("%s: caught exception", __func__);
} }
template <typename Func, typename Default = std::invoke_result<Func>> template <typename Func, typename Default = std::invoke_result<Func>> auto
auto
xapian_try(Func&& func, Default&& def) noexcept -> std::decay_t<decltype(func())> xapian_try(Func&& func, Default&& def) noexcept -> std::decay_t<decltype(func())>
try { try {
return func(); return func();
} catch (const Xapian::DocNotFoundError& xerr) {
return static_cast<Default>(def);
} catch (const Xapian::Error& xerr) { } catch (const Xapian::Error& xerr) {
g_critical("%s: xapian error '%s'", __func__, xerr.get_msg().c_str()); g_warning("%s: xapian error '%s'", __func__, xerr.get_msg().c_str());
return static_cast<Default>(def); return static_cast<Default>(def);
} catch (const std::runtime_error& re) { } catch (const std::runtime_error& re) {
g_critical("%s: error: %s", __func__, re.what()); g_critical("%s: runtime error: %s", __func__, re.what());
return static_cast<Default>(def); return static_cast<Default>(def);
} catch (const std::exception& e) { } catch (const std::exception& e) {
g_critical("%s: caught exception: %s", __func__, e.what()); g_critical("%s: caught std::exception: %s", __func__, e.what());
return static_cast<Default>(def); return static_cast<Default>(def);
} catch (...) { } catch (...) {
g_critical("%s: caught exception", __func__); g_critical("%s: caught exception", __func__);
@ -64,8 +64,7 @@ try {
} }
template <typename Func> template <typename Func> auto
auto
xapian_try_result(Func&& func) noexcept -> std::decay_t<decltype(func())> xapian_try_result(Func&& func) noexcept -> std::decay_t<decltype(func())>
try { try {
return func(); return func();
@ -74,7 +73,7 @@ try {
} catch (const std::runtime_error& re) { } catch (const std::runtime_error& re) {
return Err(Error::Code::Internal, "runtime error: %s", re.what()); return Err(Error::Code::Internal, "runtime error: %s", re.what());
} catch (const std::exception& e) { } catch (const std::exception& e) {
return Err(Error::Code::Internal, "caught exception: %s", e.what()); return Err(Error::Code::Internal, "caught std::exception: %s", e.what());
} catch (...) { } catch (...) {
return Err(Error::Code::Internal, "caught exception"); return Err(Error::Code::Internal, "caught exception");
} }