From e239f802b1d61c0d8ebe7a2c1fdcf3e451ffd62b Mon Sep 17 00:00:00 2001 From: swrup Date: Sun, 7 Dec 2025 06:39:33 +0100 Subject: [PATCH] wip better errors --- src/crypto.ml | 18 +++++++++++++----- src/pg.ml | 2 +- src/util.ml | 2 ++ 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/crypto.ml b/src/crypto.ml index 7f56a119..fbdf996c 100644 --- a/src/crypto.ml +++ b/src/crypto.ml @@ -10,15 +10,23 @@ module EddsaPublicKey = struct type t = pub let to_octets t = pub_to_octets t - let of_octets t = pub_of_octets t |> Result.get_ok - let bin = Bin.map (Bin.bytes 32) of_octets to_octets + + let of_octets t = + pub_of_octets t |> function + | Error e -> Fmt.error "%a" Mirage_crypto_ec.pp_error e + | Ok v -> Ok v + + let bin = + let of_octets o = + match of_octets o with Error e -> raise (Util.Bin_error e) | Ok v -> v + in + Bin.map (Bin.bytes 32) of_octets to_octets let of_b32 s = let open Syntax in let* octets = B32.decode s in - match pub_of_octets octets with - | Error e -> Fmt.error "%a" Mirage_crypto_ec.pp_error e - | Ok pub -> Ok pub + let* pub = of_octets octets in + Ok pub let to_b32 t = B32.encode (to_octets t) let jsont = Jsont.of_of_string ~kind:"EddsaPublicKey" of_b32 ~enc:to_b32 diff --git a/src/pg.ml b/src/pg.ml index dd45b369..6be18f8e 100644 --- a/src/pg.ml +++ b/src/pg.ml @@ -38,7 +38,7 @@ module Caqti_type = struct let open EddsaPublicKey in custom ~encode:(fun v -> Ok (to_octets v)) - ~decode:(fun v -> Ok (of_octets v)) + ~decode:(fun v -> of_octets v) octets let eddsa_signature : EddsaSignature.t t = diff --git a/src/util.ml b/src/util.ml index 31be5ef4..5d6bac8e 100644 --- a/src/util.ml +++ b/src/util.ml @@ -1,3 +1,5 @@ +exception Bin_error of string + (* TODO bin - no [Bin.of_string] ? *) let bin_of_string bin s =