Skip to content

Commit

Permalink
feat: add Modifier.S.register_printer
Browse files Browse the repository at this point in the history
  • Loading branch information
favonia committed Sep 21, 2023
1 parent 975804c commit 9ea5777
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/Modifier.ml
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,11 @@ struct
Effect.Deep.try_with f () @@ handler ~not_found ~shadow ~hook

let try_with ?(not_found=Perform.not_found) ?(shadow=Perform.shadow) ?(hook=Perform.hook) f = run ~not_found ~shadow ~hook f

let register_printer f =
Printexc.register_printer @@ function
| Effect.Unhandled (NotFound {context; prefix}) -> f (`NotFound (context, prefix))
| Effect.Unhandled (Shadow {context; path; former; latter}) -> f (`Shadow (context, path, former, latter))
| Effect.Unhandled (Hook {context; prefix; hook; input}) -> f (`Hook (context, prefix, hook, input))
| _ -> None
end
6 changes: 5 additions & 1 deletion src/ModifierSigs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ sig
try_with ~shadow @@ fun () ->
(* even more code *)
]}
*)

val register_printer : ([ `NotFound of context option * Trie.bwd_path | `Shadow of context option * Trie.bwd_path * (data * tag) * (data * tag) | `Hook of context option * Trie.bwd_path * hook * (data, tag) Trie.t ] -> string option) -> unit
(** [register_printer f] registers a printer [f] via {!val:Printexc.register_printer} to turn unhandled effects into strings for the runtime system to display the error of unhandled effects. The functor {!module:Modifier.Make} will register a simple printer to suggest using {!val:run} to capture effects, but you can register new printers to override it. The registered printers are executed in reverse order; that is, the last registered printer is executed first. The return type of the printer [f] is [string option], where [None] means the printer passes on the (unhandled) effect and lets the runtime system try the next printer or (if no more printers are available) use the default system message.
The input type of [f] is a variant representation of all internal effects used in this module. They correspond to the three effect triggers in {!module:Perform}. More precisely, [`NotFound (ctx, path)] corresponds to the effect triggered by [Perform.not_found ctx path], [`Shadow (ctx, path, x, y)] corresponds to [Perform.shadow ctx path x y], and then [`Hook (ctx, path, hook, t)] corresponds to [Perform.hook ctx path hook t]. *)
end

0 comments on commit 9ea5777

Please sign in to comment.