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#include "ores.qt/export.hpp"
29
30namespace ores::qt {
31
37class exception_helper final {
38public:
53 template<typename Logger, typename EmitFunc>
55 const std::exception& e,
56 const QString& entity_name,
57 Logger& logger,
58 EmitFunc emit_error) {
59
60 const auto details = QString::fromStdString(
61 boost::diagnostic_information(e));
62
63 BOOST_LOG_SEV(logger, ores::logging::error)
64 << "Exception fetching " << entity_name.toStdString() << ": "
65 << details.toStdString();
66
67 const auto message = QString("Failed to fetch %1 from server.")
68 .arg(entity_name);
69
70 emit_error(message, details);
71 }
72
89 template<typename ResultType, typename FetchFunc>
90 static ResultType wrap_async_fetch(
91 FetchFunc&& fetch_func,
92 const QString& entity_name) {
93
94 try {
95 return fetch_func();
96 } catch (const std::exception& e) {
97 ResultType result{};
98 result.success = false;
99 result.error_message = QString("Failed to fetch %1 from server.")
100 .arg(entity_name);
101 result.error_details = QString::fromStdString(
102 boost::diagnostic_information(e));
103 return result;
104 }
105 }
106};
107
108}
109
110#endif
Qt-based graphical user interface for ORE Studio.
Definition AccountController.hpp:32
Helper class for handling exceptions and server errors in async operations.
Definition ExceptionHelper.hpp:37
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:90
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:54