diff --git a/src/bun.js/api/BunObject.zig b/src/bun.js/api/BunObject.zig index 914e315339..cec1342b14 100644 --- a/src/bun.js/api/BunObject.zig +++ b/src/bun.js/api/BunObject.zig @@ -2561,8 +2561,7 @@ pub const Crypto = struct { BoringSSL.ERR_clear_error(); return globalThis.throwValue2(instance); } else { - globalThis.throwTODO("HMAC is not supported for this algorithm yet"); - return error.JSError; + return globalThis.throwTODO("HMAC is not supported for this algorithm yet"); } } return error.JSError; @@ -3366,8 +3365,7 @@ pub fn allocUnsafe(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) b pub fn mmapFile(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { if (comptime Environment.isWindows) { - globalThis.throwTODO("mmapFile is not supported on Windows"); - return JSC.JSValue.zero; + return globalThis.throwTODO("mmapFile is not supported on Windows"); } const arguments_ = callframe.arguments(2); diff --git a/src/bun.js/api/bun/socket.zig b/src/bun.js/api/bun/socket.zig index 1bf5e2f686..307f8d58ce 100644 --- a/src/bun.js/api/bun/socket.zig +++ b/src/bun.js/api/bun/socket.zig @@ -2305,7 +2305,7 @@ fn NewSocket(comptime ssl: bool) type { const length_value = args[2]; if (encoding_value != .undefined and (offset_value != .undefined or length_value != .undefined)) { - globalObject.throwTODO("Support encoding with offset and length altogether. Only either encoding or offset, length is supported, but not both combinations yet."); + globalObject.throwTODO("Support encoding with offset and length altogether. Only either encoding or offset, length is supported, but not both combinations yet.") catch {}; return .fail; } diff --git a/src/bun.js/api/bun/spawn/stdio.zig b/src/bun.js/api/bun/spawn/stdio.zig index 1f4e332344..320118d72f 100644 --- a/src/bun.js/api/bun/spawn/stdio.zig +++ b/src/bun.js/api/bun/spawn/stdio.zig @@ -387,12 +387,12 @@ pub const Stdio = union(enum) { switch (req.ptr) { .File, .Blob => { - globalThis.throwTODO("Support fd/blob backed ReadableStream in spawn stdin. See https://github.com/oven-sh/bun/issues/8049"); + globalThis.throwTODO("Support fd/blob backed ReadableStream in spawn stdin. See https://github.com/oven-sh/bun/issues/8049") catch {}; return false; }, .Direct, .JavaScript, .Bytes => { // out_stdio.* = .{ .connect = req }; - globalThis.throwTODO("Re-enable ReadableStream support in spawn stdin. "); + globalThis.throwTODO("Re-enable ReadableStream support in spawn stdin. ") catch {}; return false; }, .Invalid => { diff --git a/src/bun.js/api/server.zig b/src/bun.js/api/server.zig index 153af584e8..fbe2e3d13c 100644 --- a/src/bun.js/api/server.zig +++ b/src/bun.js/api/server.zig @@ -226,8 +226,7 @@ const StaticRoute = struct { .Blob, .InternalBlob, .WTFStringImpl => { if (response.body.value == .Blob and response.body.value.Blob.needsToReadFile()) { - globalThis.throwTODO("TODO: support Bun.file(path) in static routes"); - return error.JSError; + return globalThis.throwTODO("TODO: support Bun.file(path) in static routes"); } var blob = response.body.value.use(); blob.globalThis = globalThis; diff --git a/src/bun.js/bindings/bindings.zig b/src/bun.js/bindings/bindings.zig index 13519ed658..f595949a62 100644 --- a/src/bun.js/bindings/bindings.zig +++ b/src/bun.js/bindings/bindings.zig @@ -2962,10 +2962,10 @@ pub const JSGlobalObject = opaque { return .zero; } - pub fn throwTODO(this: *JSGlobalObject, msg: []const u8) void { + pub fn throwTODO(this: *JSGlobalObject, msg: []const u8) bun.JSError { const err = this.createErrorInstance("{s}", .{msg}); err.put(this, ZigString.static("name"), bun.String.static("TODOError").toJS(this)); - this.throwValue(err); + return this.throwValue2(err); } pub const throwTerminationException = JSGlobalObject__throwTerminationException; @@ -2980,21 +2980,13 @@ pub const JSGlobalObject = opaque { } /// Deprecated: use `throwInvalidArguments2` - pub fn throwInvalidArguments( - this: *JSGlobalObject, - comptime fmt: [:0]const u8, - args: anytype, - ) void { + pub fn throwInvalidArguments(this: *JSGlobalObject, comptime fmt: [:0]const u8, args: anytype) void { const err = JSC.toInvalidArguments(fmt, args, this); this.vm().throwError(this, err); } /// New system for throwing errors: returning bun.JSError - pub fn throwInvalidArguments2( - this: *JSGlobalObject, - comptime fmt: [:0]const u8, - args: anytype, - ) bun.JSError { + pub fn throwInvalidArguments2(this: *JSGlobalObject, comptime fmt: [:0]const u8, args: anytype) bun.JSError { const err = JSC.toInvalidArguments(fmt, args, this); return this.vm().throwError2(this, err); } diff --git a/src/codegen/generate-node-errors.ts b/src/codegen/generate-node-errors.ts index 41b5de47c7..916bece2d4 100644 --- a/src/codegen/generate-node-errors.ts +++ b/src/codegen/generate-node-errors.ts @@ -88,10 +88,10 @@ listHeader += ` `; zig += ` - + extern fn Bun__createErrorWithCode(globalThis: *JSC.JSGlobalObject, code: Error, message: *bun.String) JSC.JSValue; - + /// Creates an Error object with the given error code. /// Derefs the message string. pub fn toJS(this: Error, globalThis: *JSC.JSGlobalObject, message: *bun.String) JSC.JSValue { @@ -110,7 +110,7 @@ zig += ` } pub fn throw(this: Error, globalThis: *JSC.JSGlobalObject, comptime fmt_str: [:0]const u8, args: anytype) void { - globalThis.throwValue(fmt(this, globalThis, fmt_str, args)); + globalThis.throwValue(fmt(this, globalThis, fmt_str, args)); } }; diff --git a/src/install/dependency.zig b/src/install/dependency.zig index 66959bd493..2f5767866d 100644 --- a/src/install/dependency.zig +++ b/src/install/dependency.zig @@ -354,8 +354,7 @@ pub const Version = struct { } }, else => { - globalThis.throwTODO("Unsupported dependency type"); - return error.JSError; + return globalThis.throwTODO("Unsupported dependency type"); }, } diff --git a/src/shell/interpreter.zig b/src/shell/interpreter.zig index afefd5a12b..4456f2f2d9 100644 --- a/src/shell/interpreter.zig +++ b/src/shell/interpreter.zig @@ -1180,33 +1180,18 @@ pub const Interpreter = struct { } }; - pub fn createShellInterpreter( - globalThis: *JSC.JSGlobalObject, - callframe: *JSC.CallFrame, - ) bun.JSError!JSValue { + pub fn createShellInterpreter(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue { const allocator = bun.default_allocator; const arguments_ = callframe.arguments(3); var arguments = JSC.Node.ArgumentsSlice.init(globalThis.bunVM(), arguments_.slice()); - const resolve = arguments.nextEat() orelse { - globalThis.throw("shell: expected 3 arguments, got 0", .{}); - return .undefined; - }; + const resolve = arguments.nextEat() orelse return globalThis.throw2("shell: expected 3 arguments, got 0", .{}); - const reject = arguments.nextEat() orelse { - globalThis.throw("shell: expected 3 arguments, got 0", .{}); - return .undefined; - }; + const reject = arguments.nextEat() orelse return globalThis.throw2("shell: expected 3 arguments, got 0", .{}); - const parsed_shell_script_js = arguments.nextEat() orelse { - globalThis.throw("shell: expected 3 arguments, got 0", .{}); - return .undefined; - }; + const parsed_shell_script_js = arguments.nextEat() orelse return globalThis.throw2("shell: expected 3 arguments, got 0", .{}); - const parsed_shell_script = parsed_shell_script_js.as(ParsedShellScript) orelse { - globalThis.throw("shell: expected a ParsedShellScript", .{}); - return .undefined; - }; + const parsed_shell_script = parsed_shell_script_js.as(ParsedShellScript) orelse return globalThis.throw2("shell: expected a ParsedShellScript", .{}); var shargs: *ShellArgs = undefined; var jsobjs: std.ArrayList(JSValue) = std.ArrayList(JSValue).init(allocator); @@ -1214,10 +1199,7 @@ pub const Interpreter = struct { var cwd: ?bun.String = null; var export_env: ?EnvMap = null; - if (parsed_shell_script.args == null) { - globalThis.throw("shell: shell args is null, this is a bug in Bun. Please file a GitHub issue.", .{}); - return .undefined; - } + if (parsed_shell_script.args == null) return globalThis.throw2("shell: shell args is null, this is a bug in Bun. Please file a GitHub issue.", .{}); parsed_shell_script.take( globalThis, @@ -1248,8 +1230,7 @@ pub const Interpreter = struct { if (export_env) |*ee| ee.deinit(); if (cwd) |*cc| cc.deref(); shargs.deinit(); - throwShellErr(e, .{ .js = globalThis.bunVM().event_loop }); - return .zero; + return try throwShellErr(e, .{ .js = globalThis.bunVM().event_loop }); }, }; @@ -1259,7 +1240,7 @@ pub const Interpreter = struct { if (cwd) |*cc| cc.deref(); shargs.deinit(); interpreter.finalize(); - return .zero; + return error.JSError; } interpreter.flags.quiet = quiet; @@ -1472,8 +1453,7 @@ pub const Interpreter = struct { null, )) { .err => |*e| { - throwShellErr(e, .{ .mini = mini }); - return 1; + e.throwMini(); }, .result => |i| i, }; @@ -1542,8 +1522,7 @@ pub const Interpreter = struct { null, )) { .err => |*e| { - throwShellErr(e, .{ .mini = mini }); - return 1; + e.throwMini(); }, .result => |i| i, }; @@ -1643,8 +1622,7 @@ pub const Interpreter = struct { if (this.setupIOBeforeRun().asErr()) |e| { defer this.deinitEverything(); const shellerr = bun.shell.ShellErr.newSys(e); - throwShellErr(&shellerr, .{ .js = globalThis.bunVM().event_loop }); - return .undefined; + return try throwShellErr(&shellerr, .{ .js = globalThis.bunVM().event_loop }); } incrPendingActivityFlag(&this.has_pending_activity); @@ -2766,7 +2744,7 @@ pub const Interpreter = struct { } pub fn throw(this: *const State, err: *const bun.shell.ShellErr) void { - throwShellErr(err, this.eventLoop()); + throwShellErr(err, this.eventLoop()) catch {}; //TODO: } pub fn rootIO(this: *const State) *const IO { @@ -5745,7 +5723,7 @@ pub const Interpreter = struct { } pub inline fn throw(this: *const Builtin, err: *const bun.shell.ShellErr) void { - this.parentCmd().base.throw(err); + this.parentCmd().base.throw(err) catch {}; } pub inline fn parentCmd(this: *const Builtin) *const Cmd { @@ -12216,11 +12194,13 @@ inline fn fastMod(val: anytype, comptime rhs: comptime_int) @TypeOf(val) { return val & (rhs - 1); } -fn throwShellErr(e: *const bun.shell.ShellErr, event_loop: JSC.EventLoopHandle) void { - switch (event_loop) { +/// 'js' event loop will always return JSError +/// 'mini' event loop will always return noreturn and exit 1 +fn throwShellErr(e: *const bun.shell.ShellErr, event_loop: JSC.EventLoopHandle) bun.JSError!noreturn { + return switch (event_loop) { .mini => e.throwMini(), .js => e.throwJS(event_loop.js.global), - } + }; } pub const ReadChunkAction = enum { diff --git a/src/shell/shell.zig b/src/shell/shell.zig index 562d834946..1f36479922 100644 --- a/src/shell/shell.zig +++ b/src/shell/shell.zig @@ -83,30 +83,30 @@ pub const ShellErr = union(enum) { } } - pub fn throwJS(this: *const @This(), globalThis: *JSC.JSGlobalObject) void { + pub fn throwJS(this: *const @This(), globalThis: *JSC.JSGlobalObject) bun.JSError { defer this.deinit(bun.default_allocator); switch (this.*) { .sys => { const err = this.sys.toErrorInstance(globalThis); - globalThis.throwValue(err); + return globalThis.throwValue2(err); }, .custom => { var str = JSC.ZigString.init(this.custom); str.markUTF8(); const err_value = str.toErrorInstance(globalThis); - globalThis.throwValue(err_value); + return globalThis.throwValue2(err_value); // this.bunVM().allocator.free(JSC.ZigString.untagged(str._unsafe_ptr_do_not_use)[0..str.len]); }, .invalid_arguments => { - globalThis.throwInvalidArguments("{s}", .{this.invalid_arguments.val}); + return globalThis.throwInvalidArguments2("{s}", .{this.invalid_arguments.val}); }, .todo => { - globalThis.throwTODO(this.todo); + return globalThis.throwTODO(this.todo); }, } } - pub fn throwMini(this: @This()) void { + pub fn throwMini(this: @This()) noreturn { defer this.deinit(bun.default_allocator); switch (this) { .sys => |err| {