Skip to content

Commit

Permalink
zig: make throwPretty use JSError (#15410)
Browse files Browse the repository at this point in the history
  • Loading branch information
nektro authored Nov 26, 2024
1 parent 8ca0eb8 commit c434b2c
Show file tree
Hide file tree
Showing 7 changed files with 224 additions and 447 deletions.
6 changes: 2 additions & 4 deletions src/bun.js/api/JSBundler.zig
Original file line number Diff line number Diff line change
Expand Up @@ -322,15 +322,13 @@ pub const JSBundler = struct {
defer path.deinit();

var dir = std.fs.cwd().openDir(path.slice(), .{}) catch |err| {
globalThis.throwPretty("{s}: failed to open root directory: {s}", .{ @errorName(err), path.slice() });
return error.JSError;
return globalThis.throwPretty("{s}: failed to open root directory: {s}", .{ @errorName(err), path.slice() });
};
defer dir.close();

var rootdir_buf: bun.PathBuffer = undefined;
const rootdir = bun.getFdPath(bun.toFD(dir.fd), &rootdir_buf) catch |err| {
globalThis.throwPretty("{s}: failed to get full root directory path: {s}", .{ @errorName(err), path.slice() });
return error.JSError;
return globalThis.throwPretty("{s}: failed to get full root directory path: {s}", .{ @errorName(err), path.slice() });
};
try this.rootdir.appendSliceExact(rootdir);
}
Expand Down
5 changes: 2 additions & 3 deletions src/bun.js/api/server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5330,7 +5330,7 @@ pub const ServerWebSocket = struct {
callframe: *JSC.CallFrame,
comptime name: string,
comptime opcode: uws.Opcode,
) JSValue {
) bun.JSError!JSValue {
const args = callframe.arguments_old(2);

if (this.isClosed()) {
Expand Down Expand Up @@ -5377,8 +5377,7 @@ pub const ServerWebSocket = struct {
},
}
} else {
globalThis.throwPretty("{s} requires a string or BufferSource", .{name});
return .zero;
return globalThis.throwPretty("{s} requires a string or BufferSource", .{name});
}
}
}
Expand Down
27 changes: 7 additions & 20 deletions src/bun.js/bindings/bindings.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3303,37 +3303,24 @@ pub const JSGlobalObject = opaque {
return err;
}

pub fn throw(
this: *JSGlobalObject,
comptime fmt: [:0]const u8,
args: anytype,
) void {
pub fn throw(this: *JSGlobalObject, comptime fmt: [:0]const u8, args: anytype) void {
const instance = this.createErrorInstance(fmt, args);
if (instance != .zero)
this.vm().throwError(this, instance);
bun.assert(instance != .zero);
this.vm().throwError(this, instance);
}

pub fn throw2(
this: *JSGlobalObject,
comptime fmt: [:0]const u8,
args: anytype,
) JSError {
pub fn throw2(this: *JSGlobalObject, comptime fmt: [:0]const u8, args: anytype) JSError {
const instance = this.createErrorInstance(fmt, args);
bun.assert(instance != .zero);
return this.vm().throwError2(this, instance);
}

pub fn throwPretty(
this: *JSGlobalObject,
comptime fmt: [:0]const u8,
args: anytype,
) void {
pub fn throwPretty(this: *JSGlobalObject, comptime fmt: [:0]const u8, args: anytype) bun.JSError {
const instance = switch (Output.enable_ansi_colors) {
inline else => |enabled| this.createErrorInstance(Output.prettyFmt(fmt, enabled), args),
};

if (instance != .zero)
this.vm().throwError(this, instance);
bun.assert(instance != .zero);
return this.vm().throwError2(this, instance);
}
extern fn JSC__JSGlobalObject__queueMicrotaskCallback(*JSGlobalObject, *anyopaque, Function: *const (fn (*anyopaque) callconv(.C) void)) void;
pub fn queueMicrotaskCallback(
Expand Down
Loading

0 comments on commit c434b2c

Please sign in to comment.