diff options
author | Vsevolod Stakhov <vsevolod@rspamd.com> | 2022-10-17 11:39:38 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@rspamd.com> | 2022-10-17 11:39:38 +0100 |
commit | ff8d45abef65ae9552a5c089317f96208474bbe6 (patch) | |
tree | d3b693cb228f1fb7b5ee3a935600f044cd2a2b92 /src/libutil | |
parent | 14b52f4498c10625f3c16101cd34dec25a59bac6 (diff) | |
download | rspamd-ff8d45abef65ae9552a5c089317f96208474bbe6.tar.gz rspamd-ff8d45abef65ae9552a5c089317f96208474bbe6.zip |
[Minor] Use a separate error class instead of std::string
Diffstat (limited to 'src/libutil')
-rw-r--r-- | src/libutil/cxx/error.hxx | 100 | ||||
-rw-r--r-- | src/libutil/cxx/file_util.cxx | 52 | ||||
-rw-r--r-- | src/libutil/cxx/file_util.hxx | 25 |
3 files changed, 140 insertions, 37 deletions
diff --git a/src/libutil/cxx/error.hxx b/src/libutil/cxx/error.hxx new file mode 100644 index 000000000..65fa76f3b --- /dev/null +++ b/src/libutil/cxx/error.hxx @@ -0,0 +1,100 @@ +/*- + * Copyright 2022 Vsevolod Stakhov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef RSPAMD_ERROR_HXX +#define RSPAMD_ERROR_HXX +#pragma once + +#include "config.h" +#include <string> +#include <string_view> +#include <cstdint> +#include <optional> + +/*** + * This unit is used to represent Rspamd C++ errors in a way to interoperate + * with C code if needed and avoid allocations for static strings + */ +namespace rspamd::util { + +enum class error_category : std::uint8_t { + INFORMAL, + IMPORTANT, + CRITICAL +}; + +struct error { +public: + /** + * Construct from a static string, this string must live long enough to outlive this object + * @param msg + * @param code + * @param category + */ + error(const char *msg, int code, error_category category = error_category::INFORMAL) : + error_message(msg), error_code(code), category(category) {} + /** + * Construct error from a temporary string taking membership + * @param msg + * @param code + * @param category + */ + error(std::string &&msg, int code, error_category category = error_category::INFORMAL) + : error_code(code), category(category) { + static_storage = std::move(msg); + error_message = static_storage.value(); + } + /** + * Construct error from another string copying it into own storage + * @param msg + * @param code + * @param category + */ + error(const std::string &msg, int code, error_category category = error_category::INFORMAL) + : error_code(code), category(category) { + static_storage = msg; + error_message = static_storage.value(); + } + + /** + * Convert into GError + * @return + */ + auto into_g_error() const -> GError * { + return g_error_new(g_quark_from_static_string("rspamd"), error_code, "%s", + error_message.data()); + } + + /** + * Convenience alias for the `into_g_error` + * @param err + */ + auto into_g_error_set(GError **err) const -> void { + if (err && *err == nullptr) { + *err = into_g_error(); + } + } +public: + std::string_view error_message; + int error_code; + error_category category; +private: + std::optional<std::string> static_storage; +}; + +} // namespace rspamd::util + +#endif //RSPAMD_ERROR_HXX diff --git a/src/libutil/cxx/file_util.cxx b/src/libutil/cxx/file_util.cxx index 10a91a251..d0860b77b 100644 --- a/src/libutil/cxx/file_util.cxx +++ b/src/libutil/cxx/file_util.cxx @@ -24,7 +24,7 @@ namespace rspamd::util { -auto raii_file::open(const char *fname, int flags) -> tl::expected<raii_file, std::string> +auto raii_file::open(const char *fname, int flags) -> tl::expected<raii_file, error> { int oflags = flags; #ifdef O_CLOEXEC @@ -32,25 +32,25 @@ auto raii_file::open(const char *fname, int flags) -> tl::expected<raii_file, st #endif if (fname == nullptr) { - return tl::make_unexpected("cannot open file; filename is nullptr"); + return tl::make_unexpected(error {"cannot open file; filename is nullptr", EINVAL, error_category::CRITICAL}); } auto fd = ::open(fname, oflags); if (fd == -1) { - return tl::make_unexpected(fmt::format("cannot open file {}: {}", fname, ::strerror(errno))); + return tl::make_unexpected(error{fmt::format("cannot open file {}: {}", fname, ::strerror(errno)), errno}); } auto ret = raii_file{fname, fd, false}; if (fstat(ret.fd, &ret.st) == -1) { - return tl::make_unexpected(fmt::format("cannot stat file {}: {}", fname, ::strerror(errno))); + return tl::make_unexpected(error {fmt::format("cannot stat file {}: {}", fname, ::strerror(errno)), errno}); } return ret; } -auto raii_file::create(const char *fname, int flags, int perms) -> tl::expected<raii_file, std::string> +auto raii_file::create(const char *fname, int flags, int perms) -> tl::expected<raii_file, error> { int oflags = flags; #ifdef O_CLOEXEC @@ -58,57 +58,58 @@ auto raii_file::create(const char *fname, int flags, int perms) -> tl::expected< #endif if (fname == nullptr) { - return tl::make_unexpected("cannot open file; filename is nullptr"); + return tl::make_unexpected(error {"cannot open file; filename is nullptr", EINVAL, error_category::CRITICAL}); } auto fd = ::open(fname, oflags, perms); if (fd == -1) { - return tl::make_unexpected(fmt::format("cannot create file {}: {}", fname, ::strerror(errno))); + return tl::make_unexpected(error{fmt::format("cannot create file {}: {}", fname, ::strerror(errno)), errno}); } auto ret = raii_file{fname, fd, false}; if (fstat(ret.fd, &ret.st) == -1) { - return tl::make_unexpected(fmt::format("cannot stat file {}: {}", fname, ::strerror(errno))); + return tl::make_unexpected(error{fmt::format("cannot stat file {}: {}", fname, ::strerror(errno)), errno}); } return ret; } -auto raii_file::create_temp(const char *fname, int flags, int perms) -> tl::expected<raii_file, std::string> +auto raii_file::create_temp(const char *fname, int flags, int perms) -> tl::expected<raii_file, error> { int oflags = flags; #ifdef O_CLOEXEC oflags |= O_CLOEXEC | O_CREAT | O_EXCL; #endif if (fname == nullptr) { - return tl::make_unexpected("cannot open file; filename is nullptr"); + return tl::make_unexpected(error {"cannot open file; filename is nullptr", EINVAL, error_category::CRITICAL}); } auto fd = ::open(fname, oflags, perms); if (fd == -1) { - return tl::make_unexpected(fmt::format("cannot create file {}: {}", fname, ::strerror(errno))); + return tl::make_unexpected(error {fmt::format("cannot create file {}: {}", fname, ::strerror(errno)), errno}); } auto ret = raii_file{fname, fd, true}; if (fstat(ret.fd, &ret.st) == -1) { - return tl::make_unexpected(fmt::format("cannot stat file {}: {}", fname, ::strerror(errno))); + return tl::make_unexpected(error {fmt::format("cannot stat file {}: {}", fname, ::strerror(errno)), errno}); } return ret; } -auto raii_file::mkstemp(const char *pattern, int flags, int perms) -> tl::expected<raii_file, std::string> +auto raii_file::mkstemp(const char *pattern, int flags, int perms) -> tl::expected<raii_file, error> { int oflags = flags; #ifdef O_CLOEXEC oflags |= O_CLOEXEC | O_CREAT | O_EXCL; #endif if (pattern == nullptr) { - return tl::make_unexpected("cannot open file; pattern is nullptr"); + return tl::make_unexpected(error {"cannot open file; pattern is nullptr", EINVAL, error_category::CRITICAL}); + } std::string mutable_pattern = pattern; @@ -116,14 +117,14 @@ auto raii_file::mkstemp(const char *pattern, int flags, int perms) -> tl::expect auto fd = g_mkstemp_full(mutable_pattern.data(), oflags, perms); if (fd == -1) { - return tl::make_unexpected(fmt::format("cannot create file {}: {}", pattern, ::strerror(errno))); + return tl::make_unexpected(error {fmt::format("cannot create file {}: {}", pattern, ::strerror(errno)), errno}); } auto ret = raii_file{mutable_pattern.c_str(), fd, true}; if (fstat(ret.fd, &ret.st) == -1) { - return tl::make_unexpected(fmt::format("cannot stat file {}: {}", - mutable_pattern.c_str(), ::strerror(errno))); + return tl::make_unexpected(error { fmt::format("cannot stat file {}: {}", + mutable_pattern, ::strerror(errno)), errno} ); } return ret; @@ -152,10 +153,11 @@ raii_locked_file::~raii_locked_file() noexcept } } -auto raii_locked_file::lock_raii_file(raii_file &&unlocked) -> tl::expected<raii_locked_file, std::string> +auto raii_locked_file::lock_raii_file(raii_file &&unlocked) -> tl::expected<raii_locked_file, error> { if (!rspamd_file_lock(unlocked.get_fd(), TRUE)) { - return tl::make_unexpected(fmt::format("cannot lock file {}: {}", unlocked.get_name(), ::strerror(errno))); + return tl::make_unexpected( + error { fmt::format("cannot lock file {}: {}", unlocked.get_name(), ::strerror(errno)), errno}); } return raii_locked_file{std::move(unlocked)}; @@ -175,7 +177,7 @@ raii_mmaped_file::raii_mmaped_file(raii_file &&_file, void *_map) } auto raii_mmaped_file::mmap_shared(raii_file &&file, - int flags) -> tl::expected<raii_mmaped_file, std::string> + int flags) -> tl::expected<raii_mmaped_file, error> { void *map; @@ -184,8 +186,8 @@ auto raii_mmaped_file::mmap_shared(raii_file &&file, map = mmap(NULL, file.get_stat().st_size, flags, MAP_SHARED, file.get_fd(), 0); if (map == MAP_FAILED) { - return tl::make_unexpected(fmt::format("cannot mmap file at fd: {}: {}", - file.get_fd(), ::strerror(errno))); + return tl::make_unexpected(error { fmt::format("cannot mmap file at fd: {}: {}", + file.get_fd(), ::strerror(errno)), errno }); } @@ -193,7 +195,7 @@ auto raii_mmaped_file::mmap_shared(raii_file &&file, } auto raii_mmaped_file::mmap_shared(const char *fname, int open_flags, - int mmap_flags) -> tl::expected<raii_mmaped_file, std::string> + int mmap_flags) -> tl::expected<raii_mmaped_file, error> { auto file = raii_file::open(fname, open_flags); @@ -218,10 +220,10 @@ raii_mmaped_file::raii_mmaped_file(raii_mmaped_file &&other) noexcept } auto raii_file_sink::create(const char *fname, int flags, int perms, - const char *suffix) -> tl::expected<raii_file_sink, std::string> + const char *suffix) -> tl::expected<raii_file_sink, error> { if (!fname || !suffix) { - return tl::make_unexpected("cannot create file sink: bad input arguments"); + return tl::make_unexpected(error {"cannot open file; filename is nullptr", EINVAL, error_category::CRITICAL}); } auto tmp_fname = fmt::format("{}.{}", fname, suffix); diff --git a/src/libutil/cxx/file_util.hxx b/src/libutil/cxx/file_util.hxx index c66fd17ef..097ce03a7 100644 --- a/src/libutil/cxx/file_util.hxx +++ b/src/libutil/cxx/file_util.hxx @@ -19,6 +19,7 @@ #include "config.h" #include "contrib/expected/expected.hpp" +#include "libutil/cxx/error.hxx" #include <string> #include <sys/stat.h> @@ -31,10 +32,10 @@ struct raii_file { public: virtual ~raii_file() noexcept; - static auto open(const char *fname, int flags) -> tl::expected<raii_file, std::string>; - static auto create(const char *fname, int flags, int perms) -> tl::expected<raii_file, std::string>; - static auto create_temp(const char *fname, int flags, int perms) -> tl::expected<raii_file, std::string>; - static auto mkstemp(const char *pattern, int flags, int perms) -> tl::expected<raii_file, std::string>; + static auto open(const char *fname, int flags) -> tl::expected<raii_file, error>; + static auto create(const char *fname, int flags, int perms) -> tl::expected<raii_file, error>; + static auto create_temp(const char *fname, int flags, int perms) -> tl::expected<raii_file, error>; + static auto mkstemp(const char *pattern, int flags, int perms) -> tl::expected<raii_file, error>; auto get_fd() const -> int { return fd; @@ -135,28 +136,28 @@ struct raii_locked_file final : public raii_file { public: ~raii_locked_file() noexcept override; - static auto open(const char *fname, int flags) -> tl::expected<raii_locked_file, std::string> { + static auto open(const char *fname, int flags) -> tl::expected<raii_locked_file, error> { auto locked = raii_file::open(fname, flags).and_then([]<class T>(T &&file) { return lock_raii_file(std::forward<T>(file)); }); return locked; } - static auto create(const char *fname, int flags, int perms) -> tl::expected<raii_locked_file, std::string> { + static auto create(const char *fname, int flags, int perms) -> tl::expected<raii_locked_file, error> { auto locked = raii_file::create(fname, flags, perms).and_then([]<class T>(T &&file) { return lock_raii_file(std::forward<T>(file)); }); return locked; } - static auto create_temp(const char *fname, int flags, int perms) -> tl::expected<raii_locked_file, std::string> { + static auto create_temp(const char *fname, int flags, int perms) -> tl::expected<raii_locked_file, error> { auto locked = raii_file::create_temp(fname, flags, perms).and_then([]<class T>(T &&file) { return lock_raii_file(std::forward<T>(file)); }); return locked; } - static auto mkstemp(const char *pattern, int flags, int perms) -> tl::expected<raii_locked_file, std::string> { + static auto mkstemp(const char *pattern, int flags, int perms) -> tl::expected<raii_locked_file, error> { auto locked = raii_file::mkstemp(pattern, flags, perms).and_then([]<class T>(T &&file) { return lock_raii_file(std::forward<T>(file)); }); @@ -185,7 +186,7 @@ public: raii_locked_file() = delete; raii_locked_file(const raii_locked_file &other) = delete; private: - static auto lock_raii_file(raii_file &&unlocked) -> tl::expected<raii_locked_file, std::string>; + static auto lock_raii_file(raii_file &&unlocked) -> tl::expected<raii_locked_file, error>; raii_locked_file(raii_file &&other) noexcept : raii_file(std::move(other)) {} explicit raii_locked_file(const char *fname, int fd, bool temp) : raii_file(fname, fd, temp) {} }; @@ -195,8 +196,8 @@ private: */ struct raii_mmaped_file final { ~raii_mmaped_file(); - static auto mmap_shared(raii_file &&file, int flags) -> tl::expected<raii_mmaped_file, std::string>; - static auto mmap_shared(const char *fname, int open_flags, int mmap_flags) -> tl::expected<raii_mmaped_file, std::string>; + static auto mmap_shared(raii_file &&file, int flags) -> tl::expected<raii_mmaped_file, error>; + static auto mmap_shared(const char *fname, int open_flags, int mmap_flags) -> tl::expected<raii_mmaped_file, error>; // Returns a constant pointer to the underlying map auto get_map() const -> void* {return map;} auto get_file() const -> const raii_file& { return file; } @@ -235,7 +236,7 @@ private: */ struct raii_file_sink final { static auto create(const char *fname, int flags, int perms, const char *suffix = "new") - -> tl::expected<raii_file_sink, std::string>; + -> tl::expected<raii_file_sink, error>; auto write_output() -> bool; ~raii_file_sink(); auto get_fd() const -> int |