Don't pass bare Exceptions to JS in node:net (#17639)

This commit is contained in:
Kai Tamkun
2025-02-24 20:08:33 -08:00
committed by GitHub
parent 94274b7198
commit 32e6049be0
2 changed files with 19 additions and 6 deletions

View File

@@ -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) });
};
}

View File

@@ -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.");
};
}