Skip to content

Commit 2756903

Browse files
committed
Add DomExceptionName
1 parent 0b5172e commit 2756903

File tree

2 files changed

+139
-44
lines changed

2 files changed

+139
-44
lines changed

modules/llrt_abort/src/abort_signal.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::sync::{Arc, RwLock};
44

55
use llrt_events::{Emitter, EventEmitter, EventList};
6-
use llrt_exceptions::DOMException;
6+
use llrt_exceptions::{DOMException, DOMExceptionName};
77
use llrt_utils::mc_oneshot;
88
use rquickjs::{
99
class::{Trace, Tracer},
@@ -160,7 +160,11 @@ impl<'js> AbortSignal<'js> {
160160
pub fn send_aborted(this: This<Class<'js, Self>>, ctx: Ctx<'js>) -> Result<()> {
161161
let mut borrow = this.borrow_mut();
162162
borrow.aborted = true;
163-
let reason = get_reason_or_dom_exception(&ctx, borrow.reason.as_ref(), "AbortError")?;
163+
let reason = get_reason_or_dom_exception(
164+
&ctx,
165+
borrow.reason.as_ref(),
166+
DOMExceptionName::AbortError,
167+
)?;
164168
borrow.reason = Some(reason.clone());
165169
borrow.sender.send(reason);
166170
drop(borrow);
@@ -179,7 +183,8 @@ impl<'js> AbortSignal<'js> {
179183

180184
#[qjs(static)]
181185
pub fn timeout(ctx: Ctx<'js>, milliseconds: u64) -> Result<Class<'js, Self>> {
182-
let timeout_error = get_reason_or_dom_exception(&ctx, None, "TimeoutError")?;
186+
let timeout_error =
187+
get_reason_or_dom_exception(&ctx, None, DOMExceptionName::TimeoutError)?;
183188

184189
let signal = Self::new();
185190
let signal_instance = Class::instance(ctx.clone(), signal)?;
@@ -221,12 +226,12 @@ impl<'js> AbortSignal<'js> {
221226
fn get_reason_or_dom_exception<'js>(
222227
ctx: &Ctx<'js>,
223228
reason: Option<&Value<'js>>,
224-
name: &str,
229+
name: DOMExceptionName,
225230
) -> Result<Value<'js>> {
226231
let reason = if let Some(reason) = reason {
227232
reason.clone()
228233
} else {
229-
let ex = DOMException::new(ctx.clone(), Opt(None), Opt(Some(name.into())))?;
234+
let ex = DOMException::new_with_name(ctx, name, String::new())?;
230235
Class::instance(ctx.clone(), ex)?.into_value()
231236
};
232237
Ok(reason)

modules/llrt_exceptions/src/lib.rs

Lines changed: 129 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,81 @@
1-
use llrt_utils::primordials::{BasePrimordials, Primordial};
21
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
32
// SPDX-License-Identifier: Apache-2.0
3+
use llrt_utils::primordials::{BasePrimordials, Primordial};
44
use rquickjs::{atom::PredefinedAtom, function::Opt, Class, Ctx, Object, Result};
55

66
#[rquickjs::class]
77
#[derive(rquickjs::class::Trace, rquickjs::JsLifetime)]
88
pub struct DOMException {
9-
message: String,
109
name: String,
10+
message: String,
1111
stack: String,
1212
code: u8,
1313
}
1414

15+
// https://webidl.spec.whatwg.org/#dfn-error-names-table
16+
#[derive(Debug)]
17+
pub enum DOMExceptionName {
18+
IndexSizeError,
19+
HierarchyRequestError,
20+
WrongDocumentError,
21+
InvalidCharacterError,
22+
NoModificationAllowedError,
23+
NotFoundError,
24+
NotSupportedError,
25+
InUseAttributeError,
26+
InvalidStateError,
27+
SyntaxError,
28+
InvalidModificationError,
29+
NamespaceError,
30+
InvalidAccessError,
31+
TypeMismatchError,
32+
SecurityError,
33+
NetworkError,
34+
AbortError,
35+
URLMismatchError,
36+
QuotaExceededError,
37+
TimeoutError,
38+
InvalidNodeTypeError,
39+
DataCloneError,
40+
EncodingError,
41+
NotReadableError,
42+
UnknownError,
43+
ConstraintError,
44+
DataError,
45+
TransactionInactiveError,
46+
ReadOnlyError,
47+
VersionError,
48+
OperationError,
49+
NotAllowedError,
50+
Error,
51+
Other(String),
52+
}
53+
1554
#[rquickjs::methods]
1655
impl DOMException {
1756
#[qjs(constructor)]
1857
pub fn new(ctx: Ctx<'_>, message: Opt<String>, name: Opt<String>) -> Result<Self> {
19-
let primordials = BasePrimordials::get(&ctx)?;
58+
let message = message.0.unwrap_or_default();
59+
let name = name
60+
.0
61+
.map(DOMExceptionName::from)
62+
.unwrap_or(DOMExceptionName::Error);
63+
64+
Self::new_with_name(&ctx, name, message)
65+
}
66+
67+
#[qjs(skip)]
68+
pub fn new_with_name(ctx: &Ctx<'_>, name: DOMExceptionName, message: String) -> Result<Self> {
69+
let primordials = BasePrimordials::get(ctx)?;
2070

2171
let new: Object = primordials
2272
.constructor_error
2373
.construct((message.clone(),))?;
2474

25-
let message = message.0.unwrap_or(String::from(""));
26-
let name = name.0.unwrap_or(String::from("Error"));
27-
28-
// https://webidl.spec.whatwg.org/#dfn-error-names-table
29-
let code = match name.as_str() {
30-
"IndexSizeError" => 1,
31-
"HierarchyRequestError" => 3,
32-
"WrongDocumentError" => 4,
33-
"InvalidCharacterError" => 5,
34-
"NoModificationAllowedError" => 7,
35-
"NotFoundError" => 8,
36-
"NotSupportedError" => 9,
37-
"InUseAttributeError" => 10,
38-
"InvalidStateError" => 11,
39-
"SyntaxError" => 12,
40-
"InvalidModificationError" => 13,
41-
"NamespaceError" => 14,
42-
"InvalidAccessError" => 15,
43-
"TypeMismatchError" => 17,
44-
"SecurityError" => 18,
45-
"NetworkError" => 19,
46-
"AbortError" => 20,
47-
"URLMismatchError" => 21,
48-
"QuotaExceededError" => 22,
49-
"TimeoutError" => 23,
50-
"InvalidNodeTypeError" => 24,
51-
"DataCloneError" => 25,
52-
_ => 0,
53-
};
54-
5575
Ok(Self {
76+
name: format!("{:?}", name),
77+
code: name.code(),
5678
message,
57-
name,
58-
code,
5979
stack: new.get::<_, String>(PredefinedAtom::Stack)?,
6080
})
6181
}
@@ -66,8 +86,8 @@ impl DOMException {
6686
}
6787

6888
#[qjs(get)]
69-
pub fn name(&self) -> String {
70-
self.name.clone()
89+
pub fn name(&self) -> &str {
90+
self.name.as_str()
7191
}
7292

7393
#[qjs(get)]
@@ -84,10 +104,80 @@ impl DOMException {
84104
#[qjs(rename = PredefinedAtom::ToString)]
85105
pub fn to_string(&self) -> String {
86106
if self.message.is_empty() {
87-
return self.name.clone();
107+
return self.name().to_string();
108+
}
109+
110+
[self.name(), self.message.as_str()].join(": ")
111+
}
112+
}
113+
114+
impl DOMExceptionName {
115+
fn code(&self) -> u8 {
116+
match self {
117+
DOMExceptionName::IndexSizeError => 1,
118+
DOMExceptionName::HierarchyRequestError => 3,
119+
DOMExceptionName::WrongDocumentError => 4,
120+
DOMExceptionName::InvalidCharacterError => 5,
121+
DOMExceptionName::NoModificationAllowedError => 7,
122+
DOMExceptionName::NotFoundError => 8,
123+
DOMExceptionName::NotSupportedError => 9,
124+
DOMExceptionName::InUseAttributeError => 10,
125+
DOMExceptionName::InvalidStateError => 11,
126+
DOMExceptionName::SyntaxError => 12,
127+
DOMExceptionName::InvalidModificationError => 13,
128+
DOMExceptionName::NamespaceError => 14,
129+
DOMExceptionName::InvalidAccessError => 15,
130+
DOMExceptionName::TypeMismatchError => 17,
131+
DOMExceptionName::SecurityError => 18,
132+
DOMExceptionName::NetworkError => 19,
133+
DOMExceptionName::AbortError => 20,
134+
DOMExceptionName::URLMismatchError => 21,
135+
DOMExceptionName::QuotaExceededError => 22,
136+
DOMExceptionName::TimeoutError => 23,
137+
DOMExceptionName::InvalidNodeTypeError => 24,
138+
DOMExceptionName::DataCloneError => 25,
139+
_ => 0,
88140
}
141+
}
142+
}
89143

90-
[self.name.as_str(), self.message.as_str()].join(": ")
144+
impl From<String> for DOMExceptionName {
145+
fn from(value: String) -> Self {
146+
match value.as_str() {
147+
"IndexSizeError" => DOMExceptionName::IndexSizeError,
148+
"HierarchyRequestError" => DOMExceptionName::HierarchyRequestError,
149+
"WrongDocumentError" => DOMExceptionName::WrongDocumentError,
150+
"InvalidCharacterError" => DOMExceptionName::InvalidCharacterError,
151+
"NoModificationAllowedError" => DOMExceptionName::NoModificationAllowedError,
152+
"NotFoundError" => DOMExceptionName::NotFoundError,
153+
"NotSupportedError" => DOMExceptionName::NotSupportedError,
154+
"InUseAttributeError" => DOMExceptionName::InUseAttributeError,
155+
"InvalidStateError" => DOMExceptionName::InvalidStateError,
156+
"SyntaxError" => DOMExceptionName::SyntaxError,
157+
"InvalidModificationError" => DOMExceptionName::InvalidModificationError,
158+
"NamespaceError" => DOMExceptionName::NamespaceError,
159+
"InvalidAccessError" => DOMExceptionName::InvalidAccessError,
160+
"TypeMismatchError" => DOMExceptionName::TypeMismatchError,
161+
"SecurityError" => DOMExceptionName::SecurityError,
162+
"NetworkError" => DOMExceptionName::NetworkError,
163+
"AbortError" => DOMExceptionName::AbortError,
164+
"URLMismatchError" => DOMExceptionName::URLMismatchError,
165+
"QuotaExceededError" => DOMExceptionName::QuotaExceededError,
166+
"TimeoutError" => DOMExceptionName::TimeoutError,
167+
"InvalidNodeTypeError" => DOMExceptionName::InvalidNodeTypeError,
168+
"DataCloneError" => DOMExceptionName::DataCloneError,
169+
"EncodingError" => DOMExceptionName::EncodingError,
170+
"NotReadableError" => DOMExceptionName::NotReadableError,
171+
"UnknownError" => DOMExceptionName::UnknownError,
172+
"ConstraintError" => DOMExceptionName::ConstraintError,
173+
"DataError" => DOMExceptionName::DataError,
174+
"TransactionInactiveError" => DOMExceptionName::TransactionInactiveError,
175+
"ReadOnlyError" => DOMExceptionName::ReadOnlyError,
176+
"VersionError" => DOMExceptionName::VersionError,
177+
"OperationError" => DOMExceptionName::OperationError,
178+
"NotAllowedError" => DOMExceptionName::NotAllowedError,
179+
_ => DOMExceptionName::Other(value),
180+
}
91181
}
92182
}
93183

0 commit comments

Comments
 (0)