Error handling
If the problem occurs on the EasyCurl side then EasyCurlError
exception will be thrown.
EasyCurl.EasyCurlError
— TypeEasyCurlError{code} <: Exception
Type that is returned if curl_request
fails on the libcurl side.
Fields
code::UInt32
: The error code (see libcurl error codes).message::String
: The error message.
Examples
julia> curl_request("GET", "http://httpbin.org/status/400", interface = "9.9.9.9")
ERROR: EasyCurlError{45}(Failed binding local connection end)
[...]
julia> curl_request("GET", "http://httpbin.org/status/400", interface = "")
ERROR: EasyCurlError{7}(Couldn't connect to server)
[...]
Or, if the problem was caused by HHTP, a EasyCurlStatusError
exception will be thrown.
EasyCurl.EasyCurlStatusError
— TypeEasyCurlStatusError{code} <: Exception
Type that is returned if curl_request
fails on the HTTP side.
Fields
code::Int64
: The HTTP error code (seeHTTP_STATUS_CODES
).message::String
: The error message.response::Response
: The HTTP response object.
Examples
julia> curl_request("GET", "http://httpbin.org/status/400", interface = "0.0.0.0")
ERROR: EasyCurlStatusError{400}(BAD_REQUEST)
[...]
julia> curl_request("GET", "http://httpbin.org/status/404", interface = "0.0.0.0")
ERROR: EasyCurlStatusError{404}(NOT_FOUND)
[...]
Below is a small example of error handling.
Example
using EasyCurl
headers = Pair{String,String}[
"User-Agent" => "EasyCurl.jl",
"Content-Type" => "application/json",
]
try
response = curl_request("GET", "http://httpbin.org/status/400", query = "echo=你好嗎",
headers = headers, interface = "0.0.0.0", read_timeout = 30, retries = 1)
# If the request is successful, you can process the response here
# ...
catch e
if isa(e, EasyCurlError{EasyCurl.CURLE_COULDNT_CONNECT})
# Handle the case where the connection to the server could not be made
elseif isa(e, EasyCurlError{EasyCurl.CURLE_OPERATION_TIMEDOUT})
# Handle the case where the operation timed out
elseif isa(e, EasyCurlStatusError{400})
# Handle a 400 Bad Request error specifically
rethrow(e)
end
end