this repo has no description

Add error formatting utilities to simplify error handling

Added string_of_error and pp_error functions to the Jmap.Api module
to provide a consistent, reusable way of formatting error messages.
This centralizes error formatting logic and reduces duplicated code
in client applications.

Updated the fastmail_list binary to use these functions, resulting
in cleaner, more maintainable code.

🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>

Changed files
+28 -33
bin
lib
+5 -33
bin/fastmail_list.ml
···
let* mailboxes_result = Jmap_mail.get_mailboxes conn ~account_id in
match mailboxes_result with
| Error err ->
-
Printf.printf "Error getting mailboxes: %s\n"
-
(match err with
-
| Jmap.Api.Connection_error msg -> "Connection error: " ^ msg
-
| Jmap.Api.HTTP_error (code, body) -> Printf.sprintf "HTTP error %d: %s" code body
-
| Jmap.Api.Parse_error msg -> "Parse error: " ^ msg
-
| Jmap.Api.Authentication_error -> "Authentication error");
Lwt.return_unit
| Ok mailboxes ->
···
match emails_result with
| Error err ->
-
Printf.printf "Error getting emails: %s\n"
-
(match err with
-
| Jmap.Api.Connection_error msg -> "Connection error: " ^ msg
-
| Jmap.Api.HTTP_error (code, body) -> Printf.sprintf "HTTP error %d: %s" code body
-
| Jmap.Api.Parse_error msg -> "Parse error: " ^ msg
-
| Jmap.Api.Authentication_error -> "Authentication error");
Lwt.return_unit
| Ok emails ->
···
in
match result with
| Error err ->
-
(match err with
-
| Api.Connection_error msg ->
-
Printf.eprintf "Connection error: %s\n" msg
-
| Api.HTTP_error (code, body) ->
-
Printf.eprintf "HTTP error %d: %s\n" code body
-
| Api.Parse_error msg ->
-
Printf.eprintf "Parse error: %s\n" msg
-
| Api.Authentication_error ->
-
Printf.eprintf "Authentication error. Check your API token.\n");
Lwt.return 1
| Ok conn ->
(* Get the primary account ID *)
···
let* mailboxes_result = get_mailboxes conn ~account_id:primary_account_id in
match mailboxes_result with
| Error err ->
-
Printf.eprintf "Failed to get mailboxes: %s\n"
-
(match err with
-
| Api.Connection_error msg -> "Connection error: " ^ msg
-
| Api.HTTP_error (code, body) -> Printf.sprintf "HTTP error %d: %s" code body
-
| Api.Parse_error msg -> "Parse error: " ^ msg
-
| Api.Authentication_error -> "Authentication error");
Lwt.return 1
| Ok mailboxes ->
(* If there's a mailbox list, just use the first one for this example *)
···
in
match emails_result with
| Error err ->
-
Printf.eprintf "Failed to get emails: %s\n"
-
(match err with
-
| Api.Connection_error msg -> "Connection error: " ^ msg
-
| Api.HTTP_error (code, body) -> Printf.sprintf "HTTP error %d: %s" code body
-
| Api.Parse_error msg -> "Parse error: " ^ msg
-
| Api.Authentication_error -> "Authentication error");
Lwt.return 1
| Ok emails ->
(* Apply filters based on command line arguments *)
···
let* mailboxes_result = Jmap_mail.get_mailboxes conn ~account_id in
match mailboxes_result with
| Error err ->
+
Printf.printf "Error getting mailboxes: %s\n" (Api.string_of_error err);
Lwt.return_unit
| Ok mailboxes ->
···
match emails_result with
| Error err ->
+
Printf.printf "Error getting emails: %s\n" (Api.string_of_error err);
Lwt.return_unit
| Ok emails ->
···
in
match result with
| Error err ->
+
Printf.eprintf "%s\n" (Api.string_of_error err);
Lwt.return 1
| Ok conn ->
(* Get the primary account ID *)
···
let* mailboxes_result = get_mailboxes conn ~account_id:primary_account_id in
match mailboxes_result with
| Error err ->
+
Printf.eprintf "Failed to get mailboxes: %s\n" (Api.string_of_error err);
Lwt.return 1
| Ok mailboxes ->
(* If there's a mailbox list, just use the first one for this example *)
···
in
match emails_result with
| Error err ->
+
Printf.eprintf "Failed to get emails: %s\n" (Api.string_of_error err);
Lwt.return 1
| Ok emails ->
(* Apply filters based on command line arguments *)
+11
lib/jmap.ml
···
(** Result type for API operations *)
type 'a result = ('a, error) Stdlib.result
(** Configuration for a JMAP API client *)
type config = {
···
(** Result type for API operations *)
type 'a result = ('a, error) Stdlib.result
+
+
(** Convert an error to a human-readable string *)
+
let string_of_error = function
+
| Connection_error msg -> "Connection error: " ^ msg
+
| HTTP_error (code, body) -> Printf.sprintf "HTTP error %d: %s" code body
+
| Parse_error msg -> "Parse error: " ^ msg
+
| Authentication_error -> "Authentication error"
+
+
(** Pretty-print an error to a formatter *)
+
let pp_error ppf err =
+
Format.fprintf ppf "%s" (string_of_error err)
(** Configuration for a JMAP API client *)
type config = {
+12
lib/jmap.mli
···
(** Result type for API operations *)
type 'a result = ('a, error) Stdlib.result
(** Configuration for a JMAP API client as defined in RFC8620 Section 3.1
@see <https://datatracker.ietf.org/doc/html/rfc8620#section-3.1>
*)
···
(** Result type for API operations *)
type 'a result = ('a, error) Stdlib.result
+
(** Convert an error to a human-readable string
+
@param err The error to convert
+
@return A string representation of the error
+
*)
+
val string_of_error : error -> string
+
+
(** Pretty-print an error to a formatter
+
@param ppf The formatter to print to
+
@param err The error to print
+
*)
+
val pp_error : Format.formatter -> error -> unit
+
(** Configuration for a JMAP API client as defined in RFC8620 Section 3.1
@see <https://datatracker.ietf.org/doc/html/rfc8620#section-3.1>
*)