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