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.comms/messaging/frame.hpp"
29#include "ores.comms/messaging/error_protocol.hpp"
30
31namespace ores::qt {
32
37 QString message;
38 QString details;
39};
40
48class exception_helper final {
49public:
60 static std::optional<server_error_info>
62 using comms::messaging::message_type;
64
65 if (response.header().type != message_type::error_response) {
66 return std::nullopt;
67 }
68
69 auto payload_result = response.decompressed_payload();
70 if (!payload_result) {
71 return server_error_info{
72 .message = "Server returned an error (failed to decompress)",
73 .details = {}
74 };
75 }
76
77 auto err_resp = error_response::deserialize(*payload_result);
78 if (!err_resp) {
79 return server_error_info{
80 .message = "Server returned an error (failed to parse)",
81 .details = {}
82 };
83 }
84
85 return server_error_info{
86 .message = QString("Server error: %1")
87 .arg(QString::fromStdString(err_resp->message)),
88 .details = QString("Error code: %1 (%2)")
89 .arg(QString::fromStdString(
90 ores::utility::serialization::to_string(err_resp->code)))
91 .arg(static_cast<int>(err_resp->code))
92 };
93 }
94
109 template<typename Logger, typename EmitFunc>
111 const std::exception& e,
112 const QString& entity_name,
113 Logger& logger,
114 EmitFunc emit_error) {
115
116 const auto details = QString::fromStdString(
117 boost::diagnostic_information(e));
118
119 BOOST_LOG_SEV(logger, ores::logging::error)
120 << "Exception fetching " << entity_name.toStdString() << ": "
121 << details.toStdString();
122
123 const auto message = QString("Failed to fetch %1 from server.")
124 .arg(entity_name);
125
126 emit_error(message, details);
127 }
128
145 template<typename ResultType, typename FetchFunc>
146 static ResultType wrap_async_fetch(
147 FetchFunc&& fetch_func,
148 const QString& entity_name) {
149
150 try {
151 return fetch_func();
152 } catch (const std::exception& e) {
153 ResultType result{};
154 result.success = false;
155 result.error_message = QString("Failed to fetch %1 from server.")
156 .arg(entity_name);
157 result.error_details = QString::fromStdString(
158 boost::diagnostic_information(e));
159 return result;
160 }
161 }
162};
163
164}
165
166#endif
Qt-based graphical user interface for ORE Studio.
Definition AboutDialog.hpp:35
Error response message sent when request processing fails.
Definition error_protocol.hpp:34
Complete frame with header and payload.
Definition frame.hpp:77
const frame_header & header() const
Get the frame header.
Definition frame.hpp:103
std::expected< std::vector< std::byte >, ores::utility::serialization::error_code > decompressed_payload() const
Decompress and return the payload.
Definition frame.cpp:361
Result of checking a response frame for server errors.
Definition ExceptionHelper.hpp:36
Helper class for handling exceptions and server errors in async operations.
Definition ExceptionHelper.hpp:48
static std::optional< server_error_info > check_error_response(const comms::messaging::frame &response)
Check if a response frame is an error_response and extract the message.
Definition ExceptionHelper.hpp:61
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:146
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:110