2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2013-2022 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-01-13 02:13:58 +00:00
|
|
|
#include <string_view>
|
|
|
|
|
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"
|
|
|
|
|
2022-01-28 22:46:14 +00:00
|
|
|
bool tr_error_is_set(tr_error const* const* error)
|
|
|
|
{
|
|
|
|
return (error != nullptr) && (*error != nullptr);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(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);
|
2021-12-28 02:32:22 +00:00
|
|
|
*error = new tr_error{ code, tr_strvDup(message) };
|
2014-06-23 02:38:53 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
auto* const new_message = tr_strvDup(tr_strvJoin(prefix, err->message));
|
|
|
|
tr_free(err->message);
|
|
|
|
err->message = new_message;
|
2014-06-23 02:38:53 +00:00
|
|
|
}
|