2021-11-03 11:30:37 +01:00
|
|
|
/*
|
2023-01-29 10:24:29 +01:00
|
|
|
** Copyright (C) 2021-2023 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
2021-11-03 11:30:37 +01:00
|
|
|
**
|
|
|
|
** 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
|
|
|
|
** Free Software Foundation; either version 3, or (at your option) any
|
|
|
|
** later version.
|
|
|
|
**
|
|
|
|
** This program is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
** GNU General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with this program; if not, write to the Free Software Foundation,
|
|
|
|
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef MU_XAPIAN_UTILS_HH__
|
|
|
|
#define MU_XAPIAN_UTILS_HH__
|
|
|
|
|
|
|
|
#include <xapian.h>
|
2022-03-20 13:12:41 +01:00
|
|
|
#include <glib.h>
|
2022-04-28 21:49:45 +02:00
|
|
|
#include "mu-result.hh"
|
2021-11-03 11:30:37 +01:00
|
|
|
|
|
|
|
namespace Mu {
|
|
|
|
|
2022-05-23 22:55:11 +02:00
|
|
|
// LCOV_EXCL_START
|
|
|
|
|
2021-11-03 11:30:37 +01:00
|
|
|
// avoid exception-handling boilerplate.
|
2023-01-29 10:24:29 +01:00
|
|
|
template <typename Func> void
|
2021-11-03 11:30:37 +01:00
|
|
|
xapian_try(Func&& func) noexcept
|
|
|
|
try {
|
|
|
|
func();
|
|
|
|
} catch (const Xapian::Error& xerr) {
|
|
|
|
g_critical("%s: xapian error '%s'", __func__, xerr.get_msg().c_str());
|
|
|
|
} catch (const std::runtime_error& re) {
|
2023-01-29 10:24:29 +01:00
|
|
|
g_critical("%s: runtime error: %s", __func__, re.what());
|
2022-03-12 13:58:19 +01:00
|
|
|
} catch (const std::exception& e) {
|
2023-01-29 10:24:29 +01:00
|
|
|
g_critical("%s: caught std::exception: %s", __func__, e.what());
|
2021-11-03 11:30:37 +01:00
|
|
|
} catch (...) {
|
|
|
|
g_critical("%s: caught exception", __func__);
|
|
|
|
}
|
|
|
|
|
2023-01-29 10:24:29 +01:00
|
|
|
template <typename Func, typename Default = std::invoke_result<Func>> auto
|
2021-11-03 11:30:37 +01:00
|
|
|
xapian_try(Func&& func, Default&& def) noexcept -> std::decay_t<decltype(func())>
|
|
|
|
try {
|
|
|
|
return func();
|
2023-01-29 10:24:29 +01:00
|
|
|
} catch (const Xapian::DocNotFoundError& xerr) {
|
|
|
|
return static_cast<Default>(def);
|
2021-11-03 11:30:37 +01:00
|
|
|
} catch (const Xapian::Error& xerr) {
|
2023-01-29 10:24:29 +01:00
|
|
|
g_warning("%s: xapian error '%s'", __func__, xerr.get_msg().c_str());
|
2021-11-03 11:30:37 +01:00
|
|
|
return static_cast<Default>(def);
|
|
|
|
} catch (const std::runtime_error& re) {
|
2023-01-29 10:24:29 +01:00
|
|
|
g_critical("%s: runtime error: %s", __func__, re.what());
|
2021-11-03 11:30:37 +01:00
|
|
|
return static_cast<Default>(def);
|
2022-03-12 13:58:19 +01:00
|
|
|
} catch (const std::exception& e) {
|
2023-01-29 10:24:29 +01:00
|
|
|
g_critical("%s: caught std::exception: %s", __func__, e.what());
|
2022-03-12 13:58:19 +01:00
|
|
|
return static_cast<Default>(def);
|
2021-11-03 11:30:37 +01:00
|
|
|
} catch (...) {
|
|
|
|
g_critical("%s: caught exception", __func__);
|
|
|
|
return static_cast<Default>(def);
|
|
|
|
}
|
|
|
|
|
2022-04-28 21:49:45 +02:00
|
|
|
|
2023-01-29 10:24:29 +01:00
|
|
|
template <typename Func> auto
|
2022-04-28 21:49:45 +02:00
|
|
|
xapian_try_result(Func&& func) noexcept -> std::decay_t<decltype(func())>
|
|
|
|
try {
|
|
|
|
return func();
|
|
|
|
} catch (const Xapian::Error& xerr) {
|
|
|
|
return Err(Error::Code::Xapian, "%s", xerr.get_error_string());
|
|
|
|
} catch (const std::runtime_error& re) {
|
|
|
|
return Err(Error::Code::Internal, "runtime error: %s", re.what());
|
|
|
|
} catch (const std::exception& e) {
|
2023-01-29 10:24:29 +01:00
|
|
|
return Err(Error::Code::Internal, "caught std::exception: %s", e.what());
|
2022-04-28 21:49:45 +02:00
|
|
|
} catch (...) {
|
|
|
|
return Err(Error::Code::Internal, "caught exception");
|
|
|
|
}
|
|
|
|
|
2022-05-23 22:55:11 +02:00
|
|
|
// LCOV_EXCL_STOP
|
2022-04-28 21:49:45 +02:00
|
|
|
|
2021-11-03 11:30:37 +01:00
|
|
|
} // namespace Mu
|
|
|
|
|
|
|
|
#endif /* MU_ XAPIAN_UTILS_HH__ */
|