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