refactor: don't use varargs in tr_error (#2352)
This commit is contained in:
parent
fa35b8ad39
commit
02aa2f46d4
|
@ -20,6 +20,8 @@
|
|||
|
||||
#include "daemon.h"
|
||||
|
||||
using namespace std::literals;
|
||||
|
||||
/***
|
||||
****
|
||||
***/
|
||||
|
@ -33,9 +35,9 @@ static int signal_pipe[2];
|
|||
****
|
||||
***/
|
||||
|
||||
static void set_system_error(tr_error** error, int code, char const* message)
|
||||
static void set_system_error(tr_error** error, int code, std::string_view message)
|
||||
{
|
||||
tr_error_set(error, code, "%s (%d): %s", message, code, tr_strerror(code));
|
||||
tr_error_set(error, code, tr_strvJoin(message, " ("sv, std::to_string(code), "): "sv, tr_strerror(code)));
|
||||
}
|
||||
|
||||
/***
|
||||
|
|
|
@ -45,8 +45,10 @@ static HANDLE service_stop_thread = nullptr;
|
|||
|
||||
static void set_system_error(tr_error** error, DWORD code, char const* message)
|
||||
{
|
||||
char* const system_message = tr_win32_format_message(code);
|
||||
tr_error_set(error, code, "%s (0x%08lx): %s", message, code, system_message);
|
||||
auto* const system_message = tr_win32_format_message(code);
|
||||
auto* const buf = tr_strdup_printf("%s (0x%08lx): %s", message, code, system_message);
|
||||
tr_error_set(error, code, buf);
|
||||
tr_free(buf);
|
||||
tr_free(system_message);
|
||||
}
|
||||
|
||||
|
|
|
@ -311,7 +311,7 @@ bool gtr_file_trash_or_remove(std::string const& filename, tr_error** error)
|
|||
catch (Glib::Error const& e)
|
||||
{
|
||||
g_message("Unable to trash file \"%s\": %s", filename.c_str(), e.what().c_str());
|
||||
tr_error_set_literal(error, e.code(), e.what().c_str());
|
||||
tr_error_set(error, e.code(), e.what().raw());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -325,7 +325,7 @@ bool gtr_file_trash_or_remove(std::string const& filename, tr_error** error)
|
|||
{
|
||||
g_message("Unable to delete file \"%s\": %s", filename.c_str(), e.what().c_str());
|
||||
tr_error_clear(error);
|
||||
tr_error_set_literal(error, e.code(), e.what().c_str());
|
||||
tr_error_set(error, e.code(), e.what().raw());
|
||||
result = false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,7 +8,6 @@
|
|||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cstdarg>
|
||||
#include <cstring> /* memmove(), memset(), strlen() */
|
||||
#include <random> /* random_device, mt19937, uniform_int_distribution*/
|
||||
#include <string>
|
||||
|
|
|
@ -6,8 +6,6 @@
|
|||
*
|
||||
*/
|
||||
|
||||
#include <cstdarg>
|
||||
|
||||
#include "transmission.h"
|
||||
|
||||
#include "error.h"
|
||||
|
@ -15,30 +13,6 @@
|
|||
#include "tr-macros.h"
|
||||
#include "utils.h"
|
||||
|
||||
tr_error* tr_error_new_literal(int code, char const* message)
|
||||
{
|
||||
TR_ASSERT(message != nullptr);
|
||||
|
||||
auto* const error = tr_new(tr_error, 1);
|
||||
error->code = code;
|
||||
error->message = tr_strdup(message);
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
static tr_error* tr_error_new_valist(int code, char const* message_format, va_list args) TR_GNUC_PRINTF(2, 0);
|
||||
|
||||
static tr_error* tr_error_new_valist(int code, char const* message_format, va_list args)
|
||||
{
|
||||
TR_ASSERT(message_format != nullptr);
|
||||
|
||||
auto* const error = tr_new(tr_error, 1);
|
||||
error->code = code;
|
||||
error->message = tr_strdup_vprintf(message_format, args);
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
void tr_error_free(tr_error* error)
|
||||
{
|
||||
if (error == nullptr)
|
||||
|
@ -47,39 +21,18 @@ void tr_error_free(tr_error* error)
|
|||
}
|
||||
|
||||
tr_free(error->message);
|
||||
tr_free(error);
|
||||
delete error;
|
||||
}
|
||||
|
||||
void tr_error_set(tr_error** error, int code, char const* message_format, ...)
|
||||
void tr_error_set(tr_error** error, int code, std::string_view message)
|
||||
{
|
||||
TR_ASSERT(message_format != nullptr);
|
||||
|
||||
if (error == nullptr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
TR_ASSERT(*error == nullptr);
|
||||
|
||||
va_list args;
|
||||
|
||||
va_start(args, message_format);
|
||||
*error = tr_error_new_valist(code, message_format, args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
void tr_error_set_literal(tr_error** error, int code, char const* message)
|
||||
{
|
||||
TR_ASSERT(message != nullptr);
|
||||
|
||||
if (error == nullptr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
TR_ASSERT(*error == nullptr);
|
||||
|
||||
*error = tr_error_new_literal(code, message);
|
||||
*error = new tr_error{ code, tr_strvDup(message) };
|
||||
}
|
||||
|
||||
void tr_error_propagate(tr_error** new_error, tr_error** old_error)
|
||||
|
|
|
@ -8,6 +8,8 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <string_view>
|
||||
|
||||
#include "tr-macros.h"
|
||||
|
||||
/**
|
||||
|
@ -24,16 +26,6 @@ struct tr_error
|
|||
char* message;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Create new error object using literal error message.
|
||||
*
|
||||
* @param[in] code Error code (platform-specific).
|
||||
* @param[in] message Error message.
|
||||
*
|
||||
* @return Newly allocated error object on success, `nullptr` otherwise.
|
||||
*/
|
||||
tr_error* tr_error_new_literal(int code, char const* message);
|
||||
|
||||
/**
|
||||
* @brief Free memory used by error object.
|
||||
*
|
||||
|
@ -41,18 +33,6 @@ tr_error* tr_error_new_literal(int code, char const* message);
|
|||
*/
|
||||
void tr_error_free(tr_error* error);
|
||||
|
||||
/**
|
||||
* @brief Create and set new error object using `printf`-style formatting.
|
||||
*
|
||||
* If passed pointer to error object is `nullptr`, do nothing.
|
||||
*
|
||||
* @param[in,out] error Pointer to error object to be set.
|
||||
* @param[in] code Error code (platform-specific).
|
||||
* @param[in] message_format Error message format string.
|
||||
* @param[in] ... Format arguments.
|
||||
*/
|
||||
void tr_error_set(tr_error** error, int code, char const* message_format, ...) TR_GNUC_PRINTF(3, 4);
|
||||
|
||||
/**
|
||||
* @brief Create and set new error object using literal error message.
|
||||
*
|
||||
|
@ -62,7 +42,7 @@ void tr_error_set(tr_error** error, int code, char const* message_format, ...) T
|
|||
* @param[in] code Error code (platform-specific).
|
||||
* @param[in] message Error message.
|
||||
*/
|
||||
void tr_error_set_literal(tr_error** error, int code, char const* message);
|
||||
void tr_error_set(tr_error** error, int code, std::string_view message);
|
||||
|
||||
/**
|
||||
* @brief Propagate existing error object upwards.
|
||||
|
|
|
@ -96,6 +96,8 @@
|
|||
#endif
|
||||
#endif
|
||||
|
||||
using namespace std::literals;
|
||||
|
||||
static void set_system_error(tr_error** error, int code)
|
||||
{
|
||||
if (error == nullptr)
|
||||
|
@ -103,7 +105,7 @@ static void set_system_error(tr_error** error, int code)
|
|||
return;
|
||||
}
|
||||
|
||||
tr_error_set_literal(error, code, tr_strerror(code));
|
||||
tr_error_set(error, code, tr_strerror(code));
|
||||
}
|
||||
|
||||
static void set_system_error_if_file_found(tr_error** error, int code)
|
||||
|
@ -175,7 +177,7 @@ static bool create_path_require_dir(char const* path, tr_error** error)
|
|||
|
||||
if ((sb.st_mode & S_IFMT) != S_IFDIR)
|
||||
{
|
||||
tr_error_set(error, ENOTDIR, _("File \"%s\" is in the way"), path);
|
||||
tr_error_set(error, ENOTDIR, tr_strvJoin("File is in the way: "sv, path));
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
@ -7,8 +7,9 @@
|
|||
*/
|
||||
|
||||
#include <algorithm>
|
||||
#include <ctype.h> /* isalpha() */
|
||||
#include <array>
|
||||
#include <cstring>
|
||||
#include <ctype.h> /* isalpha() */
|
||||
#include <string_view>
|
||||
|
||||
#include <shlobj.h> /* SHCreateDirectoryEx() */
|
||||
|
@ -55,12 +56,14 @@ static void set_system_error(tr_error** error, DWORD code)
|
|||
|
||||
if (message != nullptr)
|
||||
{
|
||||
tr_error_set_literal(error, code, message);
|
||||
tr_error_set(error, code, message);
|
||||
tr_free(message);
|
||||
}
|
||||
else
|
||||
{
|
||||
tr_error_set(error, code, "Unknown error: 0x%08lx", code);
|
||||
auto buf = std::array<char, 32>{};
|
||||
tr_snprintf(std::data(buf), std::size(buf), "Unknown error: 0x%08lx", code);
|
||||
tr_error_set(error, code, std::data(buf));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
*/
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdarg>
|
||||
#include <cstring> /* strlen() */
|
||||
|
||||
#include "transmission.h"
|
||||
|
@ -15,6 +16,8 @@
|
|||
#include "tr-assert.h"
|
||||
#include "utils.h"
|
||||
|
||||
using namespace std::literals;
|
||||
|
||||
bool tr_sys_file_read_line(tr_sys_file_t handle, char* buffer, size_t buffer_size, tr_error** error)
|
||||
{
|
||||
TR_ASSERT(handle != TR_BAD_SYS_FILE);
|
||||
|
@ -114,7 +117,7 @@ bool tr_sys_file_write_fmt(tr_sys_file_t handle, char const* format, tr_error**
|
|||
}
|
||||
else
|
||||
{
|
||||
tr_error_set_literal(error, 0, "Unable to format message.");
|
||||
tr_error_set(error, 0, "Unable to format message."sv);
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
*/
|
||||
|
||||
#include <cerrno>
|
||||
#include <cstdarg>
|
||||
#include <cstdio>
|
||||
#include <mutex>
|
||||
|
||||
|
|
|
@ -152,7 +152,7 @@ bool tr_magnet_metainfo::parseMagnet(std::string_view magnet_link, tr_error** er
|
|||
auto const parsed = tr_urlParse(magnet_link);
|
||||
if (!parsed || parsed->scheme != "magnet"sv)
|
||||
{
|
||||
tr_error_set_literal(error, TR_ERROR_EINVAL, "Error parsing URL");
|
||||
tr_error_set(error, TR_ERROR_EINVAL, "Error parsing URL"sv);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
@ -535,7 +535,7 @@ std::optional<tr_metainfo_parsed> tr_metainfoParse(tr_session const* session, tr
|
|||
char const* bad_tag = tr_metainfoParseImpl(session, &out.info, &out.pieces, &out.info_dict_length, meta_in);
|
||||
if (bad_tag != nullptr)
|
||||
{
|
||||
tr_error_set(error, TR_ERROR_EINVAL, _("Error parsing metainfo: %s"), bad_tag);
|
||||
tr_error_set(error, TR_ERROR_EINVAL, tr_strvJoin("Error parsing metainfo: "sv, bad_tag));
|
||||
tr_metainfoFree(&out.info);
|
||||
return {};
|
||||
}
|
||||
|
|
|
@ -24,6 +24,8 @@
|
|||
#include "tr-macros.h"
|
||||
#include "utils.h"
|
||||
|
||||
using namespace std::literals;
|
||||
|
||||
static void handle_sigchld(int /*i*/)
|
||||
{
|
||||
int rc = 0;
|
||||
|
@ -37,21 +39,14 @@ static void handle_sigchld(int /*i*/)
|
|||
/* FIXME: Call old handler, if any */
|
||||
}
|
||||
|
||||
static void set_system_error(tr_error** error, int code, char const* what)
|
||||
static void set_system_error(tr_error** error, int code, std::string_view what)
|
||||
{
|
||||
if (error == nullptr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (what == nullptr)
|
||||
{
|
||||
tr_error_set_literal(error, code, tr_strerror(code));
|
||||
}
|
||||
else
|
||||
{
|
||||
tr_error_set(error, code, "%s failed: %s", what, tr_strerror(code));
|
||||
}
|
||||
tr_error_set(error, code, tr_strvJoin(what, " failed "sv, tr_strerror(code)));
|
||||
}
|
||||
|
||||
static bool tr_spawn_async_in_child(
|
||||
|
|
|
@ -30,7 +30,7 @@ enum tr_app_type
|
|||
TR_APP_TYPE_BATCH
|
||||
};
|
||||
|
||||
static void set_system_error(tr_error** error, DWORD code, char const* what)
|
||||
static void set_system_error(tr_error** error, DWORD code, std::string_view what)
|
||||
{
|
||||
if (error == nullptr)
|
||||
{
|
||||
|
@ -44,14 +44,7 @@ static void set_system_error(tr_error** error, DWORD code, char const* what)
|
|||
message = tr_strdup_printf("Unknown error: 0x%08lx", code);
|
||||
}
|
||||
|
||||
if (what == nullptr)
|
||||
{
|
||||
tr_error_set_literal(error, code, message);
|
||||
}
|
||||
else
|
||||
{
|
||||
tr_error_set(error, code, "%s failed: %s", what, message);
|
||||
}
|
||||
tr_error_set(error, code, tr_strvJoin(what, " failed: "sv, message));
|
||||
|
||||
tr_free(message);
|
||||
}
|
||||
|
|
|
@ -71,7 +71,7 @@ bool tr_ctorSetMetainfoFromFile(tr_ctor* ctor, char const* filename, tr_error**
|
|||
{
|
||||
if (filename == nullptr)
|
||||
{
|
||||
tr_error_set_literal(error, EINVAL, "no filename specified");
|
||||
tr_error_set(error, EINVAL, "no filename specified"sv);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -116,7 +116,7 @@ bool tr_ctorSaveContents(tr_ctor const* ctor, std::string_view filename, tr_erro
|
|||
|
||||
if (std::empty(ctor->contents))
|
||||
{
|
||||
tr_error_set_literal(error, EINVAL, "torrent ctor has no contents to save");
|
||||
tr_error_set(error, EINVAL, "torrent ctor has no contents to save"sv);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
@ -637,7 +637,7 @@ bool tr_torrent_metainfo::parseBenc(std::string_view benc, tr_error** error)
|
|||
tr_variantFree(&top);
|
||||
if (!std::empty(errmsg))
|
||||
{
|
||||
tr_error_set(error, TR_ERROR_EINVAL, "Error parsing metainfo: %" TR_PRIsv, TR_PRIsv_ARG(errmsg));
|
||||
tr_error_set(error, TR_ERROR_EINVAL, tr_strvJoin("Error parsing metainfo: ", errmsg));
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
@ -283,7 +283,7 @@ uint8_t* tr_loadFile(char const* path, size_t* size, tr_error** error)
|
|||
if (info.type != TR_SYS_PATH_IS_FILE)
|
||||
{
|
||||
tr_logAddError(err_fmt, path, _("Not a regular file"));
|
||||
tr_error_set_literal(error, TR_ERROR_EISDIR, _("Not a regular file"));
|
||||
tr_error_set(error, TR_ERROR_EISDIR, "Not a regular file"sv);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
@ -337,7 +337,7 @@ bool tr_loadFile(std::vector<char>& setme, std::string_view path_sv, tr_error**
|
|||
if (info.type != TR_SYS_PATH_IS_FILE)
|
||||
{
|
||||
tr_logAddError(err_fmt, path_sz, _("Not a regular file"));
|
||||
tr_error_set_literal(error, TR_ERROR_EISDIR, _("Not a regular file"));
|
||||
tr_error_set(error, TR_ERROR_EISDIR, "Not a regular file"sv);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -1251,7 +1251,7 @@ bool tr_moveFile(char const* oldpath, char const* newpath, tr_error** error)
|
|||
|
||||
if (info.type != TR_SYS_PATH_IS_FILE)
|
||||
{
|
||||
tr_error_set_literal(error, TR_ERROR_EINVAL, "Old path does not point to a file.");
|
||||
tr_error_set(error, TR_ERROR_EINVAL, "Old path does not point to a file."sv);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
@ -1243,7 +1243,7 @@ bool tr_variantFromBuf(tr_variant* setme, int opts, std::string_view buf, char c
|
|||
|
||||
if (err)
|
||||
{
|
||||
tr_error_set_literal(error, EILSEQ, "error parsing encoded data");
|
||||
tr_error_set(error, EILSEQ, "error parsing encoded data"sv);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
@ -141,7 +141,7 @@ bool trashDataFile(char const* filename, tr_error** error)
|
|||
NSError* localError;
|
||||
if (![Torrent trashFile:@(filename) error:&localError])
|
||||
{
|
||||
tr_error_set_literal(error, localError.code, localError.description.UTF8String);
|
||||
tr_error_set(error, localError.code, localError.description.UTF8String);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,6 +11,8 @@
|
|||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
using namespace std::literals;
|
||||
|
||||
TEST(Error, errorSet)
|
||||
{
|
||||
tr_error* err = nullptr;
|
||||
|
@ -18,14 +20,7 @@ TEST(Error, errorSet)
|
|||
tr_error_prefix(&err, "error: ");
|
||||
EXPECT_EQ(nullptr, err);
|
||||
|
||||
tr_error_set(&err, 1, "error: %s (%d)", "oops", 2);
|
||||
EXPECT_NE(nullptr, err);
|
||||
EXPECT_EQ(1, err->code);
|
||||
EXPECT_STREQ("error: oops (2)", err->message);
|
||||
tr_error_clear(&err);
|
||||
EXPECT_EQ(nullptr, err);
|
||||
|
||||
tr_error_set_literal(&err, 2, "oops");
|
||||
tr_error_set(&err, 2, "oops"sv);
|
||||
EXPECT_NE(nullptr, err);
|
||||
EXPECT_EQ(2, err->code);
|
||||
EXPECT_STREQ("oops", err->message);
|
||||
|
@ -44,7 +39,7 @@ TEST(Error, propagate)
|
|||
tr_error* err2 = nullptr;
|
||||
auto constexpr Code = int{ 1 };
|
||||
|
||||
tr_error_set_literal(&err, Code, "oops");
|
||||
tr_error_set(&err, Code, "oops"sv);
|
||||
EXPECT_NE(nullptr, err);
|
||||
EXPECT_EQ(Code, err->code);
|
||||
EXPECT_STREQ("oops", err->message);
|
||||
|
|
Loading…
Reference in New Issue