blob: 06f488eed93d70e95ffdd23653d9974c95fb77bd [file] [log] [blame]
Patrick Williamsf4a6f412016-11-11 13:47:29 -06001#pragma once
2
3#include <exception>
William A. Kennington III20db3bf2018-05-14 16:18:37 -07004#include <string>
5#include <system_error>
6#include <systemd/sd-bus.h>
Patrick Williamsf4a6f412016-11-11 13:47:29 -06007
8namespace sdbusplus
9{
10
11namespace exception
12{
13
Patrick Williams1a283062016-11-13 19:05:10 -060014/** Base exception class for all sdbusplus exceptions, including those created
15 * by the bindings. */
Patrick Williamsf4a6f412016-11-11 13:47:29 -060016struct exception : public std::exception
17{
Patrick Williamsea241442016-11-15 14:41:13 -060018 virtual const char* name() const noexcept = 0;
19 virtual const char* description() const noexcept = 0;
Patrick Williamsf4a6f412016-11-11 13:47:29 -060020};
21
Patrick Williams1a283062016-11-13 19:05:10 -060022/** base exception class for all errors generated by sdbusplus itself. */
23struct internal_exception : public exception
24{
25};
26
William A. Kennington III20db3bf2018-05-14 16:18:37 -070027/** Exception for when an underlying sd_bus method call fails. */
28class SdBusError final : public internal_exception, public std::system_error
29{
30 public:
31 /** Errno must be positive */
32 SdBusError(int error, const char* prefix);
33 /** Becomes the owner of the error */
34 SdBusError(sd_bus_error error, const char* prefix);
35
36 SdBusError(const SdBusError&) = delete;
37 SdBusError& operator=(const SdBusError&) = delete;
38 SdBusError(SdBusError&& other);
39 SdBusError& operator=(SdBusError&& other);
40 virtual ~SdBusError();
41
42 const char* name() const noexcept override;
43 const char* description() const noexcept override;
44 const char* what() const noexcept override;
45
46 private:
47 sd_bus_error error;
48 std::string full_message;
49
50 /** Populates the full_message from the stored
51 * error and the passed in prefix. */
52 void populateMessage(const char* prefix);
53
54 /** Helper to reduce duplicate move logic */
55 void move(SdBusError&& other);
56};
57
Patrick Williams59b70d12016-11-16 16:11:25 -060058/** Exception for when an invalid conversion from string to enum is
59 * attempted. */
60struct InvalidEnumString final : public internal_exception
61{
62 static constexpr auto errName =
Patrick Williams02d96752017-04-19 07:52:29 -050063 "xyz.openbmc_project.sdbusplus.Error.InvalidEnumString";
Patrick Williams59b70d12016-11-16 16:11:25 -060064 static constexpr auto errDesc =
65 "An enumeration mapping was attempted for which no valid enumeration "
66 "value exists.";
67 static constexpr auto errWhat =
Patrick Williams02d96752017-04-19 07:52:29 -050068 "xyz.openbmc_project.sdbusplus.Error.InvalidEnumString: "
Patrick Williams59b70d12016-11-16 16:11:25 -060069 "An enumeration mapping was attempted for which no valid enumeration "
70 "value exists.";
71
Patrick Williams93b0e702017-04-18 11:19:45 -050072 const char* name() const noexcept override;
73 const char* description() const noexcept override;
74 const char* what() const noexcept override;
Patrick Williams59b70d12016-11-16 16:11:25 -060075};
76
Patrick Williamsf4a6f412016-11-11 13:47:29 -060077} // namespace exception
78
79using exception_t = exception::exception;
Patrick Williams1a283062016-11-13 19:05:10 -060080using internal_exception_t = exception::internal_exception;
Patrick Williamsf4a6f412016-11-11 13:47:29 -060081
82} // namespace sdbusplus