[totem-pl-parser/wip/hadess/cancelled-return] plparser: Fix return value from cancelled calls



commit e4dc32648883e0459518d59a10d8d174afa5dae4
Author: Bastien Nocera <hadess hadess net>
Date:   Mon Jun 20 16:10:55 2022 +0200

    plparser: Fix return value from cancelled calls
    
    As per the API documentation, and the code when it still used
    g_simple_async_result_*, totem_pl_parser_parse_finish() should return a
    TotemPlParserResult of TOTEM_PL_PARSER_RESULT_CANCELLED when parsing is
    cancelled.
    
    But the port to GTask changed that, as all the errors caught during
    parsing would return -1 as the error code, which isn't a valid
    TotemPlParserResult value.
    
    As the only way for totem_pl_parser_parse_finish() to have a GError set
    is for the call to be cancelled, catch that and correct the return
    value.
    
    Fixes: 45664037 ("plparse: Port from GSimpleAsyncResult to GTask")
    Closes: #38

 plparse/totem-pl-parser.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
---
diff --git a/plparse/totem-pl-parser.c b/plparse/totem-pl-parser.c
index b853cb7..e10d40e 100644
--- a/plparse/totem-pl-parser.c
+++ b/plparse/totem-pl-parser.c
@@ -2408,12 +2408,24 @@ TotemPlParserResult
 totem_pl_parser_parse_finish (TotemPlParser *parser, GAsyncResult *async_result, GError **error)
 {
        GTask *task = G_TASK (async_result);
+       GError *local_error = NULL;
+       int ret;
 
        g_return_val_if_fail (TOTEM_PL_IS_PARSER (parser), TOTEM_PL_PARSER_RESULT_UNHANDLED);
        g_return_val_if_fail (g_task_is_valid (async_result, parser), TOTEM_PL_PARSER_RESULT_UNHANDLED);
 
        /* Propagate any errors which were caught and return the result; otherwise just return the result */
-       return g_task_propagate_int (task, error);
+       ret = g_task_propagate_int (task, &local_error);
+       if (ret == -1) {
+               if (g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
+                       ret = TOTEM_PL_PARSER_RESULT_CANCELLED;
+                       g_error_free (local_error);
+               } else {
+                       g_warning ("Unexpected error from asynchronous parsing: %s", local_error->message);
+                       g_propagate_error (error, local_error);
+               }
+       }
+       return ret;
 }
 
 /**


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]