Error handling
If the problem occurs on the EasyCurl side then AbstractCurlError
exception will be thrown.
EasyCurl.AbstractCurlError
— TypeAbstractCurlError <: Exception
Abstract base type for exceptions related to LibCURL errors.
Concrete subtypes:
CurlEasyError
: Errors from the libcurl easy interface.CurlMultiError
: Errors from the libcurl multi interface.
All subtypes provide:
code::Int
: Numeric libcurl error code.message::String
: Human-readable libcurl error message.
See libcurl error codes for more details.
EasyCurl.CurlEasyError
— TypeCurlEasyError <: AbstractCurlError
Represents an error from a libcurl easy interface call.
Fields
code::Int
: The libcurl error code.message::String
: The corresponding error message from libcurl.
Examples
julia> curl_easy_setopt(c, 1, 1)
ERROR: CurlEasyError{48}: An unknown option was passed in to libcurl
EasyCurl.CurlMultiError
— TypeCurlMultiError <: AbstractCurlError
Represents an error from a libcurl multi interface call.
Fields
code::Int
: The libcurl multi error code.message::String
: The corresponding error message from libcurl.
Examples
julia> curl_multi_add_handle(c)
ERROR: CurlMultiError{1}: Invalid multi handle
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, CurlEasyError)
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, CurlEasyError{EasyCurl.CURLE_COULDNT_CONNECT})
# Handle the case where the connection to the server could not be made
elseif isa(e, CurlEasyError{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