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