Skip to content

Commit

Permalink
fix: make error properties writable (#1046)
Browse files Browse the repository at this point in the history
  • Loading branch information
crowlKats authored Jan 15, 2025
1 parent 8596923 commit 9121db2
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 4 deletions.
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;
}
});

0 comments on commit 9121db2

Please sign in to comment.