summaryrefslogtreecommitdiff
path: root/exn_handler.fun
blob: c0a2d7a3eca207735671ae36383aaabaa0ddff76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
structure ExnHandler: EXN_HANDLER = struct

  val eprintf = fn z => printf `"error: " z

  fun otherExn e =
  let
    val hist = MLton.Exn.history e
  in
    eprintf `"exception " `(exnMessage e) `" was raised\n";
    if hist = [] then
      printf
        `"No stack trace is avaliable\n"
        `"Recompile with -const \"Exn.keepHistory true\"\n" %
    else
      List.app (fn x => printf `"\t" `x `"\n" %) hist
  end

  fun ioExn (IO.Io { name, function = _, cause }) =
  let
    val reason =
      case cause of
        OS.SysErr (str, _) => str
      | _ => exnMessage cause
  in
    eprintf `name `": " `reason `"\n" %
  end
    | ioExn _ = die 126 `"ioExn: unreachable\n" %

  fun handler e = (
    printf `"\n";
    case e of
      IO.Io _ => ioExn e
    | _ => otherExn e;
    exit 1
  ) handle _ => sysExit 127
end