Astarte device API for C++ 0.8.1
Astarte device SDK for C++
Loading...
Searching...
No Matches
errors.hpp
Go to the documentation of this file.
1// (C) Copyright 2025 - 2026, 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
16
17#if defined(ASTARTE_USE_TL_EXPECTED)
18#include <tl/expected.hpp>
19#else
20#include <expected>
21#endif
22
23#include <cstdint>
24#include <memory>
25#include <optional>
26#include <string>
27#include <string_view>
28#include <variant>
29
31
32namespace astarte::device {
33
34#if defined(ASTARTE_USE_TL_EXPECTED)
35namespace astarte_tl = ::tl;
36#else
37namespace astarte_tl = ::std;
38#endif
39
41class FileOpenError;
43class InternalError;
45class GrpcLibError;
46class MsgHubError;
56#if !defined(ASTARTE_TRANSPORT_GRPC)
57namespace mqtt {
60class PairingApiError;
61class MqttError;
62class InvalidUrlError;
67class CryptoError;
68class UuidError;
69class HttpError;
71} // namespace mqtt
72#endif
73
80using Error =
86#if !defined(ASTARTE_TRANSPORT_GRPC)
92#else
94#endif
95
103 public:
105 virtual ~ErrorBase() = default;
106
108 ErrorBase(const ErrorBase&) = default;
109
114 auto operator=(const ErrorBase&) -> ErrorBase& = default;
115
117 ErrorBase(ErrorBase&&) = delete;
118
123 auto operator=(ErrorBase&&) -> ErrorBase& = default;
124
129 [[nodiscard]] auto message() const -> const std::string&;
130
135 [[nodiscard]] auto type() const -> const std::string&;
136
141 [[nodiscard]] auto nested_error() const -> const std::shared_ptr<ErrorBase>&;
142
143 protected:
149 explicit ErrorBase(std::string_view type, std::string_view message);
150
157 explicit ErrorBase(std::string_view type, std::string_view message, const ErrorBase& other);
158
159 private:
160 std::string type_;
161 std::string message_;
162 std::shared_ptr<ErrorBase> other_;
163};
164
167 public:
172 explicit DataSerializationError(std::string_view message);
173
179 explicit DataSerializationError(std::string_view message, const Error& other);
180
181 private:
182 static constexpr std::string_view k_type_ = "DataSerializationError";
183};
184
186class InternalError : public ErrorBase {
187 public:
192 explicit InternalError(std::string_view message);
193
199 explicit InternalError(std::string_view message, const Error& other);
200
201 private:
202 static constexpr std::string_view k_type_ = "InternalError";
203};
204
206class FileOpenError : public ErrorBase {
207 public:
212 explicit FileOpenError(std::string_view filename);
213
219 explicit FileOpenError(std::string_view filename, const Error& other);
220
221 private:
222 static constexpr std::string_view k_type_ = "FileOpenError";
223};
224
227 public:
232 explicit InvalidInputError(std::string_view message);
233
239 explicit InvalidInputError(std::string_view message, const Error& other);
240
241 private:
242 static constexpr std::string_view k_type_ = "InvalidInputError";
243};
244
247 public:
252 explicit OperationRefusedError(std::string_view message);
253
259 explicit OperationRefusedError(std::string_view message, const Error& other);
260
261 private:
262 static constexpr std::string_view k_type_ = "OperationRefusedError";
263};
264
266class GrpcLibError : public ErrorBase {
267 public:
272 explicit GrpcLibError(std::string_view message);
273
279 explicit GrpcLibError(std::string_view message, const Error& other);
280
286 explicit GrpcLibError(std::uint64_t code, std::string_view message);
287
294 explicit GrpcLibError(std::uint64_t code, std::string_view message, const Error& other);
295
296 private:
297 static constexpr std::string_view k_type_ = "GrpcLibError";
298};
299
301class MsgHubError : public ErrorBase {
302 public:
307 explicit MsgHubError(std::string_view message);
308
314 explicit MsgHubError(std::string_view message, const Error& other);
315
316 private:
317 static constexpr std::string_view k_type_ = "MsgHubError";
318};
319
320// -----------------------------------------------------------------------------
321// Interface validation errors
322// -----------------------------------------------------------------------------
323
326 public:
331 explicit InterfaceValidationError(std::string_view message);
332
338 explicit InterfaceValidationError(std::string_view message, const Error& other);
339
340 private:
341 static constexpr std::string_view k_type_ = "InterfaceValidationError";
342};
343
346 public:
351 explicit InvalidInterfaceVersionError(std::string_view message);
352
358 explicit InvalidInterfaceVersionError(std::string_view message, const Error& other);
359
360 private:
361 static constexpr std::string_view k_type_ = "InvalidInterfaceVersionError";
362};
363
366 public:
371 explicit InvalidInterfaceTypeError(std::string_view message);
372
378 explicit InvalidInterfaceTypeError(std::string_view message, const Error& other);
379
380 private:
381 static constexpr std::string_view k_type_ = "InvalidInterfaceTypeError";
382};
383
386 public:
391 explicit InvalidInterfaceOwnershipeError(std::string_view message);
392
398 explicit InvalidInterfaceOwnershipeError(std::string_view message, const Error& other);
399
400 private:
401 static constexpr std::string_view k_type_ = "InvalidInterfaceOwnershipeError";
402};
403
406 public:
411 explicit InvalidInterfaceAggregationError(std::string_view message);
412
418 explicit InvalidInterfaceAggregationError(std::string_view message, const Error& other);
419
420 private:
421 static constexpr std::string_view k_type_ = "InvalidInterfaceAggregationError";
422};
423
426 public:
431 explicit InvalidAstarteTypeError(std::string_view message);
432
438 explicit InvalidAstarteTypeError(std::string_view message, const Error& other);
439
440 private:
441 static constexpr std::string_view k_type_ = "InvalidAstarteTypeError";
442};
443
446 public:
451 explicit InvalidReliabilityError(std::string_view message);
452
458 explicit InvalidReliabilityError(std::string_view message, const Error& other);
459
460 private:
461 static constexpr std::string_view k_type_ = "InvalidReliabilityError";
462};
463
466 public:
471 explicit InvalidRetentionError(std::string_view message);
472
478 explicit InvalidRetentionError(std::string_view message, const Error& other);
479
480 private:
481 static constexpr std::string_view k_type_ = "InvalidRetentionError";
482};
483
486 public:
492
498 explicit InvalidDatabaseRetentionPolicyError(std::string_view message, const Error& other);
499
500 private:
501 static constexpr std::string_view k_type_ = "InvalidDatabaseRetentionPolicyError";
502};
503
504} // namespace astarte::device
505
506#if !defined(ASTARTE_TRANSPORT_GRPC)
507// We accept this circular inclusion as it's required for the forward declarations above to work
508// even when an user includes only this header.
509// NOLINTNEXTLINE(misc-header-include-cycle)
511#endif
512
514template <>
515// NOLINTNEXTLINE(cert-dcl58-cpp)
522 template <typename ParseContext>
523 constexpr auto parse(ParseContext& ctx) const {
524 return ctx.begin();
525 }
526
533 template <typename FormatContext>
534 auto format(const astarte::device::Error& err_variant, FormatContext& ctx) const {
535 return std::visit(
536 [&ctx](const auto& err) {
537 const auto& base_err = static_cast<const astarte::device::ErrorBase&>(err);
538 return astarte_fmt::format_to(ctx.out(), "{}", base_err);
539 },
540 err_variant);
541 }
542};
543
545template <>
546struct astarte_fmt::formatter<astarte::device::ErrorBase> {
552 template <typename ParseContext>
553 constexpr auto parse(ParseContext& ctx) const {
554 return ctx.begin();
555 }
556
563 template <typename FormatContext>
564 auto format(const astarte::device::ErrorBase& err, FormatContext& ctx) const {
565 auto out = astarte_fmt::format_to(ctx.out(), "{}: {}", err.type(), err.message());
566
567 std::string indent;
568 const astarte::device::ErrorBase* current = &err;
569 while (const auto& nested = current->nested_error()) {
570 indent += " ";
571 out = astarte_fmt::format_to(out, "\n{}-> {}: {}", indent, nested->type(), nested->message());
572 current = nested.get();
573 }
574
575 return out;
576 }
577};
578
579#endif // ASTARTE_DEVICE_SDK_ERRORS_H
Specific error for when a serialization operation failed.
Definition errors.hpp:166
DataSerializationError(std::string_view message)
Standard error constructor.
DataSerializationError(std::string_view message, const Error &other)
Nested error constructor.
Base error class representing any possible error from the Astarte device library.
Definition errors.hpp:102
auto message() const -> const std::string &
Retrieves the error message.
virtual ~ErrorBase()=default
Destructor for the Astarte error.
auto operator=(ErrorBase &&) -> ErrorBase &=default
Default move assignment operator.
ErrorBase(const ErrorBase &)=default
Default copy constructor.
auto nested_error() const -> const std::shared_ptr< ErrorBase > &
Retrieves the nested error, if any.
auto type() const -> const std::string &
Retrieves the error type string.
auto operator=(const ErrorBase &) -> ErrorBase &=default
Default copy assignment operator.
ErrorBase(ErrorBase &&)=delete
Delete move constructor.
Specific error for when a file cannot be opened.
Definition errors.hpp:206
FileOpenError(std::string_view filename, const Error &other)
Nested error constructor.
FileOpenError(std::string_view filename)
Standard error constructor.
Error reported by the gRPC transport library.
Definition errors.hpp:266
GrpcLibError(std::string_view message, const Error &other)
Nested error constructor.
GrpcLibError(std::uint64_t code, std::string_view message)
Error constructor including gRPC error codes.
GrpcLibError(std::string_view message)
Standard error constructor.
GrpcLibError(std::uint64_t code, std::string_view message, const Error &other)
Nested error constructor including gRPC error codes.
Error during the Interface validation process.
Definition errors.hpp:325
InterfaceValidationError(std::string_view message, const Error &other)
Nested error constructor.
InterfaceValidationError(std::string_view message)
Standard error constructor.
Specific error for when an operation failed due to an internal error.
Definition errors.hpp:186
InternalError(std::string_view message, const Error &other)
Nested error constructor.
InternalError(std::string_view message)
Standard error constructor.
Error indicating that the provided Astarte type is incorrect.
Definition errors.hpp:425
InvalidAstarteTypeError(std::string_view message)
Standard error constructor.
InvalidAstarteTypeError(std::string_view message, const Error &other)
Nested error constructor.
Error indicating that the provided Astarte database retention policy is incorrect.
Definition errors.hpp:485
InvalidDatabaseRetentionPolicyError(std::string_view message)
Standard error constructor.
InvalidDatabaseRetentionPolicyError(std::string_view message, const Error &other)
Nested error constructor.
Specific error for when an operation failed due to incompatible user input.
Definition errors.hpp:226
InvalidInputError(std::string_view message)
Standard error constructor.
InvalidInputError(std::string_view message, const Error &other)
Nested error constructor.
Error indicating that the provided interface aggregation is incorrect.
Definition errors.hpp:405
InvalidInterfaceAggregationError(std::string_view message, const Error &other)
Nested error constructor.
InvalidInterfaceAggregationError(std::string_view message)
Standard error constructor.
Error indicating that the provided interface ownership is incorrect.
Definition errors.hpp:385
InvalidInterfaceOwnershipeError(std::string_view message)
Standard error constructor.
InvalidInterfaceOwnershipeError(std::string_view message, const Error &other)
Nested error constructor.
Error indicating that the provided interface type is incorrect.
Definition errors.hpp:365
InvalidInterfaceTypeError(std::string_view message, const Error &other)
Nested error constructor.
InvalidInterfaceTypeError(std::string_view message)
Standard error constructor.
Error indicating that the minor or major version of an interface is incorrect.
Definition errors.hpp:345
InvalidInterfaceVersionError(std::string_view message, const Error &other)
Nested error constructor.
InvalidInterfaceVersionError(std::string_view message)
Standard error constructor.
Error indicating that the provided Astarte reliability is incorrect.
Definition errors.hpp:445
InvalidReliabilityError(std::string_view message, const Error &other)
Nested error constructor.
InvalidReliabilityError(std::string_view message)
Standard error constructor.
Error indicating that the provided Astarte retention is incorrect.
Definition errors.hpp:465
InvalidRetentionError(std::string_view message)
Standard error constructor.
InvalidRetentionError(std::string_view message, const Error &other)
Nested error constructor.
Error reported by the Astarte message hub library.
Definition errors.hpp:301
MsgHubError(std::string_view message, const Error &other)
Nested error constructor.
MsgHubError(std::string_view message)
Standard error constructor.
An operation which is not permitted according to the current device status was attempted.
Definition errors.hpp:246
OperationRefusedError(std::string_view message)
Standard error constructor.
OperationRefusedError(std::string_view message, const Error &other)
Nested error constructor.
Specific error for when an operation failed due to an internal error.
Definition errors.hpp:219
Specific error for when an operation failed due to an internal error.
Definition errors.hpp:99
Specific error for when an operation failed due to an internal error.
Definition errors.hpp:259
Specific error for when an operation failed due to an internal error.
Definition errors.hpp:79
Specific error for when an operation failed due to an internal error.
Definition errors.hpp:119
Specific MQTT connection error.
Definition errors.hpp:279
Specific error for when an operation failed due to an internal error.
Definition errors.hpp:30
Specific error for when an operation failed due to an internal error.
Definition errors.hpp:59
Specific error for when the configuration to connect the device to Astarte is invalid.
Definition errors.hpp:199
Specific error for when reading from a file.
Definition errors.hpp:159
Specific error for when an operation failed due to an internal error.
Definition errors.hpp:139
Specific error for when a UUID operation failed.
Definition errors.hpp:239
Specific error for when writing to a file.
Definition errors.hpp:179
Data formatting utilities for Astarte types.
specific error definitions for the Astarte device SDK.
Namespace for Astarte device functionality using the MQTT transport protocol.
Definition errors.hpp:57
Umbrella namespace for the Astarte device library.
Definition data.hpp:29
std::variant< DataSerializationError, InternalError, FileOpenError, InvalidInputError, InterfaceValidationError, InvalidInterfaceVersionError, InvalidInterfaceTypeError, InvalidInterfaceOwnershipeError, InvalidInterfaceAggregationError, InvalidAstarteTypeError, InvalidReliabilityError, InvalidRetentionError, InvalidDatabaseRetentionPolicyError, OperationRefusedError, GrpcLibError, MsgHubError, mqtt::JsonParsingError, mqtt::DeviceRegistrationError, mqtt::PairingApiError, mqtt::MqttError, mqtt::InvalidUrlError, mqtt::RetrieveBrokerUrlError, mqtt::ReadCredentialError, mqtt::WriteCredentialError, mqtt::PairingConfigError, mqtt::CryptoError, mqtt::UuidError, mqtt::HttpError, mqtt::MqttConnectionError > Error
Variant type representing any error from the Astarte device library.
Definition errors.hpp:80
Global namespace for all Astarte related functionality.
Definition data.hpp:29
auto format(const astarte::device::ErrorBase &err, FormatContext &ctx) const
Formats the ErrorBase object including any nested errors.
Definition errors.hpp:564
constexpr auto parse(ParseContext &ctx) const
Parses the format string.
Definition errors.hpp:553
constexpr auto parse(ParseContext &ctx) const
Parses the format string.
Definition errors.hpp:523
auto format(const astarte::device::Error &err_variant, FormatContext &ctx) const
Formats the Error object by visiting the variant.
Definition errors.hpp:534
Base formatter struct declaration for Doxygen.
Definition formatter.hpp:48