Skip to content

chore: leverage bridge for error rejection - #3403

Open
karlseguin wants to merge 1 commit into
mainfrom
error-bridge-rejection
Open

chore: leverage bridge for error rejection#3403
karlseguin wants to merge 1 commit into
mainfrom
error-bridge-rejection

Conversation

@karlseguin

Copy link
Copy Markdown
Collaborator

#3095 made the bridge more promise- aware. A WebAPI with a !js.Promise return type that returns an error will not reject the promise.

That PR was limited to Crypto. This expands it where possible. Generally speaking, the result is just more idiomatic Zig code. From:

validateName(name) catch |err| switch (err) {
 error.SyntaxError => return local.rejectPromise(.{ .dom_exception = .{ .err = error.SyntaxError } }),
}

to:

try validateName(name);

However, because TypeErrors must often reject with a specific message, we use a pseudo-global in Env. So, you'd do:

.invalid => return local.typeError("invalid algorithm"),

which return error.TypeError AND stores "invalid algorithm" on the Env. When Caller gets error.TypeError it checks the Env for a message. Who says Zig errors can't carry payloads?!

;# Please enter the commit message for your changes. Lines starting

#3095 made the bridge more promise-
aware. A WebAPI with a `!js.Promise` return type that returns an error will not
reject the promise.

That PR was limited to Crypto. This expands it where possible. Generally
speaking, the result is just more idiomatic Zig code. From:

```zig
validateName(name) catch |err| switch (err) {
 error.SyntaxError => return local.rejectPromise(.{ .dom_exception = .{ .err = error.SyntaxError } }),
}
```

to:

```zig
try validateName(name);
```

However, because TypeErrors must often reject with a specific message, we use
a pseudo-global in Env. So, you'd do:

```zig
.invalid => return local.typeError("invalid algorithm"),
```

which return error.TypeError AND stores "invalid algorithm" on the `Env`. When
`Caller` gets `error.TypeError` it checks the Env for a message. Who says Zig
errors can't carry payloads?!

;# Please enter the commit message for your changes. Lines starting
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant