IceGStreamer/src/modules/exceptions.cpp

125 lines
4.4 KiB
C++

#include "exceptions.h"
#include <iostream>
namespace IceGStreamer {
namespace Exceptions {
// ===== PlayerExceptionBase START =====
const char *PlayerExceptionBase::what() { return "Base exception"; };
// ===== PlayerExceptionBase END =====
// ===== ConfigMissingException START =====
const char *ConfigMissingException::what() {
return "Configuration file not passed";
}
// ===== ConfigMissingException END =====
// ===== PlaylistUnreadableException START =====
PlaylistUnreadableException::PlaylistUnreadableException(
const std::string &path)
: error_msg("Playlist unreadable: " + path) {}
const char *PlaylistUnreadableException::what() { return error_msg.c_str(); }
// ===== PlaylistUnreadableException END =====
// ===== PlaylistUnreadableException START =====
PlaylistEmptyException::PlaylistEmptyException(const std::string &path)
: error_msg("Playlist empty: " + path) {}
const char *PlaylistEmptyException::what() { return error_msg.c_str(); }
// ===== PlaylistEmptyException END =====
// ===== PlaylistPathNotFoundException START =====
PlaylistPathNotFoundException::PlaylistPathNotFoundException(
const std::string &path)
: error_msg("Playlist path not found/not readable: " + path) {}
const char *PlaylistPathNotFoundException::what() { return error_msg.c_str(); }
// ===== PlaylistPathNotFoundException END =====
// ===== ConnectionErrorException START =====
ConnectionErrorException::ConnectionErrorException(
const std::string &error_detail)
: error_msg("Icecast server connection error: " + error_detail) {}
const char *ConnectionErrorException::what() { return error_msg.c_str(); }
// ===== PlaylistPathNotFoundException END =====
// ===== ConfigOptionMissingException START =====
ConfigOptionMissingException::ConfigOptionMissingException()
: d_ptr_{std::make_unique<Data>()} {};
struct ConfigOptionMissingException::Data {
std::string error_str{
"Configuration file item missing in YAML file or is of bad type:"};
std::string missing_item{"<unknown>"};
std::string allocated_message{};
};
// Copy constructor
// ConfigOptionMissingException::ConfigOptionMissingException(
// const ConfigOptionMissingException &other)
// : d_ptr_(std::make_unique<Data>(*other.d_ptr_)){};
// Move constructor
ConfigOptionMissingException::ConfigOptionMissingException(
ConfigOptionMissingException &&other) = default;
// Copy operator
// ConfigOptionMissingException &ConfigOptionMissingException::operator=(
// const ConfigOptionMissingException &other) {
// *d_ptr_ = *other.d_ptr_;
// return *this;
// };
// Move operator
ConfigOptionMissingException &ConfigOptionMissingException::operator=(
ConfigOptionMissingException &&) = default;
ConfigOptionMissingException::ConfigOptionMissingException(
const std::string &missing_item)
: d_ptr_{std::make_unique<Data>()} {
d_ptr_->missing_item = missing_item;
};
ConfigOptionMissingException::~ConfigOptionMissingException() = default;
const char *ConfigOptionMissingException::what() {
if (!d_ptr_->allocated_message.empty())
return d_ptr_->allocated_message.c_str();
int size = std::snprintf(
nullptr, 0, "%s %s", d_ptr_->error_str.c_str(),
d_ptr_->missing_item.c_str());
std::unique_ptr<char[]> buf = std::make_unique<char[]>(size + 1);
std::snprintf(
buf.get(), size + 1, "%s %s", d_ptr_->error_str.c_str(),
d_ptr_->missing_item.c_str());
d_ptr_->allocated_message = std::string(buf.get());
return d_ptr_->allocated_message.c_str();
}
// ===== ConfigOptionMissingException END =====
// ===== PlayerSetupException START =====
PlayerSetupException::PlayerSetupException(const std::string &error)
: error_msg("Player setup exception: " + error) {}
const char *PlayerSetupException::what() { return error_msg.c_str(); }
// ===== PlayerSetupException END =====
// ===== PlayerBusException START =====
PlayerBusException::PlayerBusException(const std::string &error)
: error_msg("Player bus exception: " + error) {}
const char *PlayerBusException::what() { return error_msg.c_str(); }
// ===== PlayerBusException END =====
// ===== NoReadablePlaylistItemException START =====
NoReadablePlaylistItemException::NoReadablePlaylistItemException(const std::string &path)
: error_msg("No readable path found in playlist file: " + path) {}
const char *NoReadablePlaylistItemException::what() { return error_msg.c_str(); }
// ===== NoReadablePlaylistItemException END =====
} // namespace Exceptions
} // namespace IceGStreamer