ErrorCentralized error handling for the Requests library using Eio.Io exceptions.
This module follows the Eio.Io exception pattern for structured error handling, providing granular error types and query functions for smart retry logic.
Errors are raised using the Eio.Io pattern:
raise (Error.err (Error.Timeout { operation = "connect"; duration = Some 30.0 }))To catch and handle errors:
try
(* ... HTTP request ... *)
with
| Eio.Io (Error.E e, _) when Error.is_retryable e ->
(* Retry the request *)
| Eio.Io (Error.E e, _) ->
Printf.eprintf "Request failed: %s\n" (Error.to_string e)val src : Logs.Src.tLog source for error reporting
Granular error variants with contextual information. Each variant contains a record with relevant details.
type error = | Timeout of {}| Too_many_redirects of {}| Invalid_redirect of {}| Http_error of {url : string;status : int;reason : string;body_preview : string option;headers : (string * string) list;}| Authentication_failed of {}| Dns_resolution_failed of {}| Tcp_connect_failed of {}| Tls_handshake_failed of {}| Invalid_header of {}| Body_too_large of {}| Headers_too_large of {}| Decompression_bomb of {}| Content_length_mismatch of {}| Insecure_auth of {}Per RFC 7617 Section 4 and RFC 6750 Section 5.1: Basic, Bearer, and Digest authentication over unencrypted HTTP exposes credentials to eavesdropping. Raised when attempting to use these auth methods over HTTP without explicit opt-out.
*)| Json_parse_error of {}| Json_encode_error of {}| Proxy_error of {}| Encoding_error of {}| Invalid_url of {}| Invalid_request of {}| Oauth_error of {}| Token_refresh_failed of {}Token refresh operation failed.
*)| Token_expiredAccess token has expired and no refresh token is available.
*)| H2_protocol_error of {}HTTP/2 connection error per RFC 9113 Section 5.4.1. Error codes are defined in RFC 9113 Section 7.
*)| H2_stream_error of {}| H2_flow_control_error of {}| H2_compression_error of {}| H2_settings_timeout| H2_goaway of {}| H2_frame_error of {}Invalid frame received per RFC 9113 Section 4-6.
*)| H2_header_validation_error of {}HTTP/2 header validation failed per RFC 9113 Section 8.2-8.3.
*)Extension of Eio.Exn.err for Requests errors
val err : error -> exnCreate an Eio exception from an error. Usage: raise (err (Timeout { operation = "read"; duration = Some 5.0 }))
Redact sensitive headers (Authorization, Cookie, etc.) for safe logging. Takes and returns a list of (name, value) pairs.
val pp_error : Format.formatter -> error -> unitPretty printer for error values
These functions enable smart error handling without pattern matching.
val is_timeout : error -> boolReturns true if the error is a timeout
val is_dns : error -> boolReturns true if the error is a DNS resolution failure
val is_tls : error -> boolReturns true if the error is a TLS handshake failure
val is_connection : error -> boolReturns true if the error is any connection-related failure (DNS, TCP connect, or TLS handshake)
val is_http_error : error -> boolReturns true if the error is an HTTP response error
val is_client_error : error -> boolReturns true if the error is a client error (4xx status or similar)
val is_server_error : error -> boolReturns true if the error is a server error (5xx status)
val is_retryable : error -> boolReturns true if the error is typically retryable. Retryable errors include: timeouts, connection errors, and certain HTTP status codes (408, 429, 500, 502, 503, 504)
val is_security_error : error -> boolReturns true if the error is security-related (header injection, body too large, decompression bomb, etc.)
val is_json_error : error -> boolReturns true if the error is a JSON parsing or encoding error
val is_oauth_error : error -> boolReturns true if the error is an OAuth-related error (Oauth_error, Token_refresh_failed, Token_expired)
val of_eio_exn : exn -> error optionExtract error from an Eio.Io exception, if it's a Requests error
val get_http_status : error -> int optionGet the HTTP status code from an error, if applicable
val get_url : error -> string optionGet the URL associated with an error, if applicable
val to_string : error -> stringConvert error to human-readable string
These functions provide a more concise way to create error exceptions compared to the verbose err (Error_type { field = value; ... }) pattern.
Example:
(* Instead of: *)
raise (err (Invalid_request { reason = "missing host" }))
(* Use: *)
raise (invalid_request ~reason:"missing host")Create a Tls_handshake_failed exception
Create a Tcp_connect_failed exception
These functions accept printf-style format strings for the reason field, making error construction more concise when messages need interpolation.
Example:
(* Instead of: *)
raise (Error.err (Error.Invalid_request {
reason = Printf.sprintf "Invalid status code: %s" code_str
}))
(* Use: *)
raise (Error.invalid_requestf "Invalid status code: %s" code_str)val invalid_requestf : ('a, unit, string, exn) format4 -> 'aCreate an Invalid_request exception with a format string
val invalid_redirectf : url:string -> ('a, unit, string, exn) format4 -> 'aCreate an Invalid_redirect exception with a format string
val invalid_urlf : url:string -> ('a, unit, string, exn) format4 -> 'aCreate an Invalid_url exception with a format string
val proxy_errorf : host:string -> ('a, unit, string, exn) format4 -> 'aCreate a Proxy_error exception with a format string
val tls_handshake_failedf :
host:string ->
('a, unit, string, exn) format4 ->
'aCreate a Tls_handshake_failed exception with a format string
val tcp_connect_failedf :
host:string ->
port:int ->
('a, unit, string, exn) format4 ->
'aCreate a Tcp_connect_failed exception with a format string
Create an Oauth_error exception
Query functions for HTTP/2 specific errors per RFC 9113.
val is_h2_error : error -> boolReturns true if the error is any HTTP/2 protocol error
val is_h2_connection_error : error -> boolReturns true if the error is an HTTP/2 connection-level error. Connection errors terminate the entire HTTP/2 connection.
val is_h2_stream_error : error -> boolReturns true if the error is an HTTP/2 stream-level error. Stream errors only affect a single stream.
val is_h2_retryable : error -> boolReturns true if the HTTP/2 error is typically retryable. Retryable errors include:
val get_h2_error_code : error -> int32 optionGet the HTTP/2 error code from an error, if applicable. Error codes are defined in RFC 9113 Section 7.
val get_h2_stream_id : error -> int32 optionGet the stream ID associated with an HTTP/2 error, if applicable.
Convenience constructors for HTTP/2 errors per RFC 9113 Section 7.
Create an H2_stream_error exception
Create an H2_flow_control_error exception. If stream_id is provided, it's a stream-level error; otherwise, it's a connection-level error.
Create an H2_goaway exception
Create an H2_header_validation_error exception
h2_error_code_name code returns the name of an HTTP/2 error code. Per RFC 9113 Section 7: