mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-03-02 14:12:10 +01:00
gtask: Add g_task_return_new_error_literal()
Avoids going through the formatting function or nesting GError calls.
This commit is contained in:
parent
f08609b169
commit
7c71090723
29
gio/gtask.c
29
gio/gtask.c
@ -2031,7 +2031,8 @@ g_task_propagate_boolean (GTask *task,
|
|||||||
* Call g_error_copy() on the error if you need to keep a local copy
|
* Call g_error_copy() on the error if you need to keep a local copy
|
||||||
* as well.
|
* as well.
|
||||||
*
|
*
|
||||||
* See also g_task_return_new_error().
|
* See also [method@Gio.Task.return_new_error],
|
||||||
|
* [method@Gio.Task.return_new_error_literal].
|
||||||
*
|
*
|
||||||
* Since: 2.36
|
* Since: 2.36
|
||||||
*/
|
*/
|
||||||
@ -2130,6 +2131,32 @@ g_task_return_new_error (GTask *task,
|
|||||||
g_task_return_error (task, error);
|
g_task_return_error (task, error);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* g_task_return_new_error_literal:
|
||||||
|
* @task: a #GTask.
|
||||||
|
* @domain: a #GQuark.
|
||||||
|
* @code: an error code.
|
||||||
|
* @message: an error message
|
||||||
|
*
|
||||||
|
* Sets @task’s result to a new [type@GLib.Error] created from @domain, @code,
|
||||||
|
* @message and completes the task.
|
||||||
|
*
|
||||||
|
* See [method@Gio.Task.return_pointer] for more discussion of exactly what
|
||||||
|
* ‘completing the task’ means.
|
||||||
|
*
|
||||||
|
* See also [method@Gio.Task.return_new_error].
|
||||||
|
*
|
||||||
|
* Since: 2.80
|
||||||
|
*/
|
||||||
|
void
|
||||||
|
g_task_return_new_error_literal (GTask *task,
|
||||||
|
GQuark domain,
|
||||||
|
gint code,
|
||||||
|
const char *message)
|
||||||
|
{
|
||||||
|
g_task_return_error (task, g_error_new_literal (domain, code, message));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* g_task_return_error_if_cancelled:
|
* g_task_return_error_if_cancelled:
|
||||||
* @task: a #GTask
|
* @task: a #GTask
|
||||||
|
@ -175,6 +175,12 @@ void g_task_return_new_error (GTask *task,
|
|||||||
gint code,
|
gint code,
|
||||||
const char *format,
|
const char *format,
|
||||||
...) G_GNUC_PRINTF (4, 5);
|
...) G_GNUC_PRINTF (4, 5);
|
||||||
|
|
||||||
|
GIO_AVAILABLE_IN_2_80
|
||||||
|
void g_task_return_new_error_literal (GTask *task,
|
||||||
|
GQuark domain,
|
||||||
|
gint code,
|
||||||
|
const char *message);
|
||||||
GIO_AVAILABLE_IN_2_64
|
GIO_AVAILABLE_IN_2_64
|
||||||
void g_task_return_value (GTask *task,
|
void g_task_return_value (GTask *task,
|
||||||
GValue *result);
|
GValue *result);
|
||||||
|
@ -213,6 +213,53 @@ test_error (void)
|
|||||||
g_free (result.expected_message);
|
g_free (result.expected_message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_error_literal (void)
|
||||||
|
{
|
||||||
|
GTask *task;
|
||||||
|
TaskErrorResult result;
|
||||||
|
|
||||||
|
task = g_task_new (NULL, NULL, error_callback, &result);
|
||||||
|
result = (TaskErrorResult){
|
||||||
|
.expected_domain = G_IO_ERROR,
|
||||||
|
.expected_code = G_IO_ERROR_FAILED,
|
||||||
|
.expected_message = "Literal Failure",
|
||||||
|
};
|
||||||
|
|
||||||
|
g_task_return_new_error_literal (task,
|
||||||
|
result.expected_domain,
|
||||||
|
result.expected_code,
|
||||||
|
"Literal Failure");
|
||||||
|
|
||||||
|
wait_for_completed_notification (task);
|
||||||
|
g_assert_cmpint (result.int_result, ==, -1);
|
||||||
|
|
||||||
|
g_assert_finalize_object (task);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_error_literal_from_variable (void)
|
||||||
|
{
|
||||||
|
GTask *task;
|
||||||
|
TaskErrorResult result;
|
||||||
|
|
||||||
|
task = g_task_new (NULL, NULL, error_callback, &result);
|
||||||
|
result = (TaskErrorResult){
|
||||||
|
.expected_domain = G_IO_ERROR,
|
||||||
|
.expected_code = G_IO_ERROR_FAILED,
|
||||||
|
.expected_message = "Literal Failure",
|
||||||
|
};
|
||||||
|
|
||||||
|
g_task_return_new_error_literal (task,
|
||||||
|
result.expected_domain,
|
||||||
|
result.expected_code,
|
||||||
|
result.expected_message);
|
||||||
|
|
||||||
|
wait_for_completed_notification (task);
|
||||||
|
g_assert_cmpint (result.int_result, ==, -1);
|
||||||
|
g_assert_finalize_object (task);
|
||||||
|
}
|
||||||
|
|
||||||
/* test_return_from_same_iteration: calling g_task_return_* from the
|
/* test_return_from_same_iteration: calling g_task_return_* from the
|
||||||
* loop iteration the task was created in defers completion until the
|
* loop iteration the task was created in defers completion until the
|
||||||
* next iteration.
|
* next iteration.
|
||||||
@ -2532,6 +2579,8 @@ main (int argc, char **argv)
|
|||||||
|
|
||||||
g_test_add_func ("/gtask/basic", test_basic);
|
g_test_add_func ("/gtask/basic", test_basic);
|
||||||
g_test_add_func ("/gtask/error", test_error);
|
g_test_add_func ("/gtask/error", test_error);
|
||||||
|
g_test_add_func ("/gtask/error-literal", test_error_literal);
|
||||||
|
g_test_add_func ("/gtask/error-literal-from-variable", test_error_literal_from_variable);
|
||||||
g_test_add_func ("/gtask/return-from-same-iteration", test_return_from_same_iteration);
|
g_test_add_func ("/gtask/return-from-same-iteration", test_return_from_same_iteration);
|
||||||
g_test_add_func ("/gtask/return-from-toplevel", test_return_from_toplevel);
|
g_test_add_func ("/gtask/return-from-toplevel", test_return_from_toplevel);
|
||||||
g_test_add_func ("/gtask/return-from-anon-thread", test_return_from_anon_thread);
|
g_test_add_func ("/gtask/return-from-anon-thread", test_return_from_anon_thread);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user