blob: 1bfd89068a773bf487e53c72587495f859cbb1ab [file] [log] [blame]
#include <sdbusplus/exception.hpp>
#include <cerrno>
#include <stdexcept>
#include <utility>
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wc99-extensions"
#endif
namespace sdbusplus
{
namespace exception
{
void exception::unused() const noexcept {}
int generated_exception::get_errno() const noexcept
{
return EIO;
}
SdBusError::SdBusError(int error_in, const char* prefix,
SdBusInterface* intf_in) :
SdBusError(error_in, std::string(prefix), intf_in)
{}
SdBusError::SdBusError(int error_in, std::string&& prefix,
SdBusInterface* intf_in) :
error(SD_BUS_ERROR_NULL),
intf(intf_in)
{
// We can't check the output of intf->sd_bus_error_set_errno() because
// it returns the input errorcode. We don't want to try and guess
// possible error statuses. Instead, check to see if the error was
// constructed to determine success.
intf->sd_bus_error_set_errno(&this->error, error_in);
if (!intf->sd_bus_error_is_set(&this->error))
{
throw std::runtime_error("Failed to create SdBusError");
}
populateMessage(std::move(prefix));
}
SdBusError::SdBusError(sd_bus_error* error_in, const char* prefix,
SdBusInterface* intf_in) :
error(*error_in),
intf(intf_in)
{
// We own the error so remove the caller's reference
*error_in = SD_BUS_ERROR_NULL;
populateMessage(std::string(prefix));
}
SdBusError::SdBusError(SdBusError&& other) : error(SD_BUS_ERROR_NULL)
{
move(std::move(other));
}
SdBusError& SdBusError::operator=(SdBusError&& other)
{
if (this != &other)
{
move(std::move(other));
}
return *this;
}
SdBusError::~SdBusError()
{
intf->sd_bus_error_free(&error);
}
const char* SdBusError::name() const noexcept
{
return error.name;
}
const char* SdBusError::description() const noexcept
{
return error.message;
}
const char* SdBusError::what() const noexcept
{
return full_message.c_str();
}
int SdBusError::get_errno() const noexcept
{
return intf->sd_bus_error_get_errno(&this->error);
}
const sd_bus_error* SdBusError::get_error() const noexcept
{
return &error;
}
void SdBusError::populateMessage(std::string&& prefix)
{
full_message = std::move(prefix);
if (error.name)
{
full_message += ": ";
full_message += error.name;
}
if (error.message)
{
full_message += ": ";
full_message += error.message;
}
}
void SdBusError::move(SdBusError&& other)
{
intf = std::move(other.intf);
intf->sd_bus_error_free(&error);
error = other.error;
other.error = SD_BUS_ERROR_NULL;
full_message = std::move(other.full_message);
}
const char* InvalidEnumString::name() const noexcept
{
return errName;
}
const char* InvalidEnumString::description() const noexcept
{
return errDesc;
}
const char* InvalidEnumString::what() const noexcept
{
return errWhat;
}
int InvalidEnumString::get_errno() const noexcept
{
return EINVAL;
}
static std::string unpackErrorReasonToString(const UnpackErrorReason reason)
{
switch (reason)
{
case UnpackErrorReason::missingProperty:
return "Missing property";
case UnpackErrorReason::wrongType:
return "Type not matched";
}
return "Unknown";
}
UnpackPropertyError::UnpackPropertyError(std::string_view propertyNameIn,
const UnpackErrorReason reasonIn) :
propertyName(propertyNameIn),
reason(reasonIn),
errWhatDetailed(std::string(errWhat) + " PropertyName: '" + propertyName +
"', Reason: '" + unpackErrorReasonToString(reason) + "'.")
{}
const char* UnpackPropertyError::name() const noexcept
{
return errName;
}
const char* UnpackPropertyError::description() const noexcept
{
return errDesc;
}
const char* UnpackPropertyError::what() const noexcept
{
return errWhatDetailed.c_str();
}
int UnpackPropertyError::get_errno() const noexcept
{
return EINVAL;
}
const char* UnhandledStop::name() const noexcept
{
return errName;
}
const char* UnhandledStop::description() const noexcept
{
return errDesc;
}
const char* UnhandledStop::what() const noexcept
{
return errWhat;
}
int UnhandledStop::get_errno() const noexcept
{
return ECANCELED;
}
} // namespace exception
} // namespace sdbusplus
#ifdef __clang__
#pragma clang diagnostic pop
#endif