blob: d7eb03f3382da6f077e42f7dafebf222adf1df63 [file] [log] [blame]
Patrick Williamsf4a6f412016-11-11 13:47:29 -06001#pragma once
2
Patrick Venture95269db2018-08-31 09:19:17 -07003#include <systemd/sd-bus.h>
4
William A. Kennington III37657502018-06-22 19:00:05 -07005#include <sdbusplus/sdbus.hpp>
Patrick Williams127b8ab2020-05-21 15:24:19 -05006
7#include <exception>
William A. Kennington III20db3bf2018-05-14 16:18:37 -07008#include <string>
Patrick Williamsf4a6f412016-11-11 13:47:29 -06009
10namespace sdbusplus
11{
12
Krzysztof Grobelnyc8447d52022-01-05 13:21:37 +010013enum class UnpackErrorReason
14{
15 missingProperty,
16 wrongType
17};
18
Patrick Williamsf4a6f412016-11-11 13:47:29 -060019namespace exception
20{
21
Patrick Williams1a283062016-11-13 19:05:10 -060022/** Base exception class for all sdbusplus exceptions, including those created
23 * by the bindings. */
Patrick Williamsf4a6f412016-11-11 13:47:29 -060024struct exception : public std::exception
25{
Patrick Williamsea241442016-11-15 14:41:13 -060026 virtual const char* name() const noexcept = 0;
27 virtual const char* description() const noexcept = 0;
Patrick Williamsb68700e2021-09-09 06:05:15 -050028 virtual int get_errno() const noexcept = 0;
Patrick Williamsa8e35022021-09-28 06:16:52 -050029
30 private:
31 // This unused function is to ensure that the vtable for this class is
32 // properly emitted when `-flto=auto` is used, which is the default in
33 // Yocto builds. Without this, the vtable is a hidden symbol and no
34 // users can inherit from our exception type directly.
35 virtual void unused() const noexcept;
Patrick Williamsf4a6f412016-11-11 13:47:29 -060036};
37
Patrick Williams15228662021-09-03 06:04:25 -050038/** base exception class for all errors created by the sdbus++ generator */
39struct generated_exception : public exception
40{
41 int get_errno() const noexcept override;
42};
43
Patrick Williams1a283062016-11-13 19:05:10 -060044/** base exception class for all errors generated by sdbusplus itself. */
45struct internal_exception : public exception
Patrick Williams127b8ab2020-05-21 15:24:19 -050046{};
Patrick Williams1a283062016-11-13 19:05:10 -060047
William A. Kennington III20db3bf2018-05-14 16:18:37 -070048/** Exception for when an underlying sd_bus method call fails. */
Vernon Maueryfac43a62018-08-01 12:29:23 -070049class SdBusError final : public internal_exception
William A. Kennington III20db3bf2018-05-14 16:18:37 -070050{
51 public:
52 /** Errno must be positive */
William A. Kennington III37657502018-06-22 19:00:05 -070053 SdBusError(int error, const char* prefix,
54 SdBusInterface* intf = &sdbus_impl);
William A. Kennington III20db3bf2018-05-14 16:18:37 -070055 /** Becomes the owner of the error */
William A. Kennington III68cb1702018-06-22 19:35:48 -070056 SdBusError(sd_bus_error* error, const char* prefix,
William A. Kennington III37657502018-06-22 19:00:05 -070057 SdBusInterface* intf = &sdbus_impl);
William A. Kennington III20db3bf2018-05-14 16:18:37 -070058
59 SdBusError(const SdBusError&) = delete;
60 SdBusError& operator=(const SdBusError&) = delete;
61 SdBusError(SdBusError&& other);
62 SdBusError& operator=(SdBusError&& other);
Brad Bishop0eda6eb2022-09-29 11:04:13 -040063 ~SdBusError() override;
William A. Kennington III20db3bf2018-05-14 16:18:37 -070064
65 const char* name() const noexcept override;
66 const char* description() const noexcept override;
67 const char* what() const noexcept override;
Patrick Williamsb6f729d2021-09-02 15:02:19 -050068 int get_errno() const noexcept override;
Adrian Ambrożewicza66f6b42020-01-09 17:21:58 +010069 const sd_bus_error* get_error() const noexcept;
William A. Kennington III20db3bf2018-05-14 16:18:37 -070070
71 private:
72 sd_bus_error error;
73 std::string full_message;
William A. Kennington III37657502018-06-22 19:00:05 -070074 SdBusInterface* intf;
William A. Kennington III20db3bf2018-05-14 16:18:37 -070075
76 /** Populates the full_message from the stored
77 * error and the passed in prefix. */
78 void populateMessage(const char* prefix);
79
80 /** Helper to reduce duplicate move logic */
81 void move(SdBusError&& other);
82};
83
Patrick Williams59b70d12016-11-16 16:11:25 -060084/** Exception for when an invalid conversion from string to enum is
85 * attempted. */
86struct InvalidEnumString final : public internal_exception
87{
88 static constexpr auto errName =
Patrick Williams02d96752017-04-19 07:52:29 -050089 "xyz.openbmc_project.sdbusplus.Error.InvalidEnumString";
Patrick Williams59b70d12016-11-16 16:11:25 -060090 static constexpr auto errDesc =
91 "An enumeration mapping was attempted for which no valid enumeration "
92 "value exists.";
93 static constexpr auto errWhat =
Patrick Williams02d96752017-04-19 07:52:29 -050094 "xyz.openbmc_project.sdbusplus.Error.InvalidEnumString: "
Patrick Williams59b70d12016-11-16 16:11:25 -060095 "An enumeration mapping was attempted for which no valid enumeration "
96 "value exists.";
97
Patrick Williams93b0e702017-04-18 11:19:45 -050098 const char* name() const noexcept override;
99 const char* description() const noexcept override;
100 const char* what() const noexcept override;
Patrick Williamsbd372ec2021-09-02 15:15:24 -0500101 int get_errno() const noexcept override;
Patrick Williams59b70d12016-11-16 16:11:25 -0600102};
103
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200104/** Exception for when unpackProperties cannot find given property in provided
105 * container */
106class UnpackPropertyError final : public internal_exception
107{
108 public:
Krzysztof Grobelnyc8447d52022-01-05 13:21:37 +0100109 UnpackPropertyError(std::string_view propertyName,
110 const UnpackErrorReason reason);
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200111
112 static constexpr auto errName =
113 "xyz.openbmc_project.sdbusplus.Error.UnpackPropertyError";
114 static constexpr auto errDesc =
115 "unpackProperties failed to unpack one of requested properties.";
116 static constexpr auto errWhat =
117 "xyz.openbmc_project.sdbusplus.Error.UnpackPropertyError: "
118 "unpackProperties failed to unpack one of requested properties.";
119
120 const char* name() const noexcept override;
121 const char* description() const noexcept override;
122 const char* what() const noexcept override;
Patrick Williamsbd372ec2021-09-02 15:15:24 -0500123 int get_errno() const noexcept override;
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200124
125 const std::string propertyName;
Krzysztof Grobelnyc8447d52022-01-05 13:21:37 +0100126 const UnpackErrorReason reason;
Szymon Dompke6d83cf52021-10-19 16:31:29 +0200127
128 private:
129 const std::string errWhatDetailed;
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200130};
131
Patrick Williamsf4a6f412016-11-11 13:47:29 -0600132} // namespace exception
133
134using exception_t = exception::exception;
Patrick Williams1a283062016-11-13 19:05:10 -0600135using internal_exception_t = exception::internal_exception;
Patrick Williamsf4a6f412016-11-11 13:47:29 -0600136
137} // namespace sdbusplus