andelf / go-curl

golang curl(libcurl) binding.
Apache License 2.0
478 stars 129 forks source link

Get cURL error code #78

Open dwill20 opened 3 years ago

dwill20 commented 3 years ago

I haven't found a way to get cURL error code so far. When using EasyInit, it's returning error message string like curl: Couldn't resolve host name.

func main() {
        easy := curl.EasyInit()
    defer easy.Cleanup()

    easy.Setopt(curl.OPT_URL, "http://zzzzzzzz---.com")

    if err := easy.Perform(); err != nil {
        fmt.Println(err, err.Error())
    }
}

But, how to get the cURL error code like the libcurl equivalent returns.

int main(int argc, char *argv[]) {
    CURL *easyhandle = curl_easy_init();
    curl_easy_setopt(easyhandle, CURLOPT_URL, "http://zzzzzzzz---.com");
    int errorCode = curl_easy_perform(easyhandle);
    printf("Error Code: %d %d\n", errorCode, errorCode == CURLE_COULDNT_RESOLVE_HOST);
    return 0;
}
hwshadow commented 1 year ago

This is a bit hacky but you could do the following to extract the int errorCode from the curl error provided by this library.

errornum, convErr := strconv.Atoi(fmt.Sprintf("%d", err.(curl.CurlError) ))

Sorry this is so late, I just ran into this as well ^_^