Add make_system_error

This commit is contained in:
blankie 2023-01-16 15:41:26 +07:00
parent d2006d5a4e
commit 7c5e9e5db5
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
4 changed files with 27 additions and 9 deletions

View File

@ -6,7 +6,6 @@
#include <sys/stat.h>
#include <string>
#include <memory>
#include <system_error>
#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) + ')');
}

View File

@ -2,7 +2,6 @@
#include <string>
#include <vector>
#include <exception>
#include <system_error>
#include "log.h"

View File

@ -1,5 +1,6 @@
#include <iomanip>
#include <sstream>
#include <system_error>
#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));
}

5
misc.h
View File

@ -1,5 +1,10 @@
#pragma once
#include <string>
#include <system_error>
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);