Skip to content

Commit

Permalink
Lin: Print commands that trigger unexpected exceptions
Browse files Browse the repository at this point in the history
The standard QCheck mechanism to catch exceptions that are raised in
tests do not give much clues about the source (ie Lin command) of that
exception
So this creates a specific Unexpected_exception that will pack the
command along with the original exception so that it can be explained
when the tests errors out

Before:
```
exception Invalid_argument("Bytes.blit")
```

After:
```
exception Invalid_argument("Bytes.blit") unexpected in command Buffer.add_string t ""
```
  • Loading branch information
shym committed Apr 6, 2023
1 parent 158b943 commit 09fd357
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lib/lin.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
exception Unexpected_exception of string * exn

let print_unhandled_exception e =
match e with
| Unexpected_exception (cmd, exc) ->
Some (Format.sprintf "%s unexpected in command %s" (Printexc.to_string exc) cmd)
| _ -> None

let _ =
Printexc.register_printer print_unhandled_exception

module Internal =
struct
open QCheck
Expand Down Expand Up @@ -464,5 +475,6 @@ module MakeCmd (ApiSpec : Spec) : Internal.CmdSpec = struct

let run cmd state =
let Cmd { args ; rty ; f ; _ } = cmd in
Res (rty, apply_f f args state)
try Res (rty, apply_f f args state)
with e -> raise (Unexpected_exception (show_cmd cmd, e))
end

0 comments on commit 09fd357

Please sign in to comment.