Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/common/file_settings_persistence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <algorithm>
#include <fstream>
#include <iterator>
#include <stdexcept>

// local includes
#include "display_device/logging.h"
Expand All @@ -17,7 +18,7 @@ namespace display_device {
FileSettingsPersistence::FileSettingsPersistence(std::filesystem::path filepath):
m_filepath {std::move(filepath)} {
if (m_filepath.empty()) {
throw std::runtime_error {"Empty filename provided for FileSettingsPersistence!"};
throw std::invalid_argument {"Empty filename provided for FileSettingsPersistence!"};
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#ifdef DD_JSON_DETAIL
// system includes
#include <nlohmann/json.hpp>
#include <stdexcept>

// Special versions of the NLOHMANN definitions to remove the "m_" prefix in string form ('cause I like it that way ;P)
#define DD_JSON_TO(v1) nlohmann_json_j[#v1] = nlohmann_json_t.m_##v1;
Expand Down Expand Up @@ -85,7 +86,7 @@ namespace display_device {
const auto &map {getEnumMap(T {})};
auto it {std::find_if(std::begin(map), std::end(map), predicate)};
if (it == std::end(map)) { // GCOVR_EXCL_BR_LINE for fallthrough branch
throw std::runtime_error(error_msg); // GCOVR_EXCL_BR_LINE for fallthrough branch
throw std::out_of_range(error_msg); // GCOVR_EXCL_BR_LINE for fallthrough branch
}
return it;
}
Expand Down Expand Up @@ -131,7 +132,7 @@ namespace nlohmann {
const bool found {(display_device::detail::variantFromJson<Ts>(nlohmann_json_j, nlohmann_json_t) || ...)};
if (!found) {
const std::string error {"Could not parse variant from type " + nlohmann_json_j.at("type").get<std::string>() + "!"};
throw std::runtime_error(error);
throw std::invalid_argument(error);
}
}
};
Expand Down
11 changes: 6 additions & 5 deletions src/common/include/display_device/retry_scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <condition_variable>
#include <functional>
#include <memory>
#include <stdexcept>
#include <thread>

// local includes
Expand Down Expand Up @@ -147,7 +148,7 @@ namespace display_device {
* @param iface Interface to be passed around to the executor functions.
*/
explicit RetryScheduler(std::unique_ptr<T> iface):
m_iface {iface ? std::move(iface) : throw std::logic_error {"Nullptr interface provided in RetryScheduler!"}},
m_iface {iface ? std::move(iface) : throw std::invalid_argument {"Nullptr interface provided in RetryScheduler!"}},
m_thread {[this]() {
std::unique_lock lock {m_mutex};
while (m_keep_alive) {
Expand Down Expand Up @@ -218,17 +219,17 @@ namespace display_device {
*/
void schedule(std::function<void(T &, SchedulerStopToken &stop_token)> exec_fn, const SchedulerOptions &options) {
if (!exec_fn) {
throw std::logic_error {"Empty callback function provided in RetryScheduler::schedule!"};
throw std::invalid_argument {"Empty callback function provided in RetryScheduler::schedule!"};
}

if (options.m_sleep_durations.empty()) {
throw std::logic_error {"At least 1 sleep duration must be specified in RetryScheduler::schedule!"};
throw std::invalid_argument {"At least 1 sleep duration must be specified in RetryScheduler::schedule!"};
}

if (std::ranges::any_of(options.m_sleep_durations, [&](const auto &duration) {
return duration == std::chrono::milliseconds::zero();
})) {
throw std::logic_error {"All of the durations specified in RetryScheduler::schedule must be larger than a 0!"};
throw std::invalid_argument {"All of the durations specified in RetryScheduler::schedule must be larger than a 0!"};
}

std::lock_guard lock {m_mutex};
Expand Down Expand Up @@ -348,7 +349,7 @@ namespace display_device {

if constexpr (detail::OptionalFunction<FunctionT>) {
if (!exec_fn) {
throw std::logic_error {"Empty callback function provided in RetryScheduler::execute!"};
throw std::invalid_argument {"Empty callback function provided in RetryScheduler::execute!"};
}
}

Expand Down
12 changes: 11 additions & 1 deletion src/windows/persistent_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,22 @@
// class header include
#include "display_device/windows/persistent_state.h"

// system includes
#include <stdexcept>

// local includes
#include "display_device/logging.h"
#include "display_device/noop_settings_persistence.h"
#include "display_device/windows/json.h"

namespace display_device {
namespace {
class PersistentStateLoadException final: public std::runtime_error {
public:
using std::runtime_error::runtime_error;
};
} // namespace

PersistentState::PersistentState(std::shared_ptr<SettingsPersistenceInterface> settings_persistence_api, const bool throw_on_load_error):
m_settings_persistence_api {std::move(settings_persistence_api)} {
if (!m_settings_persistence_api) {
Expand All @@ -31,7 +41,7 @@ namespace display_device {

if (!error_message.empty()) {
if (throw_on_load_error) {
throw std::runtime_error {error_message};
throw PersistentStateLoadException {error_message};
}

DD_LOG(error) << error_message;
Expand Down
7 changes: 5 additions & 2 deletions src/windows/settings_manager_general.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
// class header include
#include "display_device/windows/settings_manager.h"

// system includes
#include <stdexcept>

// local includes
#include "display_device/logging.h"
#include "display_device/noop_audio_context.h"
Expand All @@ -22,15 +25,15 @@ namespace display_device {
m_persistence_state {std::move(persistent_state)},
m_workarounds {std::move(workarounds)} {
if (!m_dd_api) {
throw std::logic_error {"Nullptr provided for WinDisplayDeviceInterface in SettingsManager!"};
throw std::invalid_argument {"Nullptr provided for WinDisplayDeviceInterface in SettingsManager!"};
}

if (!m_audio_context_api) {
m_audio_context_api = std::make_shared<NoopAudioContext>();
}

if (!m_persistence_state) {
throw std::logic_error {"Nullptr provided for PersistentState in SettingsManager!"};
throw std::invalid_argument {"Nullptr provided for PersistentState in SettingsManager!"};
}

DD_LOG(info) << "Provided workaround settings for SettingsManager:\n"
Expand Down
5 changes: 4 additions & 1 deletion src/windows/win_display_device_general.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
// class header include
#include "display_device/windows/win_display_device.h"

// system includes
#include <stdexcept>

// local includes
#include "display_device/logging.h"
#include "display_device/windows/win_api_utils.h"
Expand All @@ -13,7 +16,7 @@ namespace display_device {
WinDisplayDevice::WinDisplayDevice(std::shared_ptr<WinApiLayerInterface> w_api):
m_w_api {std::move(w_api)} {
if (!m_w_api) {
throw std::logic_error {"Nullptr provided for WinApiLayerInterface in WinDisplayDevice!"};
throw std::invalid_argument {"Nullptr provided for WinApiLayerInterface in WinDisplayDevice!"};
}
}

Expand Down
3 changes: 2 additions & 1 deletion tests/unit/general/test_file_settings_persistence.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// system includes
#include <fstream>
#include <gmock/gmock.h>
#include <stdexcept>

// local includes
#include "display_device/file_settings_persistence.h"
Expand Down Expand Up @@ -39,7 +40,7 @@ TEST_F_S(EmptyFilenameProvided) {
EXPECT_THAT([]() {
const display_device::FileSettingsPersistence persistence {{}};
},
ThrowsMessage<std::runtime_error>(HasSubstr("Empty filename provided for FileSettingsPersistence!")));
ThrowsMessage<std::invalid_argument>(HasSubstr("Empty filename provided for FileSettingsPersistence!")));
}

TEST_F_S(Store, NewFileCreated) {
Expand Down
16 changes: 8 additions & 8 deletions tests/unit/general/test_retry_scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ TEST_F_S(NullptrInterfaceProvided) {
EXPECT_THAT([]() {
const display_device::RetryScheduler<TestIface> scheduler(nullptr);
},
ThrowsMessage<std::logic_error>(HasSubstr("Nullptr interface provided in RetryScheduler!")));
ThrowsMessage<std::invalid_argument>(HasSubstr("Nullptr interface provided in RetryScheduler!")));
}

TEST_F_S(Schedule, NullptrCallbackProvided) {
EXPECT_THAT([&]() {
m_impl.schedule(nullptr, {.m_sleep_durations = {0ms}});
},
ThrowsMessage<std::logic_error>(HasSubstr("Empty callback function provided in RetryScheduler::schedule!")));
ThrowsMessage<std::invalid_argument>(HasSubstr("Empty callback function provided in RetryScheduler::schedule!")));
}

TEST_F_S(Schedule, NoDurations) {
Expand All @@ -67,7 +67,7 @@ TEST_F_S(Schedule, NoDurations) {
},
{.m_sleep_durations = {}});
},
ThrowsMessage<std::logic_error>(HasSubstr("At least 1 sleep duration must be specified in RetryScheduler::schedule!")));
ThrowsMessage<std::invalid_argument>(HasSubstr("At least 1 sleep duration must be specified in RetryScheduler::schedule!")));
}

TEST_F_S(Schedule, ZeroDuration) {
Expand All @@ -76,7 +76,7 @@ TEST_F_S(Schedule, ZeroDuration) {
},
{.m_sleep_durations = {0ms}});
},
ThrowsMessage<std::logic_error>(HasSubstr("All of the durations specified in RetryScheduler::schedule must be larger than a 0!")));
ThrowsMessage<std::invalid_argument>(HasSubstr("All of the durations specified in RetryScheduler::schedule must be larger than a 0!")));
}

TEST_F_S(Schedule, SchedulingDurations) {
Expand Down Expand Up @@ -305,7 +305,7 @@ TEST_F_S(Schedule, ExceptionThrown, DuringImmediateCall) {

EXPECT_TRUE(m_impl.isScheduled());
m_impl.schedule([](auto, auto &) {
throw std::runtime_error("Get rekt!");
throw SchedulerStopTokenTestException {};
},
{.m_sleep_durations = {1ms}});
EXPECT_FALSE(m_impl.isScheduled());
Expand Down Expand Up @@ -337,7 +337,7 @@ TEST_F_S(Schedule, ExceptionThrown, DuringScheduledCall) {
EXPECT_EQ(output, "");
m_impl.schedule([&first_call](auto, auto &) {
if (!first_call) {
throw std::runtime_error("Get rekt!");
throw SchedulerStopTokenTestException {};
}
first_call = false;
},
Expand Down Expand Up @@ -367,7 +367,7 @@ TEST_F_S(Execute, NonConst, NullptrCallbackProvided) {
auto &non_const_impl {m_impl};
non_const_impl.execute(std::function<void(TestIface &)> {});
},
ThrowsMessage<std::logic_error>(HasSubstr("Empty callback function provided in RetryScheduler::execute!")));
ThrowsMessage<std::invalid_argument>(HasSubstr("Empty callback function provided in RetryScheduler::execute!")));
}

TEST_F_S(Execute, Const, NullptrCallbackProvided) {
Expand All @@ -376,7 +376,7 @@ TEST_F_S(Execute, Const, NullptrCallbackProvided) {
auto &const_impl {m_impl};
const_impl.execute(std::function<void(TestIface &)> {});
},
ThrowsMessage<std::logic_error>(HasSubstr("Empty callback function provided in RetryScheduler::execute!")));
ThrowsMessage<std::invalid_argument>(HasSubstr("Empty callback function provided in RetryScheduler::execute!")));
}

TEST_F_S(Execute, SchedulerNotStopped) {
Expand Down
7 changes: 5 additions & 2 deletions tests/unit/windows/test_settings_manager_general.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// system includes
#include <stdexcept>

// local includes
#include "display_device/noop_audio_context.h"
#include "display_device/noop_settings_persistence.h"
Expand Down Expand Up @@ -44,7 +47,7 @@ TEST_F_S_MOCKED(NullptrDisplayDeviceApiProvided) {
EXPECT_THAT([]() {
const display_device::SettingsManager settings_manager(nullptr, nullptr, nullptr, {});
},
ThrowsMessage<std::logic_error>(HasSubstr("Nullptr provided for WinDisplayDeviceInterface in SettingsManager!")));
ThrowsMessage<std::invalid_argument>(HasSubstr("Nullptr provided for WinDisplayDeviceInterface in SettingsManager!")));
}

TEST_F_S_MOCKED(NoopAudioContext) {
Expand All @@ -62,7 +65,7 @@ TEST_F_S_MOCKED(NullptrPersistentStateProvided) {
EXPECT_THAT([this]() {
const display_device::SettingsManager settings_manager(m_dd_api, nullptr, nullptr, {});
},
ThrowsMessage<std::logic_error>(HasSubstr("Nullptr provided for PersistentState in SettingsManager!")));
ThrowsMessage<std::invalid_argument>(HasSubstr("Nullptr provided for PersistentState in SettingsManager!")));
}

TEST_F_S_MOCKED(EnumAvailableDevices) {
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/windows/test_win_display_device_general.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// system includes
#include <format>
#include <stdexcept>

// local includes
#include "display_device/windows/settings_utils.h"
Expand Down Expand Up @@ -44,7 +45,7 @@ TEST_F_S(NullptrLayerProvided) {
EXPECT_THAT([]() {
const auto win_dd {display_device::WinDisplayDevice {nullptr}};
},
ThrowsMessage<std::logic_error>(HasSubstr("Nullptr provided for WinApiLayerInterface in WinDisplayDevice!")));
ThrowsMessage<std::invalid_argument>(HasSubstr("Nullptr provided for WinApiLayerInterface in WinDisplayDevice!")));
}

TEST_F_S(IsApiAccessAvailable) {
Expand Down