2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2013-2023 Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2022-08-18 14:14:12 +00:00
|
|
|
#include <algorithm>
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <string_view>
|
|
|
|
|
2022-03-30 19:59:13 +00:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2014-06-23 02:38:53 +00:00
|
|
|
#include "transmission.h"
|
2021-12-15 21:25:42 +00:00
|
|
|
|
2014-06-23 02:38:53 +00:00
|
|
|
#include "error.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2021-12-16 08:49:04 +00:00
|
|
|
#include "tr-macros.h"
|
2014-06-23 02:38:53 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
2023-01-07 18:58:16 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
[[nodiscard]] char* tr_strvdup(std::string_view in)
|
2022-08-18 02:18:44 +00:00
|
|
|
{
|
|
|
|
auto const n = std::size(in);
|
|
|
|
auto* const ret = new char[n + 1];
|
|
|
|
std::copy(std::begin(in), std::end(in), ret);
|
|
|
|
ret[n] = '\0';
|
|
|
|
return ret;
|
|
|
|
}
|
2023-01-07 18:58:16 +00:00
|
|
|
} // namespace
|
2022-08-18 02:18:44 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_error_free(tr_error* error)
|
2014-06-23 02:38:53 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (error == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2022-08-18 02:18:44 +00:00
|
|
|
delete[] error->message;
|
2021-12-28 02:32:22 +00:00
|
|
|
delete error;
|
2014-06-23 02:38:53 +00:00
|
|
|
}
|
|
|
|
|
2021-12-28 02:32:22 +00:00
|
|
|
void tr_error_set(tr_error** error, int code, std::string_view message)
|
2014-06-23 02:38:53 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (error == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(*error == nullptr);
|
2023-01-07 18:58:16 +00:00
|
|
|
*error = new tr_error{ code, tr_strvdup(message) };
|
2014-06-23 02:38:53 +00:00
|
|
|
}
|
|
|
|
|
2023-01-25 18:20:10 +00:00
|
|
|
void tr_error_set_from_errno(tr_error** error, int errnum)
|
|
|
|
{
|
|
|
|
tr_error_set(error, errnum, tr_strerror(errnum));
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_error_propagate(tr_error** new_error, tr_error** old_error)
|
2014-06-23 02:38:53 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(old_error != nullptr);
|
|
|
|
TR_ASSERT(*old_error != nullptr);
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (new_error != nullptr)
|
2014-06-23 02:38:53 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(*new_error == nullptr);
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
*new_error = *old_error;
|
2021-09-15 00:18:09 +00:00
|
|
|
*old_error = nullptr;
|
2014-06-23 02:38:53 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2014-06-23 02:38:53 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_error_clear(old_error);
|
2014-06-23 02:38:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_error_clear(tr_error** error)
|
2014-06-23 02:38:53 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (error == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_error_free(*error);
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
*error = nullptr;
|
2014-06-23 02:38:53 +00:00
|
|
|
}
|
|
|
|
|
2021-12-16 02:09:46 +00:00
|
|
|
void tr_error_prefix(tr_error** error, char const* prefix)
|
2014-06-23 02:38:53 +00:00
|
|
|
{
|
2021-12-16 02:09:46 +00:00
|
|
|
TR_ASSERT(prefix != nullptr);
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (error == nullptr || *error == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2021-12-16 02:09:46 +00:00
|
|
|
auto* err = *error;
|
2023-01-07 18:58:16 +00:00
|
|
|
auto* const new_message = tr_strvdup(fmt::format(FMT_STRING("{:s}{:s}"), prefix, err->message));
|
2022-08-18 02:18:44 +00:00
|
|
|
delete[] err->message;
|
2021-12-16 02:09:46 +00:00
|
|
|
err->message = new_message;
|
2014-06-23 02:38:53 +00:00
|
|
|
}
|