CURLOPT_ERRORBUFFER.md (3070B)
1 --- 2 c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 3 SPDX-License-Identifier: curl 4 Title: CURLOPT_ERRORBUFFER 5 Section: 3 6 Source: libcurl 7 See-also: 8 - CURLOPT_DEBUGFUNCTION (3) 9 - CURLOPT_VERBOSE (3) 10 - curl_easy_strerror (3) 11 - curl_multi_strerror (3) 12 - curl_share_strerror (3) 13 - curl_url_strerror (3) 14 Protocol: 15 - All 16 Added-in: 7.1 17 --- 18 19 # NAME 20 21 CURLOPT_ERRORBUFFER - error buffer for error messages 22 23 # SYNOPSIS 24 25 ~~~c 26 #include <curl/curl.h> 27 28 CURLcode curl_easy_setopt(CURL *handle, CURLOPT_ERRORBUFFER, char *buf); 29 ~~~ 30 31 # DESCRIPTION 32 33 Pass a char pointer to a buffer that libcurl may use to store human readable 34 error messages on failures or problems. This may be more helpful than just the 35 return code from curl_easy_perform(3) and related functions. The buffer must 36 be at least **CURL_ERROR_SIZE** bytes big. 37 38 You must keep the associated buffer available until libcurl no longer needs 39 it. Failing to do so might cause odd behavior or even crashes. libcurl might 40 need it until you call curl_easy_cleanup(3) or you set the same option again 41 to use a different pointer. 42 43 Do not rely on the contents of the buffer unless an error code was returned. 44 Since 7.60.0 libcurl initializes the contents of the error buffer to an empty 45 string before performing the transfer. For earlier versions if an error code 46 was returned but there was no error detail then the buffer was untouched. 47 48 Do not attempt to set the contents of the buffer yourself, including in any 49 callbacks you write that may be called by libcurl. The library may overwrite 50 the buffer after your callback returns. 51 52 Consider CURLOPT_VERBOSE(3) and CURLOPT_DEBUGFUNCTION(3) to better debug and 53 trace why errors happen. 54 55 Using this option multiple times makes the last set pointer override the 56 previous ones. Set it to NULL to disable its use again. 57 58 # DEFAULT 59 60 NULL 61 62 # %PROTOCOLS% 63 64 # EXAMPLE 65 66 ~~~c 67 #include <string.h> /* for strlen() */ 68 int main(void) 69 { 70 CURL *curl = curl_easy_init(); 71 if(curl) { 72 CURLcode res; 73 char errbuf[CURL_ERROR_SIZE]; 74 75 curl_easy_setopt(curl, CURLOPT_URL, "https://example.com"); 76 77 /* provide a buffer to store errors in */ 78 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf); 79 80 /* set the error buffer as empty before performing a request */ 81 errbuf[0] = 0; 82 83 /* perform the request */ 84 res = curl_easy_perform(curl); 85 86 /* if the request did not complete correctly, show the error 87 information. if no detailed error information was written to errbuf 88 show the more generic information from curl_easy_strerror instead. 89 */ 90 if(res != CURLE_OK) { 91 size_t len = strlen(errbuf); 92 fprintf(stderr, "\nlibcurl: (%d) ", res); 93 if(len) 94 fprintf(stderr, "%s%s", errbuf, 95 ((errbuf[len - 1] != '\n') ? "\n" : "")); 96 else 97 fprintf(stderr, "%s\n", curl_easy_strerror(res)); 98 } 99 } 100 } 101 ~~~ 102 103 # %AVAILABILITY% 104 105 # RETURN VALUE 106 107 curl_easy_setopt(3) returns a CURLcode indicating success or error. 108 109 CURLE_OK (0) means everything was OK, non-zero means an error occurred, see 110 libcurl-errors(3).