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