Astarte device API for C++ 0.8.1
Astarte device SDK for C++
Loading...
Searching...
No Matches
errors.hpp
1// (C) Copyright 2025, SECO Mind Srl
2//
3// SPDX-License-Identifier: Apache-2.0
4
5#ifndef ASTARTE_DEVICE_SDK_ERRORS_H
6#define ASTARTE_DEVICE_SDK_ERRORS_H
7
8#include <memory>
9#include <optional>
10#include <string>
11#include <variant>
12
13#if defined(ASTARTE_USE_TL_EXPECTED)
14#include "tl/expected.hpp"
15#else
16#include <expected>
17#endif
18
19#include "astarte_device_sdk/formatter.hpp"
20
21namespace AstarteDeviceSdk {
22
23#if defined(ASTARTE_USE_TL_EXPECTED)
24namespace astarte_tl = ::tl;
25#else
26namespace astarte_tl = ::std;
27#endif
28
35
44
51 public:
58 auto message() const -> const std::string&;
63 auto type() const -> const std::string&;
68 auto nested_error() const -> const std::shared_ptr<AstarteErrorBase>&;
69
70 protected:
76 explicit AstarteErrorBase(std::string_view type, std::string_view message);
83 explicit AstarteErrorBase(std::string_view type, std::string_view message,
84 const AstarteErrorBase& other);
85
86 private:
87 std::string type_;
88 std::string message_;
89 std::shared_ptr<AstarteErrorBase> other_;
90};
91
96 public:
101 explicit AstarteInternalError(std::string_view message);
107 explicit AstarteInternalError(std::string_view message, const AstarteError& other);
108
109 private:
110 static constexpr std::string_view k_type_ = "AstarteInternalError";
111};
112
117 public:
122 explicit AstarteFileOpenError(std::string_view filename);
128 explicit AstarteFileOpenError(std::string_view filename, const AstarteError& other);
129
130 private:
131 static constexpr std::string_view k_type_ = "AstarteFileOpenError";
132};
133
138 public:
143 explicit AstarteInvalidInputError(std::string_view message);
149 explicit AstarteInvalidInputError(std::string_view message, const AstarteError& other);
150
151 private:
152 static constexpr std::string_view k_type_ = "AstarteInvalidInputError";
153};
154
159 public:
164 explicit AstarteOperationRefusedError(std::string_view message);
170 explicit AstarteOperationRefusedError(std::string_view message, const AstarteError& other);
171
172 private:
173 static constexpr std::string_view k_type_ = "AstarteOperationRefusedError";
174};
175
180 public:
185 explicit AstarteGrpcLibError(std::string_view message);
191 explicit AstarteGrpcLibError(std::string_view message, const AstarteError& other);
197 explicit AstarteGrpcLibError(std::uint64_t code, std::string_view message);
204 explicit AstarteGrpcLibError(std::uint64_t code, std::string_view message,
205 const AstarteError& other);
206
207 private:
208 static constexpr std::string_view k_type_ = "AstarteGrpcLibError";
209};
210
215 public:
220 explicit AstarteMsgHubError(std::string_view message);
226 explicit AstarteMsgHubError(std::string_view message, const AstarteError& other);
227
228 private:
229 static constexpr std::string_view k_type_ = "AstarteMsgHubError";
230};
231
232} // namespace AstarteDeviceSdk
233
235
239template <>
240struct astarte_fmt::formatter<AstarteDeviceSdk::AstarteErrorBase> {
246 template <typename ParseContext>
247 constexpr auto parse(ParseContext& ctx) const {
248 return ctx.begin();
249 }
250
257 template <typename FormatContext>
258 auto format(const AstarteDeviceSdk::AstarteErrorBase& err, FormatContext& ctx) const {
259 auto out = astarte_fmt::format_to(ctx.out(), "{}: {}", err.type(), err.message());
260
261 std::string indent = "";
262 const AstarteDeviceSdk::AstarteErrorBase* current = &err;
263 while (const auto& nested = current->nested_error()) {
264 indent += " ";
265 out = astarte_fmt::format_to(out, "\n{}-> {}: {}", indent, nested->type(), nested->message());
266 current = nested.get();
267 }
268
269 return out;
270 }
271};
272
276template <>
277struct astarte_fmt::formatter<AstarteDeviceSdk::AstarteError> {
283 template <typename ParseContext>
284 constexpr auto parse(ParseContext& ctx) const {
285 return ctx.begin();
286 }
287
294 template <typename FormatContext>
295 auto format(const AstarteDeviceSdk::AstarteError& err_variant, FormatContext& ctx) const {
296 return std::visit(
297 [&ctx](const auto& err) {
298 const auto& base_err = static_cast<const AstarteDeviceSdk::AstarteErrorBase&>(err);
299 return astarte_fmt::format_to(ctx.out(), "{}", base_err);
300 },
301 err_variant);
302 }
303};
304
306
307#endif // ASTARTE_DEVICE_SDK_ERRORS_H
auto message() const -> const std::string &
Return the message encapsulated in the error.
virtual ~AstarteErrorBase()
Destructor for the Astarte error.
auto nested_error() const -> const std::shared_ptr< AstarteErrorBase > &
Return the nested error.
auto type() const -> const std::string &
Return the type encapsulated in the error.
AstarteErrorBase(std::string_view type, std::string_view message)
Constructor for the Astarte error.
Specific error for when a file cannot be opened.
Definition errors.hpp:116
AstarteFileOpenError(std::string_view filename, const AstarteError &other)
Nested error constructor.
AstarteFileOpenError(std::string_view filename)
Standard error constructor.
Error reported by the gRPC transport library.
Definition errors.hpp:179
AstarteGrpcLibError(std::string_view message, const AstarteError &other)
Nested error constructor.
AstarteGrpcLibError(std::uint64_t code, std::string_view message)
Error constructor including gRPC error codes.
AstarteGrpcLibError(std::uint64_t code, std::string_view message, const AstarteError &other)
Nested error constructor including gRPC error codes.
AstarteGrpcLibError(std::string_view message)
Standard error constructor.
Specific error for when an operation failed due to an internal error.
Definition errors.hpp:95
AstarteInternalError(std::string_view message, const AstarteError &other)
Nested error constructor.
AstarteInternalError(std::string_view message)
Standard error constructor.
Specific error for when an operation failed due to incompatible user input.
Definition errors.hpp:137
AstarteInvalidInputError(std::string_view message)
Standard error constructor.
AstarteInvalidInputError(std::string_view message, const AstarteError &other)
Nested error constructor.
Error reported by the Astarte message hub library.
Definition errors.hpp:214
AstarteMsgHubError(std::string_view message)
Standard error constructor.
AstarteMsgHubError(std::string_view message, const AstarteError &other)
Nested error constructor.
Attempted an operation which is not permitted according to the current device status.
Definition errors.hpp:158
AstarteOperationRefusedError(std::string_view message, const AstarteError &other)
Nested error constructor.
AstarteOperationRefusedError(std::string_view message)
Standard error constructor.
Umbrella namespace for the Astarte device SDK.
Definition data.hpp:23
std::variant< AstarteInternalError, AstarteFileOpenError, AstarteInvalidInputError, AstarteOperationRefusedError, AstarteGrpcLibError, AstarteMsgHubError > AstarteError
A variant type representing any possible error from the Astarte device library.
Definition errors.hpp:41