From 3735397a0ff5347308f3dae9d56ec1a0e61133e6 Mon Sep 17 00:00:00 2001 From: Trygve Aaberge Date: Fri, 13 Oct 2023 09:26:00 +0200 Subject: [PATCH] core: add error codes to output in hook_url When hook_url fails, add an error_code field in the output in addition to the error field. This is so the caller can get which error happened programatically, without having to parse the (possibly translated) error string. It uses the same error codes as the return_code in hook_process, and in addition adds 5 for an error from pthread_create and 6 for a timeout error. If the error is from pthread_create, an additional field `error_code_pthread` with the error code from pthread_create is added. If the error is from curl, an additional field `error_code_curl` with the error code from curl is added. --- src/core/hook/wee-hook-url.c | 27 ++++++++++++++++++++++----- src/core/wee-url.c | 7 ++++++- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/core/hook/wee-hook-url.c b/src/core/hook/wee-hook-url.c index 4c10d028b..f34d767a2 100644 --- a/src/core/hook/wee-hook-url.c +++ b/src/core/hook/wee-hook-url.c @@ -126,14 +126,22 @@ void * hook_url_transfer_thread (void *hook_pointer) { struct t_hook *hook; + int url_rc; + char str_error_code[12]; hook = (struct t_hook *)hook_pointer; pthread_cleanup_push (&hook_url_thread_cleanup, hook); - (void) weeurl_download (HOOK_URL(hook, url), - HOOK_URL(hook, options), - HOOK_URL(hook, output)); + url_rc = weeurl_download (HOOK_URL(hook, url), + HOOK_URL(hook, options), + HOOK_URL(hook, output)); + + if (url_rc != 0) + { + snprintf (str_error_code, sizeof (str_error_code), "%d", url_rc); + hashtable_set (HOOK_URL(hook, output), "error_code", str_error_code); + } pthread_cleanup_pop (1); @@ -149,7 +157,7 @@ hook_url_timer_cb (const void *pointer, void *data, int remaining_calls) { struct t_hook *hook; const char *ptr_error; - char str_error[1024]; + char str_error[1024], str_error_code[12]; /* make C compiler happy */ (void) data; @@ -179,12 +187,14 @@ hook_url_timer_cb (const void *pointer, void *data, int remaining_calls) if (remaining_calls == 0) { - if (!hashtable_has_key (HOOK_URL(hook, output), "error")) + if (!hashtable_has_key (HOOK_URL(hook, output), "error_code")) { snprintf (str_error, sizeof (str_error), "transfer timeout reached (%.3fs)", ((float)HOOK_URL(hook, timeout)) / 1000); + snprintf (str_error_code, sizeof (str_error_code), "6"); hashtable_set (HOOK_URL(hook, output), "error", str_error); + hashtable_set (HOOK_URL(hook, output), "error_code", str_error_code); } hook_url_run_callback (hook); if (weechat_debug_core >= 1) @@ -212,6 +222,7 @@ hook_url_transfer (struct t_hook *hook) { int rc, timeout, max_calls; long interval; + char str_error[1024], str_error_code[12], str_error_code_pthread[12]; HOOK_URL(hook, thread_running) = 1; @@ -222,7 +233,13 @@ hook_url_transfer (struct t_hook *hook) { snprintf (str_error, sizeof (str_error), "error calling pthread_create (%d)", rc); + snprintf (str_error_code, sizeof (str_error_code), "5"); + snprintf (str_error_code_pthread, sizeof (str_error_code_pthread), + "%d", rc); hashtable_set (HOOK_URL(hook, output), "error", str_error); + hashtable_set (HOOK_URL(hook, output), "error_code", str_error_code); + hashtable_set (HOOK_URL(hook, output), "error_code_pthread", + str_error_code_pthread); hook_url_run_callback (hook); gui_chat_printf (NULL, diff --git a/src/core/wee-url.c b/src/core/wee-url.c index 7ec89b94f..8f77a048d 100644 --- a/src/core/wee-url.c +++ b/src/core/wee-url.c @@ -1344,7 +1344,8 @@ weeurl_download (const char *url, struct t_hashtable *options, struct t_url_file url_file[2]; char *url_file_option[2] = { "file_in", "file_out" }; char *url_file_mode[2] = { "rb", "wb" }; - char url_error[CURL_ERROR_SIZE + 1], **string_headers, **string_output; + char url_error[CURL_ERROR_SIZE + 1], url_error_code[12]; + char **string_headers, **string_output; char str_response_code[32]; CURLoption url_file_opt_func[2] = { CURLOPT_READFUNCTION, CURLOPT_WRITEFUNCTION }; CURLoption url_file_opt_data[2] = { CURLOPT_READDATA, CURLOPT_WRITEDATA }; @@ -1358,6 +1359,7 @@ weeurl_download (const char *url, struct t_hashtable *options, string_headers = NULL; string_output = NULL; url_error[0] = '\0'; + url_error_code[0] = '\0'; for (i = 0; i < 2; i++) { @@ -1465,6 +1467,7 @@ weeurl_download (const char *url, struct t_hashtable *options, { if (output) { + snprintf (url_error_code, sizeof (url_error_code), "%d", curl_rc); if (!url_error[0]) { snprintf (url_error, sizeof (url_error), @@ -1507,6 +1510,8 @@ end: } if (url_error[0]) hashtable_set (output, "error", url_error); + if (url_error_code[0]) + hashtable_set (output, "error_code_curl", url_error_code); } return rc; }