sdbus-c++  1.6.0
High-level C++ D-Bus library based on systemd D-Bus implementation
Error.h
Go to the documentation of this file.
1 
27 #ifndef SDBUS_CXX_ERROR_H_
28 #define SDBUS_CXX_ERROR_H_
29 
30 #include <errno.h>
31 #include <stdexcept>
32 #include <string>
33 
34 namespace sdbus {
35 
36  /********************************************/
42  class Error
43  : public std::runtime_error
44  {
45  public:
46  explicit Error(const std::string& name, const char* message = nullptr)
47  : Error(name, std::string(message ? message : ""))
48  {
49  }
50 
51  Error(const std::string& name, const std::string& message)
52  : std::runtime_error("[" + name + "] " + message)
53  , name_(name)
54  , message_(message)
55  {
56  }
57 
58  const std::string& getName() const
59  {
60  return name_;
61  }
62 
63  const std::string& getMessage() const
64  {
65  return message_;
66  }
67 
68  bool isValid() const
69  {
70  return !getName().empty();
71  }
72 
73  private:
74  std::string name_;
75  std::string message_;
76  };
77 
78  sdbus::Error createError(int errNo, const std::string& customMsg);
79 
80  inline const char* SDBUSCPP_ERROR_NAME = "org.sdbuscpp.Error";
81 }
82 
83 #define SDBUS_THROW_ERROR(_MSG, _ERRNO) \
84  throw sdbus::createError((_ERRNO), (_MSG)) \
85 
86 
87 #define SDBUS_THROW_ERROR_IF(_COND, _MSG, _ERRNO) \
88  if (!(_COND)) ; else SDBUS_THROW_ERROR((_MSG), (_ERRNO)) \
89 
90 
91 #endif /* SDBUS_CXX_ERROR_H_ */
Definition: Error.h:44