From e79ed4f738693b860fd99d0abd26f641a2b19461 Mon Sep 17 00:00:00 2001 From: Alexander Kuznetsov Date: Mon, 18 Jul 2022 00:24:35 +0400 Subject: fix printing message for thrown non-error objects (#764) * fix printing message for thrown non-error objects * fine tuning error formatting * more fine tuning error formatting --- src/bun.js/javascript.zig | 97 ++++++++++++++++++++--------------------------- 1 file changed, 41 insertions(+), 56 deletions(-) (limited to 'src') diff --git a/src/bun.js/javascript.zig b/src/bun.js/javascript.zig index e084f01b2..7c122fe8b 100644 --- a/src/bun.js/javascript.zig +++ b/src/bun.js/javascript.zig @@ -965,7 +965,7 @@ pub const VirtualMachine = struct { .specifier = ZigString.init("node:http"), .source_url = ZigString.init("node:http"), .hash = 0, - }; + }; }, .@"depd" => { return ResolvedSource{ @@ -1958,20 +1958,17 @@ pub const VirtualMachine = struct { const int_size = std.fmt.count("{d}", .{source.line}); const pad = max_line_number_pad - int_size; last_pad = pad; - writer.writeByteNTimes(' ', pad) catch unreachable; - writer.print( + try writer.writeByteNTimes(' ', pad); + try writer.print( comptime Output.prettyFmt("{d} | {s}\n", allow_ansi_color), .{ source.line, std.mem.trim(u8, source.text, "\n"), }, - ) catch unreachable; + ); } var name = exception.name; - if (strings.eqlComptime(exception.name.slice(), "Error")) { - name = ZigString.init("error"); - } const message = exception.message; var did_print_name = false; @@ -1980,7 +1977,7 @@ pub const VirtualMachine = struct { defer did_print_name = true; var text = std.mem.trim(u8, source.text, "\n"); - writer.print( + try writer.print( comptime Output.prettyFmt( "- | {s}\n", allow_ansi_color, @@ -1988,33 +1985,24 @@ pub const VirtualMachine = struct { .{ text, }, - ) catch unreachable; - - if (name.len > 0 and message.len > 0) { - writer.print(comptime Output.prettyFmt(" {}: {}\n", allow_ansi_color), .{ - name, - message, - }) catch unreachable; - } else if (name.len > 0) { - writer.print(comptime Output.prettyFmt(" {}\n", allow_ansi_color), .{name}) catch unreachable; - } else if (message.len > 0) { - writer.print(comptime Output.prettyFmt(" {}\n", allow_ansi_color), .{message}) catch unreachable; - } + ); + + try this.printErrorNameAndMessage(name, message, Writer, writer, allow_ansi_color); } else if (source.text.len > 0) { defer did_print_name = true; const int_size = std.fmt.count("{d}", .{source.line}); const pad = max_line_number_pad - int_size; - writer.writeByteNTimes(' ', pad) catch unreachable; + try writer.writeByteNTimes(' ', pad); const top = exception.stack.frames()[0]; var remainder = std.mem.trim(u8, source.text, "\n"); - writer.print( + try writer.print( comptime Output.prettyFmt( "{d} | {s}\n", allow_ansi_color, ), .{ source.line, remainder }, - ) catch unreachable; + ); if (!top.position.isInvalid()) { var first_non_whitespace = @intCast(u32, top.position.column_start); @@ -2023,37 +2011,19 @@ pub const VirtualMachine = struct { } const indent = @intCast(usize, pad) + " | ".len + first_non_whitespace; - writer.writeByteNTimes(' ', indent) catch unreachable; - writer.print(comptime Output.prettyFmt( + try writer.writeByteNTimes(' ', indent); + try writer.print(comptime Output.prettyFmt( "^\n", allow_ansi_color, - ), .{}) catch unreachable; + ), .{}); } - if (name.len > 0 and message.len > 0) { - writer.print(comptime Output.prettyFmt(" {s}: {s}\n", allow_ansi_color), .{ - name, - message, - }) catch unreachable; - } else if (name.len > 0) { - writer.print(comptime Output.prettyFmt(" {s}\n", allow_ansi_color), .{name}) catch unreachable; - } else if (message.len > 0) { - writer.print(comptime Output.prettyFmt(" {s}\n", allow_ansi_color), .{message}) catch unreachable; - } + try this.printErrorNameAndMessage(name, message, Writer, writer, allow_ansi_color); } } if (!did_print_name) { - if (name.len > 0 and message.len > 0) { - writer.print(comptime Output.prettyFmt("{s}: {s}\n", true), .{ - name, - message, - }) catch unreachable; - } else if (name.len > 0) { - writer.print(comptime Output.prettyFmt("{s}\n", true), .{name}) catch unreachable; - } else if (message.len > 0) { - writer.print(comptime Output.prettyFmt("{s}\n", true), .{name}) catch unreachable; - } + try this.printErrorNameAndMessage(name, message, Writer, writer, allow_ansi_color); } var add_extra_line = false; @@ -2074,40 +2044,55 @@ pub const VirtualMachine = struct { if (show.path) { if (show.syscall) { - writer.writeAll(" ") catch unreachable; + try writer.writeAll(" "); } else if (show.errno) { - writer.writeAll(" ") catch unreachable; + try writer.writeAll(" "); } - writer.print(comptime Output.prettyFmt(" path: \"{s}\"\n", allow_ansi_color), .{exception.path}) catch unreachable; + try writer.print(comptime Output.prettyFmt(" path: \"{s}\"\n", allow_ansi_color), .{exception.path}); } if (show.system_code) { if (show.syscall) { - writer.writeAll(" ") catch unreachable; + try writer.writeAll(" "); } else if (show.errno) { - writer.writeAll(" ") catch unreachable; + try writer.writeAll(" "); } - writer.print(comptime Output.prettyFmt(" code: \"{s}\"\n", allow_ansi_color), .{exception.system_code}) catch unreachable; + try writer.print(comptime Output.prettyFmt(" code: \"{s}\"\n", allow_ansi_color), .{exception.system_code}); add_extra_line = true; } if (show.syscall) { - writer.print(comptime Output.prettyFmt("syscall: \"{s}\"\n", allow_ansi_color), .{exception.syscall}) catch unreachable; + try writer.print(comptime Output.prettyFmt("syscall: \"{s}\"\n", allow_ansi_color), .{exception.syscall}); add_extra_line = true; } if (show.errno) { if (show.syscall) { - writer.writeAll(" ") catch unreachable; + try writer.writeAll(" "); } - writer.print(comptime Output.prettyFmt("errno: {d}\n", allow_ansi_color), .{exception.errno}) catch unreachable; + try writer.print(comptime Output.prettyFmt("errno: {d}\n", allow_ansi_color), .{exception.errno}); add_extra_line = true; } - if (add_extra_line) writer.writeAll("\n") catch unreachable; + if (add_extra_line) try writer.writeAll("\n"); try printStackTrace(@TypeOf(writer), writer, exception.stack, allow_ansi_color); } + + fn printErrorNameAndMessage(_: *VirtualMachine, name: ZigString, message: ZigString, comptime Writer: type, writer: Writer, comptime allow_ansi_color: bool) !void { + if (name.len > 0 and message.len > 0) { + try writer.print(comptime Output.prettyFmt("{s}: {s}\n", allow_ansi_color), .{ + name, + message, + }); + } else if (name.len > 0) { + try writer.print(comptime Output.prettyFmt("{s}\n", allow_ansi_color), .{name}); + } else if (message.len > 0) { + try writer.print(comptime Output.prettyFmt("error: {s}\n", allow_ansi_color), .{message}); + } else { + try writer.print(comptime Output.prettyFmt("error\n", allow_ansi_color), .{}); + } + } }; const GetterFn = fn ( -- cgit v1.2.3