blob: 1a83a09f0b5fdc476d95687d8b92df25a5910278 [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
13namespace exception
14{
15
Patrick Williams1a283062016-11-13 19:05:10 -060016/** Base exception class for all sdbusplus exceptions, including those created
17 * by the bindings. */
Patrick Williamsf4a6f412016-11-11 13:47:29 -060018struct exception : public std::exception
19{
Patrick Williamsea241442016-11-15 14:41:13 -060020 virtual const char* name() const noexcept = 0;
21 virtual const char* description() const noexcept = 0;
Patrick Williamsf4a6f412016-11-11 13:47:29 -060022};
23
Patrick Williams1a283062016-11-13 19:05:10 -060024/** base exception class for all errors generated by sdbusplus itself. */
25struct internal_exception : public exception
Patrick Williams127b8ab2020-05-21 15:24:19 -050026{};
Patrick Williams1a283062016-11-13 19:05:10 -060027
William A. Kennington III20db3bf2018-05-14 16:18:37 -070028/** Exception for when an underlying sd_bus method call fails. */
Vernon Maueryfac43a62018-08-01 12:29:23 -070029class SdBusError final : public internal_exception
William A. Kennington III20db3bf2018-05-14 16:18:37 -070030{
31 public:
32 /** Errno must be positive */
William A. Kennington III37657502018-06-22 19:00:05 -070033 SdBusError(int error, const char* prefix,
34 SdBusInterface* intf = &sdbus_impl);
William A. Kennington III20db3bf2018-05-14 16:18:37 -070035 /** Becomes the owner of the error */
William A. Kennington III68cb1702018-06-22 19:35:48 -070036 SdBusError(sd_bus_error* error, const char* prefix,
William A. Kennington III37657502018-06-22 19:00:05 -070037 SdBusInterface* intf = &sdbus_impl);
William A. Kennington III20db3bf2018-05-14 16:18:37 -070038
39 SdBusError(const SdBusError&) = delete;
40 SdBusError& operator=(const SdBusError&) = delete;
41 SdBusError(SdBusError&& other);
42 SdBusError& operator=(SdBusError&& other);
43 virtual ~SdBusError();
44
45 const char* name() const noexcept override;
46 const char* description() const noexcept override;
47 const char* what() const noexcept override;
Adriana Kobylak33335b32018-09-21 11:50:42 -050048 int get_errno() const noexcept;
Adrian Ambrożewicza66f6b42020-01-09 17:21:58 +010049 const sd_bus_error* get_error() const noexcept;
William A. Kennington III20db3bf2018-05-14 16:18:37 -070050
51 private:
52 sd_bus_error error;
53 std::string full_message;
William A. Kennington III37657502018-06-22 19:00:05 -070054 SdBusInterface* intf;
William A. Kennington III20db3bf2018-05-14 16:18:37 -070055
56 /** Populates the full_message from the stored
57 * error and the passed in prefix. */
58 void populateMessage(const char* prefix);
59
60 /** Helper to reduce duplicate move logic */
61 void move(SdBusError&& other);
62};
63
Patrick Williams59b70d12016-11-16 16:11:25 -060064/** Exception for when an invalid conversion from string to enum is
65 * attempted. */
66struct InvalidEnumString final : public internal_exception
67{
68 static constexpr auto errName =
Patrick Williams02d96752017-04-19 07:52:29 -050069 "xyz.openbmc_project.sdbusplus.Error.InvalidEnumString";
Patrick Williams59b70d12016-11-16 16:11:25 -060070 static constexpr auto errDesc =
71 "An enumeration mapping was attempted for which no valid enumeration "
72 "value exists.";
73 static constexpr auto errWhat =
Patrick Williams02d96752017-04-19 07:52:29 -050074 "xyz.openbmc_project.sdbusplus.Error.InvalidEnumString: "
Patrick Williams59b70d12016-11-16 16:11:25 -060075 "An enumeration mapping was attempted for which no valid enumeration "
76 "value exists.";
77
Patrick Williams93b0e702017-04-18 11:19:45 -050078 const char* name() const noexcept override;
79 const char* description() const noexcept override;
80 const char* what() const noexcept override;
Patrick Williams59b70d12016-11-16 16:11:25 -060081};
82
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +020083/** Exception for when unpackProperties cannot find given property in provided
84 * container */
85class UnpackPropertyError final : public internal_exception
86{
87 public:
88 UnpackPropertyError(std::string_view propertyName, std::string_view reason);
89
90 static constexpr std::string_view reasonMissingProperty =
91 "Missing property";
92 static constexpr std::string_view reasonTypeNotMatched = "Type not matched";
93
94 static constexpr auto errName =
95 "xyz.openbmc_project.sdbusplus.Error.UnpackPropertyError";
96 static constexpr auto errDesc =
97 "unpackProperties failed to unpack one of requested properties.";
98 static constexpr auto errWhat =
99 "xyz.openbmc_project.sdbusplus.Error.UnpackPropertyError: "
100 "unpackProperties failed to unpack one of requested properties.";
101
102 const char* name() const noexcept override;
103 const char* description() const noexcept override;
104 const char* what() const noexcept override;
105
106 const std::string propertyName;
107 const std::string reason;
108};
109
Patrick Williamsf4a6f412016-11-11 13:47:29 -0600110} // namespace exception
111
112using exception_t = exception::exception;
Patrick Williams1a283062016-11-13 19:05:10 -0600113using internal_exception_t = exception::internal_exception;
Patrick Williamsf4a6f412016-11-11 13:47:29 -0600114
115} // namespace sdbusplus