From 7c5e9e5db528f0a0eb83475419afad7e3099ac50 Mon Sep 17 00:00:00 2001 From: blankie Date: Mon, 16 Jan 2023 15:41:26 +0700 Subject: [PATCH] Add make_system_error --- config.cpp | 13 +++++-------- log.cpp | 1 - misc.cpp | 17 +++++++++++++++++ misc.h | 5 +++++ 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/config.cpp b/config.cpp index e5ef3a4..5c124c1 100644 --- a/config.cpp +++ b/config.cpp @@ -6,7 +6,6 @@ #include #include #include -#include #include "log.h" #include "misc.h" @@ -17,15 +16,14 @@ static FILE* fopen_or_raise(const char* path, const char* mode, bool ignore_enoe if (file || (ignore_enoent && errno == ENOENT)) { return file; } - throw std::system_error(errno, std::generic_category(), std::string("fopen(") + quote(path) + ')'); + throw make_system_error(std::string("fopen(") + quote(path) + ')'); } static void fclose_and_log(FILE* file) { if (!fclose(file)) { return; } - std::system_error e = std::system_error(errno, std::generic_category(), "fclose()"); - log(std::string("Failed to close a file: ") + e.what()); + log(std::string("Failed to close a file: ") + make_system_error("fclose()").what()); } static bool write(const std::string& ptr, FILE* file) { @@ -109,7 +107,7 @@ void create_config_folders_if_necessary() { if (errno == EEXIST) { continue; } - throw std::system_error(errno, std::generic_category(), std::string("mkdir(") + quote(path) + ')'); + throw make_system_error(std::string("mkdir(") + quote(path) + ')'); } } @@ -148,7 +146,7 @@ static inline Config load_config(FILE* file) { if (errsv == ENOMEM) { throw std::bad_alloc(); } else if (errsv != 0) { - throw std::system_error(errsv, std::generic_category(), "getline()"); + throw make_system_error(errsv, "getline()"); } else { break; } @@ -223,6 +221,5 @@ void write_config(const Config& config) { if (!rename(tmp_config_file_path.c_str(), config_file_path.c_str())) { return; } - throw std::system_error(errno, std::generic_category(), - std::string("rename(") + quote(tmp_config_file_path) + ", " + quote(config_file_path) + ')'); + throw make_system_error(std::string("rename(") + quote(tmp_config_file_path) + ", " + quote(config_file_path) + ')'); } diff --git a/log.cpp b/log.cpp index f3b872c..f6421d4 100644 --- a/log.cpp +++ b/log.cpp @@ -2,7 +2,6 @@ #include #include #include -#include #include "log.h" diff --git a/misc.cpp b/misc.cpp index d23cb3b..ed4a702 100644 --- a/misc.cpp +++ b/misc.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "misc.h" @@ -8,3 +9,19 @@ std::string quote(const std::string& str) { ss << std::quoted(str); return ss.str(); } + +std::system_error make_system_error(int err, const char* what) { + return std::system_error(err, std::generic_category(), what); +} + +std::system_error make_system_error(int err, std::string what) { + return std::system_error(err, std::generic_category(), std::move(what)); +} + +std::system_error make_system_error(const char* what) { + return make_system_error(errno, what); +} + +std::system_error make_system_error(std::string what) { + return make_system_error(errno, std::move(what)); +} diff --git a/misc.h b/misc.h index 1e75a5a..b94f0e3 100644 --- a/misc.h +++ b/misc.h @@ -1,5 +1,10 @@ #pragma once #include +#include std::string quote(const std::string& str); +std::system_error make_system_error(int err, const char* what); +std::system_error make_system_error(int err, std::string what); +std::system_error make_system_error(const char* what); +std::system_error make_system_error(std::string what);