diff --git a/src/bun.js/api/bun/socket.zig b/src/bun.js/api/bun/socket.zig index 95f9655b18..6629a8d400 100644 --- a/src/bun.js/api/bun/socket.zig +++ b/src/bun.js/api/bun/socket.zig @@ -1572,7 +1572,7 @@ fn NewSocket(comptime ssl: bool) type { const globalObject = handlers.globalObject; const this_value = this.getThisValue(globalObject); _ = callback.call(globalObject, this_value, &.{this_value}) catch |err| { - _ = handlers.callErrorHandler(this_value, &.{ this_value, globalObject.takeException(err) }); + _ = handlers.callErrorHandler(this_value, &.{ this_value, globalObject.takeError(err) }); }; } pub fn onTimeout( @@ -1598,7 +1598,7 @@ fn NewSocket(comptime ssl: bool) type { const globalObject = handlers.globalObject; const this_value = this.getThisValue(globalObject); _ = callback.call(globalObject, this_value, &.{this_value}) catch |err| { - _ = handlers.callErrorHandler(this_value, &.{ this_value, globalObject.takeException(err) }); + _ = handlers.callErrorHandler(this_value, &.{ this_value, globalObject.takeError(err) }); }; } @@ -1843,7 +1843,7 @@ fn NewSocket(comptime ssl: bool) type { const globalObject = handlers.globalObject; const this_value = this.getThisValue(globalObject); _ = callback.call(globalObject, this_value, &.{this_value}) catch |err| { - _ = handlers.callErrorHandler(this_value, &.{ this_value, globalObject.takeException(err) }); + _ = handlers.callErrorHandler(this_value, &.{ this_value, globalObject.takeError(err) }); }; } @@ -1956,7 +1956,7 @@ fn NewSocket(comptime ssl: bool) type { this_value, js_error, }) catch |e| { - _ = handlers.callErrorHandler(this_value, &.{ this_value, globalObject.takeException(e) }); + _ = handlers.callErrorHandler(this_value, &.{ this_value, globalObject.takeError(e) }); }; } @@ -1988,7 +1988,7 @@ fn NewSocket(comptime ssl: bool) type { this_value, output_value, }) catch |err| { - _ = handlers.callErrorHandler(this_value, &.{ this_value, globalObject.takeException(err) }); + _ = handlers.callErrorHandler(this_value, &.{ this_value, globalObject.takeError(err) }); }; } diff --git a/src/bun.js/bindings/bindings.zig b/src/bun.js/bindings/bindings.zig index d61c1f374d..a7c9e35af2 100644 --- a/src/bun.js/bindings/bindings.zig +++ b/src/bun.js/bindings/bindings.zig @@ -3247,7 +3247,20 @@ pub const JSGlobalObject = opaque { } return this.tryTakeException() orelse { - @panic("A JavaScript exception was thrown, however it was cleared before it could be read."); + @panic("A JavaScript exception was thrown, but it was cleared before it could be read."); + }; + } + + pub fn takeError(this: *JSGlobalObject, proof: bun.JSError) JSValue { + switch (proof) { + error.JSError => {}, + error.OutOfMemory => this.throwOutOfMemory() catch {}, + } + + return (this.tryTakeException() orelse { + @panic("A JavaScript exception was thrown, but it was cleared before it could be read."); + }).toError() orelse { + @panic("Couldn't convert a JavaScript exception to an Error instance."); }; }