aboutsummaryrefslogtreecommitdiff
path: root/src/panic_handler.zig
blob: ab8d1cf812e61020edc46564f666f89440f00148 (plain) (blame)
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
const std = @import("std");
const logger = @import("logger.zig");
const root = @import("root");
usingnamespace @import("global.zig");

pub fn NewPanicHandler(comptime panic_func: fn handle_panic(msg: []const u8, error_return_type: ?*std.builtin.StackTrace) noreturn) type {
    return struct {
        panic_count: usize = 0,
        skip_next_panic: bool = false,
        log: *logger.Log,

        pub var Singleton: ?*Handler = null;
        const Handler = @This();

        pub fn init(log: *logger.Log) Handler {
            return Handler{
                .log = log,
            };
        }
        pub inline fn handle_panic(msg: []const u8, error_return_type: ?*std.builtin.StackTrace) noreturn {
            // This exists to ensure we flush all buffered output before panicking.
            Output.flush();

            if (msg.len > 0) {
                if (Output.isEmojiEnabled()) {
                    Output.prettyErrorln("<r><red>Bun crashed 😭😭😭<r><d>: <r><b>{s}<r>\n", .{msg});
                } else {
                    Output.prettyErrorln("<r><red>Crash<r><d>:<r> <b>{s}<r>", .{msg});
                }
                Output.flush();
            } else {
                if (Output.isEmojiEnabled()) {
                    Output.prettyErrorln("<r><red>Bun will crash now<r> 😭😭😭<r>\n", .{});
                    Output.flush();
                } else {
                    Output.printError("Bun has crashed :'(\n", .{});
                }
                Output.flush();
            }
            Output.disableBuffering();

            // // We want to always inline the panic handler so it doesn't show up in the stacktrace.
            @call(.{ .modifier = .always_inline }, panic_func, .{ msg, error_return_type });
        }
    };
}