Fork of daniellemaywood.uk/gleam — Wasm codegen work
3.8 kB
113 lines
1-module('{{ application }}@@main').
2-export([run/1]).
3
4-define(red, "\e[31;1m").
5-define(grey, "\e[90m").
6-define(reset_color, "\e[39m").
7-define(reset_all, "\e[0m").
8
9run(Module) ->
10 io:setopts(standard_io, [binary, {encoding, utf8}]),
11 io:setopts(standard_error, [{encoding, utf8}]),
12 process_flag(trap_exit, true),
13 Pid = spawn_link(fun() -> run_module(Module) end),
14 receive
15 {'EXIT', Pid, {Reason, [First|_] = StackTrace}} when is_tuple(First) ->
16 print_error_with_stacktrace(exit, Reason, StackTrace),
17 init:stop(1);
18 {'EXIT', Pid, Reason} when Reason =/= normal ->
19 print_error(exit, Reason),
20 init:stop(1)
21 end.
22
23run_module(Module) ->
24 try
25 {ok, _} = application:ensure_all_started('{{ application }}'),
26 erlang:process_flag(trap_exit, false),
27 Module:main(),
28 init:stop(0)
29 catch
30 Class:Reason:StackTrace ->
31 print_error_with_stacktrace(Class, Reason, StackTrace),
32 init:stop(1)
33 end.
34
35print_error_with_stacktrace(Class, Error, Stacktrace) ->
36 Printed = [
37 ?red, "runtime error", ?reset_color, ": ", error_class(Class, Error), ?reset_all,
38 "\n\n",
39 error_message(Error),
40 "\n\n",
41 error_details(Class, Error),
42 "stacktrace:\n",
43 [error_frame(Line) || Line <- refine_first(Error, Stacktrace)]
44 ],
45 io:format(standard_error, "~ts~n", [Printed]).
46
47print_error(Class, Error) ->
48 Printed = [
49 ?red, "runtime error", ?reset_color, ": ", error_class(Class, Error), ?reset_all,
50 "\n\n",
51 error_message(Error),
52 "\n\n",
53 "exit reason:\n ", print_term(Error), $\n
54 ],
55 io:format(standard_error, "~ts~n", [Printed]).
56
57refine_first(#{gleam_error := _, line := L}, [{M, F, A, [{file, Fi} | _]} | S]) ->
58 [{M, F, A, [{file, Fi}, {line, L}]} | S];
59refine_first(_, S) ->
60 S.
61
62error_class(_, #{gleam_error := panic}) -> "panic";
63error_class(_, #{gleam_error := todo}) -> "todo";
64error_class(_, #{gleam_error := let_assert}) -> "let assert";
65error_class(_, #{gleam_error := assert}) -> "assert";
66error_class(Class, _) -> ["Erlang ", atom_to_binary(Class)].
67
68error_message(#{gleam_error := _, message := M}) ->
69 M;
70error_message(undef) ->
71 <<"A function was called but it did not exist."/utf8 >>;
72error_message({case_clause, _}) ->
73 <<"No pattern matched in an Erlang case expression."/utf8>>;
74error_message({badmatch, _}) ->
75 <<"An Erlang assignment pattern did not match."/utf8>>;
76error_message(function_clause) ->
77 <<"No Erlang function clause matched the arguments it was called with."/utf8>>;
78error_message(_) ->
79 <<"An error occurred outside of Gleam."/utf8>>.
80
81error_details(_, #{gleam_error := let_assert, value := V}) ->
82 ["unmatched value:\n ", print_term(V), $\n, $\n];
83error_details(_, {case_clause, V}) ->
84 ["unmatched value:\n ", print_term(V), $\n, $\n];
85error_details(_, {badmatch, V}) ->
86 ["unmatched value:\n ", print_term(V), $\n, $\n];
87error_details(_, #{gleam_error := _}) ->
88 [];
89error_details(error, function_clause) ->
90 [];
91error_details(error, undef) ->
92 [];
93error_details(C, E) ->
94 ["erlang:", atom_to_binary(C), $(, print_term(E), $), $\n, $\n].
95
96print_term(T) ->
97 try
98 gleam@string:inspect(T)
99 catch
100 _:_ -> io_lib:format("~p", [T])
101 end.
102
103error_frame({?MODULE, _, _, _}) -> [];
104error_frame({erl_eval, _, _, _}) -> [];
105error_frame({init, _, _, _}) -> [];
106error_frame({M, F, _, O}) ->
107 M1 = string:replace(atom_to_binary(M), "@", "/", all),
108 [" ", M1, $., atom_to_binary(F), error_frame_end(O), $\n].
109
110error_frame_end([{file, Fi}, {line, L} | _]) ->
111 [?grey, $\s, Fi, $:, integer_to_binary(L), ?reset_all];
112error_frame_end(_) ->
113 [?grey, " unknown source", ?reset_all].