Handle catching exception error objects

https://github.com/lightpanda-io/browser/issues/1443
This commit is contained in:
Karl Seguin
2026-01-30 18:38:22 +08:00
parent e6e32b5fd2
commit 324f6fe16e
2 changed files with 28 additions and 6 deletions

View File

@@ -76,10 +76,6 @@ pub fn defineOwnProperty(self: Object, name: []const u8, value: js.Value, attr:
} }
} }
pub fn toString(self: Object) ![]const u8 {
return self.local.ctx.valueToString(self.toValue(), .{});
}
pub fn toValue(self: Object) js.Value { pub fn toValue(self: Object) js.Value {
return .{ return .{
.local = self.local, .local = self.local,

View File

@@ -46,12 +46,38 @@ pub fn caught(self: TryCatch, allocator: Allocator) ?Caught {
const exception: ?[]const u8 = blk: { const exception: ?[]const u8 = blk: {
const handle = v8.v8__TryCatch__Exception(&self.handle) orelse break :blk null; const handle = v8.v8__TryCatch__Exception(&self.handle) orelse break :blk null;
break :blk js.String.toSliceWithAlloc(.{ .local = l, .handle = @ptrCast(handle) }, allocator) catch |err| @errorName(err); var js_val = js.Value{ .local = l, .handle = handle };
// If it's an Error object, try to get the message property
if (js_val.isObject()) {
const js_obj = js_val.toObject();
if (js_obj.has("message")) {
js_val = js_obj.get("message") catch break :blk null;
}
}
if (js_val.isString()) |js_str| {
break :blk js_str.toSliceWithAlloc(allocator) catch |err| @errorName(err);
}
break :blk null;
}; };
const stack: ?[]const u8 = blk: { const stack: ?[]const u8 = blk: {
const handle = v8.v8__TryCatch__StackTrace(&self.handle, l.handle) orelse break :blk null; const handle = v8.v8__TryCatch__StackTrace(&self.handle, l.handle) orelse break :blk null;
break :blk js.String.toSliceWithAlloc(.{ .local = l, .handle = @ptrCast(handle) }, allocator) catch |err| @errorName(err); var js_val = js.Value{ .local = l, .handle = handle };
// If it's an Error object, try to get the stack property
if (js_val.isObject()) {
const js_obj = js_val.toObject();
if (js_obj.has("stack")) {
js_val = js_obj.get("stack") catch break :blk null;
}
}
if (js_val.isString()) |js_str| {
break :blk js_str.toSliceWithAlloc(allocator) catch |err| @errorName(err);
}
break :blk null;
}; };
return .{ return .{