Fork of daniellemaywood.uk/gleam — Wasm codegen work
1.9 kB
59 lines
1#![allow(clippy::unwrap_used)]
2use std::panic::PanicHookInfo;
3
4pub fn add_handler() {
5 std::panic::set_hook(Box::new(move |info: &PanicHookInfo<'_>| {
6 print_compiler_bug_message(info)
7 }));
8}
9
10fn print_compiler_bug_message(info: &PanicHookInfo<'_>) {
11 let message = match (
12 info.payload().downcast_ref::<&str>(),
13 info.payload().downcast_ref::<String>(),
14 ) {
15 (Some(s), _) => (*s).to_string(),
16 (_, Some(s)) => s.to_string(),
17 (None, None) => "unknown error".into(),
18 };
19 let location = match info.location() {
20 None => "".into(),
21 Some(location) => format!("{}:{}\n\t", location.file(), location.line()),
22 };
23
24 let buffer_writer = crate::cli::stderr_buffer_writer();
25 let mut buffer = buffer_writer.buffer();
26 use std::io::Write;
27 use termcolor::{Color, ColorSpec, WriteColor};
28 buffer
29 .set_color(ColorSpec::new().set_bold(true).set_fg(Some(Color::Red)))
30 .unwrap();
31 write!(buffer, "error").unwrap();
32 buffer.set_color(ColorSpec::new().set_bold(true)).unwrap();
33 write!(buffer, ": Fatal compiler bug!\n\n").unwrap();
34 buffer.set_color(&ColorSpec::new()).unwrap();
35 writeln!(
36 buffer,
37 "This is a bug in the Gleam compiler, sorry!
38
39Please report this crash to https://github.com/gleam-lang/gleam/issues/new
40and include this error message with your report.
41
42Panic: {location}{message}
43Gleam version: {version}
44Operating system: {os}
45
46If you can also share your code and say what file you were editing or any
47steps to reproduce the crash that would be a great help.
48
49You may also want to try again with the `GLEAM_LOG=trace` environment
50variable set.
51",
52 location = location,
53 message = message,
54 version = env!("CARGO_PKG_VERSION"),
55 os = std::env::consts::OS,
56 )
57 .unwrap();
58 buffer_writer.print(&buffer).unwrap();
59}