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

fix: make error properties overwritable #1046

Merged
merged 5 commits into from
Jan 15, 2025
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions core/00_infra.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,7 @@
for (const property of new SafeArrayIterator(additionalProperties)) {
const key = property[0];
if (!(key in error)) {
ObjectDefineProperty(error, key, {
value: property[1],
writable: false,
});
error[key] = property[1];
}
}
}
Expand Down
1 change: 1 addition & 0 deletions testing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ deno_core.workspace = true
deno_core.features = ["unsafe_use_unprotected_platform", "snapshot_flags_eager_parse"]
deno_error.workspace = true
futures.workspace = true
thiserror.workspace = true
tokio.workspace = true
url.workspace = true

Expand Down
1 change: 1 addition & 0 deletions testing/checkin/runner/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ deno_core::extension!(
ops_error::op_async_throw_error_lazy,
ops_error::op_async_throw_error_deferred,
ops_error::op_error_custom_sync,
ops_error::op_error_custom_with_code_sync,
ops_buffer::op_v8slice_store,
ops_buffer::op_v8slice_clone,
ops_worker::op_worker_spawn,
Expand Down
17 changes: 17 additions & 0 deletions testing/checkin/runner/ops_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,20 @@ pub fn op_error_custom_sync(
) -> Result<(), JsErrorBox> {
Err(JsErrorBox::new("BadResource", message))
}

#[derive(Debug, thiserror::Error, deno_error::JsError)]
#[class(type)]
#[error("{message}")]
struct MyError {
message: String,
#[property]
code: u32,
}

#[op2(fast)]
pub fn op_error_custom_with_code_sync(
#[string] message: String,
code: u32,
) -> Result<(), MyError> {
Err(MyError { message, code })
}
5 changes: 5 additions & 0 deletions testing/checkin/runtime/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
op_async_throw_error_eager,
op_async_throw_error_lazy,
op_error_custom_sync,
op_error_custom_with_code_sync,
} from "ext:core/ops";

export async function asyncThrow(kind: "lazy" | "eager" | "deferred") {
Expand All @@ -18,3 +19,7 @@ export async function asyncThrow(kind: "lazy" | "eager" | "deferred") {
export function throwCustomError(message: string) {
op_error_custom_sync(message);
}

export function throwCustomErrorWithCode(message: string, code: number) {
op_error_custom_with_code_sync(message, code);
}
1 change: 1 addition & 0 deletions testing/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ unit_test!(
microtask_test,
ops_async_test,
ops_buffer_test,
ops_error_test,
resource_test,
serialize_deserialize_test,
stats_test,
Expand Down
1 change: 1 addition & 0 deletions testing/ops.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export function op_async_throw_error_lazy(...any: any[]): any;
export function op_error_context_async(...any: any[]): any;
export function op_error_context_sync(...any: any[]): any;
export function op_error_custom_sync(...any: any[]): any;
export function op_error_custom_with_code_sync(...any: any[]): any;

export function op_worker_await_close(...any: any[]): any;
export function op_worker_parent(...any: any[]): any;
Expand Down
13 changes: 13 additions & 0 deletions testing/unit/ops_error_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2018-2025 the Deno authors. MIT license.
import { assertEquals, test } from "checkin:testing";
import { throwCustomErrorWithCode } from "checkin:error";

test(function additionalPropertyIsWritable() {
try {
throwCustomErrorWithCode("foo", 1);
} catch (e) {
assertEquals(e.message, "foo");
assertEquals(e.code, 1);
e.code = 2;
}
});
Loading