SatNOGS-COMMS  4.1.0
A COMMS subsystem for CubeSats
Loading...
Searching...
No Matches
exception.hpp
Go to the documentation of this file.
1/*
2 * SatNOGS-COMMS control library
3 *
4 * Copyright (C) 2021-2024, Libre Space Foundation <http://libre.space>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * SPDX-License-Identifier: GNU General Public License v3.0 or later
20 */
21
22#pragma once
23
24#include <cstdint>
25#include <etl/error_handler.h>
26#include <etl/string_stream.h>
28
29namespace satnogs::comms::lib
30{
31
62class exception : public std::exception, public etl::exception
63{
64public:
70 enum class severity : uint8_t
71 {
74 MAJOR = 2,
75 MINOR = 3,
76 NONE = 4
77 };
78
83 {
84 public:
85 const char *verbose;
87 const char *terse;
89 int32_t error_num;
93 };
94
102 exception(severity sev, const char *file, int lineno,
103 const error_msg &err_msg)
104 : std::exception(),
105 etl::exception(ETL_ERROR_TEXT(err_msg.verbose, err_msg.terse), file,
106 lineno),
107 m_severity(sev),
108 m_errno(err_msg.error_num)
109 {
110 }
111
121 void
122 to_string(etl::string_stream &stream) const
123 {
124 stream << etl::exception::what() << " | File: " << file_name() << ":"
125 << line_number()
126 << " | Severity: " << static_cast<int32_t>(m_severity)
127 << " | Error: " << m_errno;
128 }
129
138 void
139 to_string(etl::istring &s) const
140 {
141 etl::string_stream stream(s);
142 to_string(stream);
143 }
144
145 const char *
146 what() const noexcept override
147 {
148 return etl::exception::what();
149 }
150
151 int32_t
152 get_errno() const
153 {
154 return m_errno;
155 }
156
166 {
167 return m_severity;
168 }
169
170private:
171 const severity m_severity;
172 const int32_t m_errno;
173};
174
180{
181public:
189 inval_arg_exception(severity sev, const char *file_name, int line)
190 : exception(sev, file_name, line,
191 error_msg{"Invalid argument", "invalarg", EINVAL})
192 {
193 }
194
202 inval_arg_exception(const char *file_name, int line)
203 : exception(severity::MINOR, file_name, line,
204 error_msg{"Invalid argument", "invalarg", EINVAL})
205 {
206 }
207};
208
216{
217public:
218 msg_too_long_exception(const char *file_name, int line)
219 : exception(exception::severity::MINOR, file_name, line,
220 error_msg{"Message too long", "msgbadsize", EMSGSIZE})
221 {
222 }
223};
224
232{
233public:
234 resource_unavailable_exception(const char *file_name, int line)
235 : exception(
236 exception::severity::MINOR, file_name, line,
237 error_msg{"The resource you are trying to access is currently "
238 "unavailable",
239 "eagain", EAGAIN})
240 {
241 }
242};
243
250{
251public:
252 timeout_exception(const char *file_name, int line)
253 : exception(exception::severity::MINOR, file_name, line,
254 error_msg{"Timeout occurred", "timeout", ETIMEDOUT})
255 {
256 }
257};
258
260
261} /* namespace satnogs::comms::lib */
A class representing error messages in the SatNOGS-COMMS system.
Definition exception.hpp:83
severity
Severity levels of exceptions.
Definition exception.hpp:71
@ MINOR
Failure having minimal impact.
Definition exception.hpp:75
@ MAJOR
Failure causing minor mission degradation.
Definition exception.hpp:74
@ CATASTROPHIC
Failure causing loss of mission.
Definition exception.hpp:72
@ CRITICAL
Failure causing mission degradation or significant damage.
Definition exception.hpp:73
exception(severity sev, const char *file, int lineno, const error_msg &err_msg)
Constructor for the exception class.
void to_string(etl::istring &s) const
Creates a string representation of the exception.
severity get_severity() const
Get error severity level as defined in FDIR.
const char * what() const noexcept override
void to_string(etl::string_stream &stream) const
Creates a string representation of the exception.
inval_arg_exception(severity sev, const char *file_name, int line)
Invalid argument exception with a specified severity level.
inval_arg_exception(const char *file_name, int line)
Invalid argument exception with a exception::severity::MINOR severity.
msg_too_long_exception(const char *file_name, int line)
resource_unavailable_exception(const char *file_name, int line)
timeout_exception(const char *file_name, int line)
Defines custom error codes for the SatNOGS-COMMS control library.