Skip to content

Commit

Permalink
zig: make throwError use JSError (#15267)
Browse files Browse the repository at this point in the history
  • Loading branch information
nektro authored and snoglobe committed Nov 21, 2024
1 parent 79c154a commit 6c46fbd
Show file tree
Hide file tree
Showing 15 changed files with 53 additions and 114 deletions.
3 changes: 1 addition & 2 deletions src/bake/FrameworkRouter.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1018,8 +1018,7 @@ pub const JSFrameworkRouter = struct {
&global.bunVM().bundler.resolver,
InsertionContext.wrap(JSFrameworkRouter, jsfr),
) catch |err| {
global.throwError(err, "while scanning route list");
return error.JSError;
return global.throwError(err, "while scanning route list");
};

return jsfr;
Expand Down
3 changes: 1 addition & 2 deletions src/bake/bake.zig
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ pub const UserOptions = struct {
return error.JSError;
},
else => {
global.throwError(err, "while querying current working directory");
return error.JSError;
return global.throwError(err, "while querying current working directory");
},
};

Expand Down
21 changes: 7 additions & 14 deletions src/bun.js/api/BunObject.zig
Original file line number Diff line number Diff line change
Expand Up @@ -333,13 +333,11 @@ pub fn braces(
defer arena.deinit();

var lexer_output = Braces.Lexer.tokenize(arena.allocator(), brace_slice.slice()) catch |err| {
globalThis.throwError(err, "failed to tokenize braces");
return .undefined;
return globalThis.throwError(err, "failed to tokenize braces");
};

const expansion_count = Braces.calculateExpandedAmount(lexer_output.tokens.items[0..]) catch |err| {
globalThis.throwError(err, "failed to calculate brace expansion amount");
return .undefined;
return globalThis.throwError(err, "failed to calculate brace expansion amount");
};

if (tokenize) {
Expand All @@ -354,8 +352,7 @@ pub fn braces(
if (parse) {
var parser = Braces.Parser.init(lexer_output.tokens.items[0..], arena.allocator());
const ast_node = parser.parse() catch |err| {
globalThis.throwError(err, "failed to parse braces");
return .undefined;
return globalThis.throwError(err, "failed to parse braces");
};
const str = std.json.stringifyAlloc(globalThis.bunVM().allocator, ast_node, .{}) catch {
globalThis.throwOutOfMemory();
Expand Down Expand Up @@ -4738,8 +4735,7 @@ pub const JSZlib = struct {
return .zero;
}

globalThis.throwError(err, "Zlib error");
return .zero;
return globalThis.throwError(err, "Zlib error") catch return .zero;
};

reader.readAll() catch {
Expand Down Expand Up @@ -4857,8 +4853,7 @@ pub const JSZlib = struct {
return .zero;
}

globalThis.throwError(err, "Zlib error");
return .zero;
return globalThis.throwError(err, "Zlib error");
};

reader.readAll() catch {
Expand Down Expand Up @@ -4931,13 +4926,11 @@ const InternalTestingAPIs = struct {
.check_for_unhighlighted_write = false,
});
std.fmt.format(writer.writer(), "{}", .{formatter}) catch |err| {
globalThis.throwError(err, "Error formatting code");
return .zero;
return globalThis.throwError(err, "Error formatting code");
};

writer.flush() catch |err| {
globalThis.throwError(err, "Error formatting code");
return .zero;
return globalThis.throwError(err, "Error formatting code");
};

var str = bun.String.createUTF8(buffer.list.items);
Expand Down
13 changes: 4 additions & 9 deletions src/bun.js/api/JSTranspiler.zig
Original file line number Diff line number Diff line change
Expand Up @@ -760,8 +760,7 @@ pub fn constructor(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) b
return globalThis.throwValue2(log.toJS(globalThis, allocator, "Failed to create transpiler"));
}

globalThis.throwError(err, "Error creating transpiler");
return error.JSError;
return globalThis.throwError(err, "Error creating transpiler");
};
bundler.options.no_macros = transpiler_options.no_macros;
bundler.configureLinkerWithAutoJSX(false);
Expand All @@ -770,9 +769,7 @@ pub fn constructor(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) b
if ((log.warnings + log.errors) > 0) {
return globalThis.throwValue2(log.toJS(globalThis, allocator, "Failed to load define"));
}

globalThis.throwError(err, "Failed to load define");
return error.JSError;
return globalThis.throwError(err, "Failed to load define");
};

if (transpiler_options.macro_map.count() > 0) {
Expand Down Expand Up @@ -1101,8 +1098,7 @@ pub fn transformSync(
buffer_writer.reset();
var printer = JSPrinter.BufferPrinter.init(buffer_writer);
_ = this.bundler.print(parse_result, @TypeOf(&printer), &printer, .esm_ascii) catch |err| {
globalThis.throwError(err, "Failed to print code");
return .zero;
return globalThis.throwError(err, "Failed to print code");
};

// TODO: benchmark if pooling this way is faster or moving is faster
Expand Down Expand Up @@ -1244,8 +1240,7 @@ pub fn scanImports(
return .zero;
}

globalThis.throwError(err, "Failed to scan imports");
return .zero;
return globalThis.throwError(err, "Failed to scan imports");
};

defer this.scan_pass_result.reset();
Expand Down
9 changes: 3 additions & 6 deletions src/bun.js/api/bun/subprocess.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1999,8 +1999,7 @@ pub const Subprocess = struct {
}

if (!override_env and env_array.items.len == 0) {
env_array.items = jsc_vm.bundler.env.map.createNullDelimitedEnvMap(allocator) catch |err|
return globalThis.handleError(err, "in Bun.spawn");
env_array.items = jsc_vm.bundler.env.map.createNullDelimitedEnvMap(allocator) catch |err| return globalThis.throwError(err, "in Bun.spawn") catch return .zero;
env_array.capacity = env_array.items.len;
}

Expand Down Expand Up @@ -2034,7 +2033,7 @@ pub const Subprocess = struct {
// And then one fd is assigned specifically and only for IPC. If the user dont specify it, we add one (default: 3).
//
// When Bun.spawn() is given an `.ipc` callback, it enables IPC as follows:
env_array.ensureUnusedCapacity(allocator, 3) catch |err| return globalThis.handleError(err, "in Bun.spawn");
env_array.ensureUnusedCapacity(allocator, 3) catch |err| return globalThis.throwError(err, "in Bun.spawn") catch return .zero;
const ipc_fd: u32 = brk: {
if (ipc_channel == -1) {
// If the user didn't specify an IPC channel, we need to add one
Expand Down Expand Up @@ -2119,9 +2118,7 @@ pub const Subprocess = struct {
@ptrCast(env_array.items.ptr),
) catch |err| {
spawn_options.deinit();
globalThis.throwError(err, ": failed to spawn process");

return .zero;
return globalThis.throwError(err, ": failed to spawn process") catch return .zero;
}) {
.err => |err| {
spawn_options.deinit();
Expand Down
3 changes: 1 addition & 2 deletions src/bun.js/api/server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6553,8 +6553,7 @@ pub fn NewServer(comptime NamespaceType: type, comptime ssl_enabled_: bool, comp
.framework = bake_options.framework,
.vm = global.bunVM(),
}) catch |err| {
global.throwError(err, "while initializing Bun Dev Server");
return error.JSError;
return global.throwError(err, "while initializing Bun Dev Server");
};
} else null else null;
errdefer if (dev_server) |d| d.deinit();
Expand Down
21 changes: 4 additions & 17 deletions src/bun.js/bindings/bindings.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3399,25 +3399,12 @@ pub const JSGlobalObject = opaque {
return this.vm().throwError2(this, value);
}

pub fn throwError(
this: *JSGlobalObject,
err: anyerror,
comptime fmt: [:0]const u8,
) void {
var str = ZigString.init(std.fmt.allocPrint(this.bunVM().allocator, "{s} " ++ fmt, .{@errorName(err)}) catch return);
pub fn throwError(this: *JSGlobalObject, err: anyerror, comptime fmt: [:0]const u8) bun.JSError {
var str = ZigString.init(try std.fmt.allocPrint(this.bunVM().allocator, "{s} " ++ fmt, .{@errorName(err)}));
defer this.bunVM().allocator.free(ZigString.untagged(str._unsafe_ptr_do_not_use)[0..str.len]);
str.markUTF8();
const err_value = str.toErrorInstance(this);
this.vm().throwError(this, err_value);
this.bunVM().allocator.free(ZigString.untagged(str._unsafe_ptr_do_not_use)[0..str.len]);
}

pub fn handleError(
this: *JSGlobalObject,
err: anyerror,
comptime fmt: [:0]const u8,
) JSValue {
this.throwError(err, fmt);
return JSValue.jsUndefined();
return this.vm().throwError2(this, err_value);
}

pub fn ref(this: *JSGlobalObject) C_API.JSContextRef {
Expand Down
12 changes: 4 additions & 8 deletions src/bun.js/webcore/response.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2717,8 +2717,7 @@ pub const Fetch = struct {
.remote => unreachable,
},
) catch |err| {
globalThis.throwError(err, "Failed to decode file url");
return .zero;
return globalThis.throwError(err, "Failed to decode file url");
}];
var url_string: bun.String = bun.String.empty;
defer url_string.deref();
Expand Down Expand Up @@ -2748,17 +2747,15 @@ pub const Fetch = struct {
url_path_decoded = url_path_decoded[1..];
}
break :brk PosixToWinNormalizer.resolveCWDWithExternalBufZ(&path_buf, url_path_decoded) catch |err| {
globalThis.throwError(err, "Failed to resolve file url");
return .zero;
return globalThis.throwError(err, "Failed to resolve file url");
};
}
break :brk url_path_decoded;
}

var cwd_buf: bun.PathBuffer = undefined;
const cwd = if (Environment.isWindows) (bun.getcwd(&cwd_buf) catch |err| {
globalThis.throwError(err, "Failed to resolve file url");
return .zero;
return globalThis.throwError(err, "Failed to resolve file url");
}) else globalThis.bunVM().bundler.fs.top_level_dir;

const fullpath = bun.path.joinAbsStringBuf(
Expand All @@ -2773,8 +2770,7 @@ pub const Fetch = struct {
);
if (Environment.isWindows) {
break :brk PosixToWinNormalizer.resolveCWDWithExternalBufZ(&path_buf2, fullpath) catch |err| {
globalThis.throwError(err, "Failed to resolve file url");
return .zero;
return globalThis.throwError(err, "Failed to resolve file url");
};
}
break :brk fullpath;
Expand Down
9 changes: 3 additions & 6 deletions src/fmt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1736,21 +1736,18 @@ pub const fmt_js_test_bindings = struct {
.check_for_unhighlighted_write = false,
});
std.fmt.format(writer.writer(), "{}", .{formatter}) catch |err| {
globalThis.throwError(err, "Error formatting");
return .zero;
return globalThis.throwError(err, "Error formatting");
};
},
.escapePowershell => {
std.fmt.format(writer.writer(), "{}", .{escapePowershell(code.slice())}) catch |err| {
globalThis.throwError(err, "Error formatting");
return .zero;
return globalThis.throwError(err, "Error formatting");
};
},
}

writer.flush() catch |err| {
globalThis.throwError(err, "Error formatting");
return .zero;
return globalThis.throwError(err, "Error formatting");
};

var str = bun.String.createUTF8(buffer.list.items);
Expand Down
3 changes: 1 addition & 2 deletions src/ini.zig
Original file line number Diff line number Diff line change
Expand Up @@ -615,8 +615,7 @@ pub const IniTestingAPIs = struct {
try parser.parse(parser.arena.allocator());

return parser.out.toJS(bun.default_allocator, globalThis) catch |e| {
globalThis.throwError(e, "failed to turn AST into JS");
return .undefined;
return globalThis.throwError(e, "failed to turn AST into JS");
};
}
};
Expand Down
13 changes: 6 additions & 7 deletions src/patch.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1119,8 +1119,7 @@ pub const TestingAPIs = struct {
defer new_folder.deinit();

return switch (gitDiffInternal(bun.default_allocator, old_folder.slice(), new_folder.slice()) catch |e| {
globalThis.throwError(e, "failed to make diff");
return .undefined;
return globalThis.throwError(e, "failed to make diff");
}) {
.result => |s| {
defer s.deinit();
Expand Down Expand Up @@ -1174,10 +1173,10 @@ pub const TestingAPIs = struct {

var patchfile = parsePatchFile(patchfile_src.slice()) catch |e| {
if (e == error.hunk_header_integrity_check_failed) {
globalThis.throwError(e, "this indicates either that the supplied patch file was incorrect, or there is a bug in Bun. Please check your .patch file, or open a GitHub issue :)");
} else globalThis.throwError(e, "failed to parse patch file");

return .undefined;
return globalThis.throwError(e, "this indicates either that the supplied patch file was incorrect, or there is a bug in Bun. Please check your .patch file, or open a GitHub issue :)");
} else {
return globalThis.throwError(e, "failed to parse patch file");
}
};
defer patchfile.deinit(bun.default_allocator);

Expand Down Expand Up @@ -1223,7 +1222,7 @@ pub const TestingAPIs = struct {
}

patchfile_src.deinit();
globalThis.throwError(e, "failed to parse patchfile");
globalThis.throwError(e, "failed to parse patchfile") catch {};
return .{ .err = .undefined };
};

Expand Down
3 changes: 1 addition & 2 deletions src/shell/interpreter.zig
Original file line number Diff line number Diff line change
Expand Up @@ -823,8 +823,7 @@ pub const ParsedShellScript = struct {
return .undefined;
}

globalThis.throwError(err, "failed to lex/parse shell");
return .undefined;
return globalThis.throwError(err, "failed to lex/parse shell");
};

shargs.script_ast = script_ast;
Expand Down
9 changes: 3 additions & 6 deletions src/shell/shell.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4385,15 +4385,13 @@ pub const TestingAPIs = struct {
if (bun.strings.isAllASCII(script.items[0..])) {
var lexer = LexerAscii.new(arena.allocator(), script.items[0..], jsstrings.items[0..]);
lexer.lex() catch |err| {
globalThis.throwError(err, "failed to lex shell");
return JSValue.undefined;
return globalThis.throwError(err, "failed to lex shell");
};
break :brk lexer.get_result();
}
var lexer = LexerUnicode.new(arena.allocator(), script.items[0..], jsstrings.items[0..]);
lexer.lex() catch |err| {
globalThis.throwError(err, "failed to lex shell");
return JSValue.undefined;
return globalThis.throwError(err, "failed to lex shell");
};
break :brk lexer.get_result();
};
Expand Down Expand Up @@ -4487,8 +4485,7 @@ pub const TestingAPIs = struct {
return .undefined;
}

globalThis.throwError(err, "failed to lex/parse shell");
return .undefined;
return globalThis.throwError(err, "failed to lex/parse shell");
};

const str = std.json.stringifyAlloc(globalThis.bunVM().allocator, script_ast, .{}) catch {
Expand Down
Loading

0 comments on commit 6c46fbd

Please sign in to comment.