Loading...
Searching...
No Matches
Error.h
Go to the documentation of this file.
#ifndef SDBUS_CXX_ERROR_H_
#define SDBUS_CXX_ERROR_H_
#include <cerrno>
#include <stdexcept>
#include <string>
namespace sdbus {
/********************************************/
class Error
: public std::runtime_error
{
public:
// Strong type representing the D-Bus error name
class Name : public std::string
{
public:
Name() = default;
explicit Name(std::string value)
: std::string(std::move(value))
{}
explicit Name(const char* value)
: std::string(value)
{}
using std::string::operator=;
};
explicit Error(Name name, const char* message = nullptr)
: Error(std::move(name), std::string(message ? message : ""))
{
}
Error(Name name, std::string message)
: std::runtime_error(!message.empty() ? "[" + name + "] " + message : "[" + name + "]")
, name_(std::move(name))
, message_(std::move(message))
{
}
[[nodiscard]] const Name& getName() const
{
return name_;
}
[[nodiscard]] const std::string& getMessage() const
{
return message_;
}
[[nodiscard]] bool isValid() const
{
return !getName().empty();
}
private:
Name name_;
std::string message_;
};
Error createError(int errNo, std::string customMsg = {});
inline const Error::Name SDBUSCPP_ERROR_NAME{"org.sdbuscpp.Error"};
} // namespace sdbus
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
#define SDBUS_THROW_ERROR(_MSG, _ERRNO) \
throw sdbus::createError((_ERRNO), (_MSG)) \
/**/
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
#define SDBUS_THROW_ERROR_IF(_COND, _MSG, _ERRNO) \
if (!(_COND)) ; else SDBUS_THROW_ERROR((_MSG), (_ERRNO)) \
/**/
#endif /* SDBUS_CXX_ERROR_H_ */
sdbus::Error::Name
Definition Error.h:48
sdbus::Error
Definition Error.h:44