Fixing data members of exceptions and exception handling

This commit is contained in:
László Károlyi 2022-01-18 18:57:43 +01:00
parent affd7c4c23
commit 9654200ac4
Signed by: karolyi
GPG Key ID: 2DCAF25E55735BFE
3 changed files with 72 additions and 26 deletions

View File

@ -86,7 +86,10 @@ int main(int argc, char *argv[]) {
try {
startup_opts = IceGStreamer::parse_options(argc, argv);
IceGStreamer::check_config_structure(startup_opts->config);
} catch (IceGStreamer::Exceptions::PlayerBaseException &e) {
// } catch (IceGStreamer::Exceptions::ConfigOptionMissingException &e) {
// std::cerr << "Exception: " << e.what() << std::endl;
// return 1;
} catch (IceGStreamer::Exceptions::PlayerExceptionBase &e) {
std::cerr << "Exception: " << e.what() << std::endl;
return 1;
} catch (YAML::Exception &e) {

View File

@ -1,35 +1,69 @@
#include "exceptions.h"
#include <cstring>
#include <iostream>
namespace IceGStreamer {
namespace Exceptions {
const char *PlayerBaseException::what() { return "Base exception"; };
const char *PlayerExceptionBase::what() { return "Base exception"; };
const char *ConfigMissingException::what() {
return "Configuration file not passed";
}
ConfigOptionMissingException::ConfigOptionMissingException(){};
// ===== ConfigOptionMissingException START =====
ConfigOptionMissingException::ConfigOptionMissingException()
: d_ptr_{std::make_unique<Data>()} {};
ConfigOptionMissingException::ConfigOptionMissingException(
const char *missing_item)
: missing_item(missing_item){};
ConfigOptionMissingException::~ConfigOptionMissingException() {
if (allocated_message)
delete allocated_message;
std::cout << "cleanup" << std::endl;
struct ConfigOptionMissingException::Data {
std::string error_str{"Configuration file item missing:"};
std::string missing_item{"<unknown>"};
std::string allocated_message{};
};
// Copy constructor
ConfigOptionMissingException::ConfigOptionMissingException(
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() {
const std::string my_error = static_cast<std::string>(error_str) +
static_cast<std::string>(missing_item);
allocated_message = new char[my_error.length() + 1];
strcpy(const_cast<char *>(allocated_message), my_error.c_str());
return allocated_message;
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());
std::cout << "XX: " << static_cast<const char*>(buf.get()) << std::endl;
d_ptr_->allocated_message = std::string(buf.get());
// return static_cast<const char*>(buf.get());
return d_ptr_->allocated_message.c_str();
}
// ===== ConfigOptionMissingException END =====
} // namespace Exceptions
} // namespace IceGStreamer

View File

@ -1,31 +1,40 @@
#pragma once
#include <iostream>
#include <memory>
namespace IceGStreamer {
namespace Exceptions {
class PlayerBaseException : std::exception {
class PlayerExceptionBase : public std::exception {
public:
virtual const char *what();
};
class ConfigMissingException : PlayerBaseException {
class ConfigMissingException : public PlayerExceptionBase {
public:
virtual const char *what();
};
class ConfigOptionMissingException : PlayerBaseException {
class ConfigOptionMissingException : public PlayerExceptionBase {
private:
const char *error_str{"Configuration file item missing: "};
const char *missing_item{"<unknown>"};
const char *allocated_message{nullptr};
public:
ConfigOptionMissingException(const char *missing_item);
struct Data;
std::unique_ptr<Data> d_ptr_;
public:
ConfigOptionMissingException();
ConfigOptionMissingException(const std::string &missing_item);
// Copy constructor
ConfigOptionMissingException(ConfigOptionMissingException &other);
// Move constructor
ConfigOptionMissingException(ConfigOptionMissingException &&other);
// Copy operator
ConfigOptionMissingException &
operator=(const ConfigOptionMissingException &other);
// Move operator
ConfigOptionMissingException &operator=(ConfigOptionMissingException &&other);
virtual const char *what();
~ConfigOptionMissingException();
};