blob: e7e31ea8d22da9a2738c8a080f21875429dcb47c [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);
Patrick Williams70bcf142023-07-26 16:19:48 -050055 SdBusError(int error, std::string&& prefix,
56 SdBusInterface* intf = &sdbus_impl);
William A. Kennington III20db3bf2018-05-14 16:18:37 -070057 /** Becomes the owner of the error */
William A. Kennington III68cb1702018-06-22 19:35:48 -070058 SdBusError(sd_bus_error* error, const char* prefix,
William A. Kennington III37657502018-06-22 19:00:05 -070059 SdBusInterface* intf = &sdbus_impl);
William A. Kennington III20db3bf2018-05-14 16:18:37 -070060
61 SdBusError(const SdBusError&) = delete;
62 SdBusError& operator=(const SdBusError&) = delete;
63 SdBusError(SdBusError&& other);
64 SdBusError& operator=(SdBusError&& other);
Brad Bishop0eda6eb2022-09-29 11:04:13 -040065 ~SdBusError() override;
William A. Kennington III20db3bf2018-05-14 16:18:37 -070066
67 const char* name() const noexcept override;
68 const char* description() const noexcept override;
69 const char* what() const noexcept override;
Patrick Williamsb6f729d2021-09-02 15:02:19 -050070 int get_errno() const noexcept override;
Adrian Ambrożewicza66f6b42020-01-09 17:21:58 +010071 const sd_bus_error* get_error() const noexcept;
William A. Kennington III20db3bf2018-05-14 16:18:37 -070072
73 private:
74 sd_bus_error error;
75 std::string full_message;
William A. Kennington III37657502018-06-22 19:00:05 -070076 SdBusInterface* intf;
William A. Kennington III20db3bf2018-05-14 16:18:37 -070077
78 /** Populates the full_message from the stored
79 * error and the passed in prefix. */
Patrick Williams70bcf142023-07-26 16:19:48 -050080 void populateMessage(std::string&& prefix);
William A. Kennington III20db3bf2018-05-14 16:18:37 -070081
82 /** Helper to reduce duplicate move logic */
83 void move(SdBusError&& other);
84};
85
Patrick Williams59b70d12016-11-16 16:11:25 -060086/** Exception for when an invalid conversion from string to enum is
87 * attempted. */
88struct InvalidEnumString final : public internal_exception
89{
90 static constexpr auto errName =
Patrick Williams02d96752017-04-19 07:52:29 -050091 "xyz.openbmc_project.sdbusplus.Error.InvalidEnumString";
Patrick Williams59b70d12016-11-16 16:11:25 -060092 static constexpr auto errDesc =
93 "An enumeration mapping was attempted for which no valid enumeration "
94 "value exists.";
95 static constexpr auto errWhat =
Patrick Williams02d96752017-04-19 07:52:29 -050096 "xyz.openbmc_project.sdbusplus.Error.InvalidEnumString: "
Patrick Williams59b70d12016-11-16 16:11:25 -060097 "An enumeration mapping was attempted for which no valid enumeration "
98 "value exists.";
99
Patrick Williams93b0e702017-04-18 11:19:45 -0500100 const char* name() const noexcept override;
101 const char* description() const noexcept override;
102 const char* what() const noexcept override;
Patrick Williamsbd372ec2021-09-02 15:15:24 -0500103 int get_errno() const noexcept override;
Patrick Williams59b70d12016-11-16 16:11:25 -0600104};
105
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200106/** Exception for when unpackProperties cannot find given property in provided
107 * container */
108class UnpackPropertyError final : public internal_exception
109{
110 public:
Krzysztof Grobelnyc8447d52022-01-05 13:21:37 +0100111 UnpackPropertyError(std::string_view propertyName,
112 const UnpackErrorReason reason);
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200113
114 static constexpr auto errName =
115 "xyz.openbmc_project.sdbusplus.Error.UnpackPropertyError";
116 static constexpr auto errDesc =
117 "unpackProperties failed to unpack one of requested properties.";
118 static constexpr auto errWhat =
119 "xyz.openbmc_project.sdbusplus.Error.UnpackPropertyError: "
120 "unpackProperties failed to unpack one of requested properties.";
121
122 const char* name() const noexcept override;
123 const char* description() const noexcept override;
124 const char* what() const noexcept override;
Patrick Williamsbd372ec2021-09-02 15:15:24 -0500125 int get_errno() const noexcept override;
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200126
127 const std::string propertyName;
Krzysztof Grobelnyc8447d52022-01-05 13:21:37 +0100128 const UnpackErrorReason reason;
Szymon Dompke6d83cf52021-10-19 16:31:29 +0200129
130 private:
131 const std::string errWhatDetailed;
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200132};
133
Patrick Williams4cfc2842022-09-22 09:53:33 -0500134class UnhandledStop final : public internal_exception
135{
136 public:
137 static constexpr auto errName =
138 "xyz.openbmc_project.sdbusplus.Error.UnhandledStop";
139 static constexpr auto errDesc =
140 "An async Sender failed to handle a stop condition.";
141 static constexpr auto errWhat =
142 "xyz.openbmc_project.sdbusplus.Error.UnhandledStop: "
143 "An async Sender failed to handle a stop condition.";
144
145 const char* name() const noexcept override;
146 const char* description() const noexcept override;
147 const char* what() const noexcept override;
148 int get_errno() const noexcept override;
149};
150
Patrick Williamsf4a6f412016-11-11 13:47:29 -0600151} // namespace exception
152
153using exception_t = exception::exception;
Patrick Williams1a283062016-11-13 19:05:10 -0600154using internal_exception_t = exception::internal_exception;
Patrick Williamsf4a6f412016-11-11 13:47:29 -0600155
156} // namespace sdbusplus