Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zig: make throwInvalidArguments use JSError #15305

Merged
merged 2 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/bake/FrameworkRouter.zig
Original file line number Diff line number Diff line change
Expand Up @@ -976,10 +976,10 @@ pub const JSFrameworkRouter = struct {
pub fn constructor(global: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) !*JSFrameworkRouter {
const opts = callframe.argumentsAsArray(1)[0];
if (!opts.isObject())
return global.throwInvalidArguments2("FrameworkRouter needs an object as it's first argument", .{});
return global.throwInvalidArguments("FrameworkRouter needs an object as it's first argument", .{});

const root = try opts.getOptional(global, "root", bun.String.Slice) orelse
return global.throwInvalidArguments2("Missing options.root", .{});
return global.throwInvalidArguments("Missing options.root", .{});
defer root.deinit();

const style = try validators.validateStringEnum(
Expand Down Expand Up @@ -1113,7 +1113,7 @@ pub const JSFrameworkRouter = struct {
const alloc = arena.allocator();

if (frame.argumentsCount() < 2)
return global.throwInvalidArguments2("parseRoutePattern takes two arguments", .{});
return global.throwInvalidArguments("parseRoutePattern takes two arguments", .{});

const style_js, const filepath_js = frame.argumentsAsArray(2);
const filepath = try filepath_js.toSlice2(global, alloc);
Expand All @@ -1122,7 +1122,7 @@ pub const JSFrameworkRouter = struct {
defer style_string.deinit();

const style = std.meta.stringToEnum(Style, style_string.slice()) orelse
return global.throwInvalidArguments2("unknown router style {}", .{bun.fmt.quote(style_string.slice())});
return global.throwInvalidArguments("unknown router style {}", .{bun.fmt.quote(style_string.slice())});

var log = TinyLog.empty;
const parsed = style.parse(filepath.slice(), std.fs.path.extension(filepath.slice()), &log, alloc) catch |err| switch (err) {
Expand Down
38 changes: 19 additions & 19 deletions src/bake/bake.zig
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub const UserOptions = struct {

pub fn fromJS(config: JSValue, global: *JSC.JSGlobalObject) !UserOptions {
if (!config.isObject()) {
return global.throwInvalidArguments2("'" ++ api_name ++ "' is not an object", .{});
return global.throwInvalidArguments("'" ++ api_name ++ "' is not an object", .{});
}
var arena = std.heap.ArenaAllocator.init(bun.default_allocator);
errdefer arena.deinit();
Expand All @@ -38,7 +38,7 @@ pub const UserOptions = struct {

const framework = try Framework.fromJS(
try config.get(global, "framework") orelse {
return global.throwInvalidArguments2("'" ++ api_name ++ "' is missing 'framework'", .{});
return global.throwInvalidArguments("'" ++ api_name ++ "' is missing 'framework'", .{});
},
global,
&allocations,
Expand Down Expand Up @@ -278,7 +278,7 @@ pub const Framework = struct {
}

if (!opts.isObject()) {
return global.throwInvalidArguments2("Framework must be an object", .{});
return global.throwInvalidArguments("Framework must be an object", .{});
}

if (try opts.get(global, "serverEntryPoint") != null) {
Expand All @@ -296,11 +296,11 @@ pub const Framework = struct {
if (rfr == .false or rfr == .null or rfr == .undefined) break :brk null;

if (!rfr.isObject()) {
return global.throwInvalidArguments2("'framework.reactFastRefresh' must be an object or 'true'", .{});
return global.throwInvalidArguments("'framework.reactFastRefresh' must be an object or 'true'", .{});
}

const prop = try rfr.get(global, "importSource") orelse {
return global.throwInvalidArguments2("'framework.reactFastRefresh' is missing 'importSource'", .{});
return global.throwInvalidArguments("'framework.reactFastRefresh' is missing 'importSource'", .{});
};

const str = try prop.toBunString2(global);
Expand All @@ -316,27 +316,27 @@ pub const Framework = struct {
if (sc == .false or sc == .null or sc == .undefined) break :sc null;

if (!sc.isObject()) {
return global.throwInvalidArguments2("'framework.serverComponents' must be an object or 'undefined'", .{});
return global.throwInvalidArguments("'framework.serverComponents' must be an object or 'undefined'", .{});
}

break :sc .{
.separate_ssr_graph = brk: {
// Intentionally not using a truthiness check
const prop = try sc.getOptional(global, "separateSSRGraph", JSValue) orelse {
return global.throwInvalidArguments2("Missing 'framework.serverComponents.separateSSRGraph'", .{});
return global.throwInvalidArguments("Missing 'framework.serverComponents.separateSSRGraph'", .{});
};
if (prop == .true) break :brk true;
if (prop == .false) break :brk false;
return global.throwInvalidArguments2("'framework.serverComponents.separateSSRGraph' must be a boolean", .{});
return global.throwInvalidArguments("'framework.serverComponents.separateSSRGraph' must be a boolean", .{});
},
.server_runtime_import = refs.track(
try sc.getOptional(global, "serverRuntimeImportSource", ZigString.Slice) orelse {
return global.throwInvalidArguments2("Missing 'framework.serverComponents.serverRuntimeImportSource'", .{});
return global.throwInvalidArguments("Missing 'framework.serverComponents.serverRuntimeImportSource'", .{});
},
),
.server_register_client_reference = refs.track(
try sc.getOptional(global, "serverRegisterClientReferenceExport", ZigString.Slice) orelse {
return global.throwInvalidArguments2("Missing 'framework.serverComponents.serverRegisterClientReferenceExport'", .{});
return global.throwInvalidArguments("Missing 'framework.serverComponents.serverRegisterClientReferenceExport'", .{});
},
),
};
Expand All @@ -353,19 +353,19 @@ pub const Framework = struct {
var i: usize = 0;
while (it.next()) |file| : (i += 1) {
if (!file.isObject()) {
return global.throwInvalidArguments2("'builtInModules[{d}]' is not an object", .{i});
return global.throwInvalidArguments("'builtInModules[{d}]' is not an object", .{i});
}

const path = try getOptionalString(file, global, "import", refs, arena) orelse {
return global.throwInvalidArguments2("'builtInModules[{d}]' is missing 'import'", .{i});
return global.throwInvalidArguments("'builtInModules[{d}]' is missing 'import'", .{i});
};

const value: BuiltInModule = if (try getOptionalString(file, global, "path", refs, arena)) |str|
.{ .import = str }
else if (try getOptionalString(file, global, "code", refs, arena)) |str|
.{ .code = str }
else
return global.throwInvalidArguments2("'builtInModules[{d}]' needs either 'path' or 'code'", .{i});
return global.throwInvalidArguments("'builtInModules[{d}]' needs either 'path' or 'code'", .{i});

files.putAssumeCapacity(path, value);
}
Expand All @@ -374,22 +374,22 @@ pub const Framework = struct {
};
const file_system_router_types: []FileSystemRouterType = brk: {
const array: JSValue = try opts.getArray(global, "fileSystemRouterTypes") orelse {
return global.throwInvalidArguments2("Missing 'framework.fileSystemRouterTypes'", .{});
return global.throwInvalidArguments("Missing 'framework.fileSystemRouterTypes'", .{});
};
const len = array.getLength(global);
if (len > 256) {
return global.throwInvalidArguments2("Framework can only define up to 256 file-system router types", .{});
return global.throwInvalidArguments("Framework can only define up to 256 file-system router types", .{});
}
const file_system_router_types = try arena.alloc(FileSystemRouterType, len);

var it = array.arrayIterator(global);
var i: usize = 0;
while (it.next()) |fsr_opts| : (i += 1) {
const root = try getOptionalString(fsr_opts, global, "root", refs, arena) orelse {
return global.throwInvalidArguments2("'fileSystemRouterTypes[{d}]' is missing 'root'", .{i});
return global.throwInvalidArguments("'fileSystemRouterTypes[{d}]' is missing 'root'", .{i});
};
const server_entry_point = try getOptionalString(fsr_opts, global, "serverEntryPoint", refs, arena) orelse {
return global.throwInvalidArguments2("'fileSystemRouterTypes[{d}]' is missing 'serverEntryPoint'", .{i});
return global.throwInvalidArguments("'fileSystemRouterTypes[{d}]' is missing 'serverEntryPoint'", .{i});
};
const client_entry_point = try getOptionalString(fsr_opts, global, "clientEntryPoint", refs, arena);
const prefix = try getOptionalString(fsr_opts, global, "prefix", refs, arena) orelse "/";
Expand Down Expand Up @@ -421,7 +421,7 @@ pub const Framework = struct {
break :exts extensions;
}

return global.throwInvalidArguments2("'extensions' must be an array of strings or \"*\" for all extensions", .{});
return global.throwInvalidArguments("'extensions' must be an array of strings or \"*\" for all extensions", .{});
} else &.{ ".jsx", ".tsx", ".js", ".ts", ".cjs", ".cts", ".mjs", ".mts" };

const ignore_dirs: []const []const u8 = if (try fsr_opts.get(global, "ignoreDirs")) |exts_js| exts: {
Expand All @@ -435,7 +435,7 @@ pub const Framework = struct {
break :exts dirs;
}

return global.throwInvalidArguments2("'ignoreDirs' must be an array of strings or \"*\" for all extensions", .{});
return global.throwInvalidArguments("'ignoreDirs' must be an array of strings or \"*\" for all extensions", .{});
} else &.{ ".git", "node_modules" };

file_system_router_types[i] = .{
Expand Down
12 changes: 4 additions & 8 deletions src/bun.js/ConsoleObject.zig
Original file line number Diff line number Diff line change
Expand Up @@ -706,17 +706,15 @@ pub const FormatOptions = struct {
if (opt.isInt32()) {
const arg = opt.toInt32();
if (arg < 0) {
globalThis.throwInvalidArguments("expected depth to be greater than or equal to 0, got {d}", .{arg});
return error.JSError;
return globalThis.throwInvalidArguments("expected depth to be greater than or equal to 0, got {d}", .{arg});
}
formatOptions.max_depth = @as(u16, @truncate(@as(u32, @intCast(@min(arg, std.math.maxInt(u16))))));
} else if (opt.isNumber()) {
const v = opt.coerce(f64, globalThis);
if (std.math.isInf(v)) {
formatOptions.max_depth = std.math.maxInt(u16);
} else {
globalThis.throwInvalidArguments("expected depth to be an integer, got {d}", .{v});
return error.JSError;
return globalThis.throwInvalidArguments("expected depth to be an integer, got {d}", .{v});
}
}
}
Expand All @@ -737,17 +735,15 @@ pub const FormatOptions = struct {
if (depthArg.isInt32()) {
const arg = depthArg.toInt32();
if (arg < 0) {
globalThis.throwInvalidArguments("expected depth to be greater than or equal to 0, got {d}", .{arg});
return error.JSError;
return globalThis.throwInvalidArguments("expected depth to be greater than or equal to 0, got {d}", .{arg});
}
formatOptions.max_depth = @as(u16, @truncate(@as(u32, @intCast(@min(arg, std.math.maxInt(u16))))));
} else if (depthArg.isNumber()) {
const v = depthArg.coerce(f64, globalThis);
if (std.math.isInf(v)) {
formatOptions.max_depth = std.math.maxInt(u16);
} else {
globalThis.throwInvalidArguments("expected depth to be an integer, got {d}", .{v});
return error.JSError;
return globalThis.throwInvalidArguments("expected depth to be an integer, got {d}", .{v});
}
}
if (arguments.len > 1 and !arguments[1].isEmptyOrUndefinedOrNull()) {
Expand Down
Loading