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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
open Ast

(*
--# Get error code
--#
--# Returns the error code as a string (e.g., "TypeError", "ValueError").
--#
--# @name error_code
--# @param x :: Error The error value to inspect.
--# @return :: String The error code as a string.
--# @family base
--# @export
*)
let register env =
  let env = Env.add "error_code"
    (make_builtin ~name:"error_code" 1 (fun args _env ->
      match args with
      | [VError { code; _ }] -> VString (Utils.error_code_to_string code)
      | [_] -> Error.type_error "Function `error_code` expects an Error value."
      | _ -> Error.arity_error_named "error_code" 1 (List.length args)
    ))
    env in
  
  (*
  --# Get error message
  --#
  --# Returns the human-readable message associated with an error.
  --#
  --# @name error_message
  --# @param x :: Error The error value to inspect.
  --# @return :: String The error message.
  --# @family base
  --# @export
  *)
  let env = Env.add "error_message"
    (make_builtin ~name:"error_message" 1 (fun args _env ->
      match args with
      | [VError { message; _ }] -> VString message
      | [_] -> Error.type_error "Function `error_message` expects an Error value."
      | _ -> Error.arity_error_named "error_message" 1 (List.length args)
    ))
    env in
  
  (*
  --# Get error context
  --#
  --# Returns a dictionary containing contextual information about where and why
  --# the error occurred.
  --#
  --# @name error_context
  --# @param x :: Error The error value to inspect.
  --# @return :: Dict A dictionary of related context data.
  --# @family base
  --# @export
  *)
  let env = Env.add "error_context"
    (make_builtin ~name:"error_context" 1 (fun args _env ->
      match args with
      | [VError { context; _ }] ->
          VDict context
      | [_] -> Error.type_error "Function `error_context` expects an Error value."
      | _ -> Error.arity_error_named "error_context" 1 (List.length args)
    ))
    env in
  env