Error handling
If the problem occurs on the EasyCurl side then CurlError
exception will be thrown.
EasyCurl.CurlError
— TypeCurlError{code} <: Exception
Type wrapping LibCURL error codes. Returned when a libcurl error occurs.
Fields
code::UInt64
: The LibCURL error code (see libcurl error codes).message::String
: The error message.
Examples
julia> http_request("GET", "http://httpbin.org/status/400", interface = "9.9.9.9")
ERROR: CurlError{45}: Failed binding local connection end
[...]
julia> http_request("GET", "http://httpbin.org/status/400", interface = "")
ERROR: CurlError{7}: Couldn't connect to server
[...]
Or, if the problem was caused by HHTP, a HTTPStatusError
exception will be thrown.
EasyCurl.HTTPStatusError
— TypeHTTPStatusError{code} <: Exception
Type wrapping HTTP error codes. Returned from http_request
when an HTTP error occurs.
Fields
code::Int64
: The HTTP error code (seeHTTP_STATUS_CODES
).message::String
: The error message.response::HTTPResponse
: The HTTP response object.
Examples
julia> http_request("GET", "http://httpbin.org/status/400")
ERROR: HTTPStatusError{400}: BAD_REQUEST
[...]
julia> http_request("GET", "http://httpbin.org/status/404")
ERROR: HTTPStatusError{404}: NOT_FOUND
[...]
Below are some small examples of error handling.
Example
Classic way of error handling:
using EasyCurl
try
http_request("GET", "http://httpbin.org/status/400"; read_timeout = 30)
# If the request is successful, you can process the response here
# ...
catch e
if isa(e, CurlError)
if e.code == EasyCurl.CURLE_COULDNT_CONNECT
# Handle the case where the connection to the server could not be made
elseif e.code == EasyCurl.CURLE_OPERATION_TIMEDOUT
# Handle the case where the operation timed out
end
elseif isa(e, HTTPStatusError)
if e.code == 400
# Handle a 400 Bad Request error specifically
end
end
rethrow(e)
end
Handling errors using error codes as a type parameter:
using EasyCurl
try
http_request("GET", "http://httpbin.org/status/400"; read_timeout = 30)
# If the request is successful, you can process the response here
# ...
catch e
if isa(e, CurlError{EasyCurl.CURLE_COULDNT_CONNECT})
# Handle the case where the connection to the server could not be made
elseif isa(e, CurlError{EasyCurl.CURLE_OPERATION_TIMEDOUT})
# Handle the case where the operation timed out
elseif isa(e, HTTPStatusError{400})
# Handle a 400 Bad Request error specifically
end
rethrow(e)
end