Add make_system_error
This commit is contained in:
parent
d2006d5a4e
commit
7c5e9e5db5
13
config.cpp
13
config.cpp
|
@ -6,7 +6,6 @@
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <system_error>
|
|
||||||
|
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "misc.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)) {
|
if (file || (ignore_enoent && errno == ENOENT)) {
|
||||||
return file;
|
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) {
|
static void fclose_and_log(FILE* file) {
|
||||||
if (!fclose(file)) {
|
if (!fclose(file)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
std::system_error e = std::system_error(errno, std::generic_category(), "fclose()");
|
log(std::string("Failed to close a file: ") + make_system_error("fclose()").what());
|
||||||
log(std::string("Failed to close a file: ") + e.what());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool write(const std::string& ptr, FILE* file) {
|
static bool write(const std::string& ptr, FILE* file) {
|
||||||
|
@ -109,7 +107,7 @@ void create_config_folders_if_necessary() {
|
||||||
if (errno == EEXIST) {
|
if (errno == EEXIST) {
|
||||||
continue;
|
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) {
|
if (errsv == ENOMEM) {
|
||||||
throw std::bad_alloc();
|
throw std::bad_alloc();
|
||||||
} else if (errsv != 0) {
|
} else if (errsv != 0) {
|
||||||
throw std::system_error(errsv, std::generic_category(), "getline()");
|
throw make_system_error(errsv, "getline()");
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -223,6 +221,5 @@ void write_config(const Config& config) {
|
||||||
if (!rename(tmp_config_file_path.c_str(), config_file_path.c_str())) {
|
if (!rename(tmp_config_file_path.c_str(), config_file_path.c_str())) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
throw std::system_error(errno, std::generic_category(),
|
throw make_system_error(std::string("rename(") + quote(tmp_config_file_path) + ", " + quote(config_file_path) + ')');
|
||||||
std::string("rename(") + quote(tmp_config_file_path) + ", " + quote(config_file_path) + ')');
|
|
||||||
}
|
}
|
||||||
|
|
1
log.cpp
1
log.cpp
|
@ -2,7 +2,6 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <exception>
|
#include <exception>
|
||||||
#include <system_error>
|
|
||||||
|
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
|
|
||||||
|
|
17
misc.cpp
17
misc.cpp
|
@ -1,5 +1,6 @@
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <system_error>
|
||||||
|
|
||||||
#include "misc.h"
|
#include "misc.h"
|
||||||
|
|
||||||
|
@ -8,3 +9,19 @@ std::string quote(const std::string& str) {
|
||||||
ss << std::quoted(str);
|
ss << std::quoted(str);
|
||||||
return ss.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
5
misc.h
|
@ -1,5 +1,10 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <system_error>
|
||||||
|
|
||||||
std::string quote(const std::string& str);
|
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);
|
||||||
|
|
Loading…
Reference in New Issue