ORE Studio 0.0.4
Loading...
Searching...
No Matches
ExceptionHelper.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * Copyright (C) 2025 Marco Craveiro <marco.craveiro@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 3 of the License, or (at your option) any later
8 * version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 */
20#ifndef ORES_QT_EXCEPTION_HELPER_HPP
21#define ORES_QT_EXCEPTION_HELPER_HPP
22
23#include <optional>
24#include <QString>
25#include <exception>
26#include <boost/exception/diagnostic_information.hpp>
27#include "ores.logging/boost_severity.hpp"
28
29namespace ores::qt {
30
36class exception_helper final {
37public:
52 template<typename Logger, typename EmitFunc>
54 const std::exception& e,
55 const QString& entity_name,
56 Logger& logger,
57 EmitFunc emit_error) {
58
59 const auto details = QString::fromStdString(
60 boost::diagnostic_information(e));
61
62 BOOST_LOG_SEV(logger, ores::logging::error)
63 << "Exception fetching " << entity_name.toStdString() << ": "
64 << details.toStdString();
65
66 const auto message = QString("Failed to fetch %1 from server.")
67 .arg(entity_name);
68
69 emit_error(message, details);
70 }
71
88 template<typename ResultType, typename FetchFunc>
89 static ResultType wrap_async_fetch(
90 FetchFunc&& fetch_func,
91 const QString& entity_name) {
92
93 try {
94 return fetch_func();
95 } catch (const std::exception& e) {
96 ResultType result{};
97 result.success = false;
98 result.error_message = QString("Failed to fetch %1 from server.")
99 .arg(entity_name);
100 result.error_details = QString::fromStdString(
101 boost::diagnostic_information(e));
102 return result;
103 }
104 }
105};
106
107}
108
109#endif
Qt-based graphical user interface for ORE Studio.
Definition AboutDialog.hpp:29
Helper class for handling exceptions and server errors in async operations.
Definition ExceptionHelper.hpp:36
static ResultType wrap_async_fetch(FetchFunc &&fetch_func, const QString &entity_name)
Wraps an async fetch operation to capture exceptions before Qt can wrap them.
Definition ExceptionHelper.hpp:89
static void handle_fetch_exception(const std::exception &e, const QString &entity_name, Logger &logger, EmitFunc emit_error)
Handles a fetch exception by logging and emitting an error signal.
Definition ExceptionHelper.hpp:53