Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature wish: custom exception printers #4477

Closed
vicuna opened this Issue Jan 11, 2008 · 2 comments

Comments

Projects
None yet
1 participant
@vicuna
Copy link
Collaborator

commented Jan 11, 2008

Original bug ID: 4477
Reporter: gerd
Assigned to: @xclerc
Status: closed (set by @xavierleroy on 2011-05-29T10:20:25Z)
Resolution: fixed
Priority: normal
Severity: feature
Version: 3.10.0
Fixed in version: 3.11.2+dev
Category: ~DO NOT USE (was: OCaml general)
Duplicate of: #2464
Monitored by: till @alainfrisch

Bug description

While we are at exceptions. It would be great if it were possible to define custom exception printers for the cases Printexc cannot handle automatically.

val register_printer : string -> (exn -> string) -> unit

This function is called with the name of the exception and the printing function. It is ensured by the runtime that the function is only called with the right exceptions, so a typical definition could be:

register_printer
"Foo.Foo"
(function
| Foo.Foo(arg1,arg2) ->
"Foo.Foo(" ^ arg1_to_string arg1 ^ "," ^ arg2_to_string arg2 ^ ")"
| _ ->
assert false
)

It is allowed to recursively call Printexc.to_string from within the printer.

I'm quite sure somebody will then come up with a camlp4 extension generating these definitions.

Additional information

The motivation for this enhancement is that we are currently developing server components, and exceptions simply happen as part of normal operations. Usually they are handled by printing them to a log file, and then terminating the current action.

Currently, we work around by defining our own custom printers as normal O'Caml functions. But these work only for a specific context, e.g. we have Hydro_util.exn_to_string for all exceptions coming from Hydro (Wink's ICE implementation), and Pxp_types.string_of_exn for all exceptions coming from PXP, and a few other. Mixing these printers so they work for all relevant contexts is cumbersome.

Furthermore, we have the problem in server platforms like Hydro that we need to print exceptions that come from anywhere, e.g. because they are caught from an invocation of a user function. One can again work around by allowing to plug in custom printers, but this feels also a bit strange.

My thinking is that the O'Caml runtime is the right place for aggregating these functions.

@vicuna

This comment has been minimized.

Copy link
Collaborator Author

commented Aug 31, 2008

Comment author: gerd

I've implemented this idea in ocamlnet, module Netexn:
https://godirepo.camlcity.org/svn/lib-ocamlnet2/trunk/code/src/netsys/netexn.mli
https://godirepo.camlcity.org/svn/lib-ocamlnet2/trunk/code/src/netsys/netexn.ml

The registry uses the name of the exception as key to a hash table (as an accelerator for lookups), and the reference to the exception definition block in an assoc list within that table. This block can be moved around by the GC, and cannot serve directly as hash key.

It is possible to define recursive printers for exception like

exception E of exn

but no provisions are taken for detecting cycles in the value graph.

@vicuna

This comment has been minimized.

Copy link
Collaborator Author

commented Sep 16, 2009

Comment author: @xclerc

support for custom exception printers has been committed in the 'release311' branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.