2014-06-23 02:38:53 +00:00
|
|
|
/*
|
|
|
|
* This file Copyright (C) 2013-2014 Mnemosyne LLC
|
|
|
|
*
|
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "transmission.h"
|
|
|
|
#include "error.h"
|
|
|
|
#include "utils.h"
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_error* tr_error_new(int code, char const* message_format, ...)
|
2014-06-23 02:38:53 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_error* error;
|
|
|
|
va_list args;
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(message_format != NULL);
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
va_start(args, message_format);
|
|
|
|
error = tr_error_new_valist(code, message_format, args);
|
|
|
|
va_end(args);
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return error;
|
2014-06-23 02:38:53 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_error* tr_error_new_literal(int code, char const* message)
|
2014-06-23 02:38:53 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_error* error;
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(message != NULL);
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
error = tr_new(tr_error, 1);
|
|
|
|
error->code = code;
|
|
|
|
error->message = tr_strdup(message);
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return error;
|
2014-06-23 02:38:53 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_error* tr_error_new_valist(int code, char const* message_format, va_list args)
|
2014-06-23 02:38:53 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_error* error;
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(message_format != NULL);
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
error = tr_new(tr_error, 1);
|
|
|
|
error->code = code;
|
|
|
|
error->message = tr_strdup_vprintf(message_format, args);
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return error;
|
2014-06-23 02:38:53 +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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (error == NULL)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(error->message);
|
|
|
|
tr_free(error);
|
2014-06-23 02:38:53 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_error_set(tr_error** error, int code, char const* message_format, ...)
|
2014-06-23 02:38:53 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
va_list args;
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (error == NULL)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(*error == NULL);
|
|
|
|
assert(message_format != NULL);
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
va_start(args, message_format);
|
|
|
|
*error = tr_error_new_valist(code, message_format, args);
|
|
|
|
va_end(args);
|
2014-06-23 02:38:53 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_error_set_literal(tr_error** error, int code, char const* message)
|
2014-06-23 02:38:53 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (error == NULL)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(*error == NULL);
|
|
|
|
assert(message != NULL);
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
*error = tr_error_new_literal(code, 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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(old_error != NULL);
|
|
|
|
assert(*old_error != NULL);
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (new_error != NULL)
|
2014-06-23 02:38:53 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(*new_error == NULL);
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
*new_error = *old_error;
|
|
|
|
*old_error = NULL;
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (error == NULL)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
*error = NULL;
|
2014-06-23 02:38:53 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void error_prefix_valist(tr_error** error, char const* prefix_format, va_list args)
|
2014-06-23 02:38:53 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char* prefix;
|
|
|
|
char* new_message;
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(error != NULL);
|
|
|
|
assert(*error != NULL);
|
|
|
|
assert(prefix_format != NULL);
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
prefix = tr_strdup_vprintf(prefix_format, args);
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
new_message = tr_strdup_printf("%s%s", prefix, (*error)->message);
|
|
|
|
tr_free((*error)->message);
|
|
|
|
(*error)->message = new_message;
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(prefix);
|
2014-06-23 02:38:53 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_error_prefix(tr_error** error, char const* prefix_format, ...)
|
2014-06-23 02:38:53 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
va_list args;
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(prefix_format != NULL);
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (error == NULL || *error == NULL)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
va_start(args, prefix_format);
|
|
|
|
error_prefix_valist(error, prefix_format, args);
|
|
|
|
va_end(args);
|
2014-06-23 02:38:53 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_error_propagate_prefixed(tr_error** new_error, tr_error** old_error, char const* prefix_format, ...)
|
2014-06-23 02:38:53 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
va_list args;
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(prefix_format != NULL);
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_error_propagate(new_error, old_error);
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (new_error == NULL)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
va_start(args, prefix_format);
|
|
|
|
error_prefix_valist(new_error, prefix_format, args);
|
|
|
|
va_end(args);
|
2014-06-23 02:38:53 +00:00
|
|
|
}
|