Skip to content

Commit 049460a

Browse files
Add a TryCatch err method helper
Signed-off-by: Francis Bouvier <francis@lightpanda.io>
1 parent 0f6d5b8 commit 049460a

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/engines/v8/v8.zig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,18 @@ pub const TryCatch = struct {
466466
return null;
467467
}
468468

469+
// a shorthand method to return either the entire stack message
470+
// or just the exception message
471+
// - in Debug mode return the stack if available
472+
// - otherwhise return the exception if available
473+
// the caller needs to deinit the string returned
474+
pub fn err(self: TryCatch, alloc: std.mem.Allocator, env: Env) anyerror!?[]const u8 {
475+
if (builtin.mode == .Debug) {
476+
if (try self.stack(alloc, env)) |msg| return msg;
477+
}
478+
return try self.exception(alloc, env);
479+
}
480+
469481
pub fn deinit(self: *TryCatch) void {
470482
self.inner.deinit();
471483
}

src/interfaces.zig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,13 @@ pub fn TryCatch(comptime T: type, comptime env: type) void {
145145
env: env,
146146
) anyerror!?[]const u8);
147147

148+
// err()
149+
assertDecl(T, "err", fn (
150+
self: T,
151+
alloc: std.mem.Allocator,
152+
env: env,
153+
) anyerror!?[]const u8);
154+
148155
// stack()
149156
assertDecl(T, "stack", fn (
150157
self: T,

0 commit comments

Comments
 (0)