mirror of
https://github.com/denoland/deno.git
synced 2025-01-08 23:28:18 -05:00
refactor(serde_v8): move to thiserror
, better error output (#18202)
Ref #17318 --------- Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
This commit is contained in:
parent
154920a24e
commit
300ced90d8
6 changed files with 33 additions and 26 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -3978,6 +3978,7 @@ dependencies = [
|
||||||
"serde_bytes",
|
"serde_bytes",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"smallvec",
|
"smallvec",
|
||||||
|
"thiserror",
|
||||||
"v8",
|
"v8",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
@ -4373,7 +4373,7 @@ Deno.core.opAsync("op_async_serialize_object_with_numbers_as_keys", {
|
||||||
let sum = Deno.core.ops.op_sum_take(w32.subarray(0, 2));
|
let sum = Deno.core.ops.op_sum_take(w32.subarray(0, 2));
|
||||||
return false;
|
return false;
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
return e.message.includes('ExpectedDetachable');
|
return e.message.includes('invalid type, expected: detachable');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
if (!assertWasmThrow()) {
|
if (!assertWasmThrow()) {
|
||||||
|
|
|
@ -20,6 +20,7 @@ num-bigint.workspace = true
|
||||||
serde.workspace = true
|
serde.workspace = true
|
||||||
serde_bytes.workspace = true
|
serde_bytes.workspace = true
|
||||||
smallvec = { workspace = true, features = ["union"] }
|
smallvec = { workspace = true, features = ["union"] }
|
||||||
|
thiserror.workspace = true
|
||||||
v8.workspace = true
|
v8.workspace = true
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
|
|
@ -281,8 +281,9 @@ impl<'de, 'a, 'b, 's, 'x> de::Deserializer<'de>
|
||||||
if obj.is_array() {
|
if obj.is_array() {
|
||||||
// If the obj is an array fail if it's length differs from the tuple length
|
// If the obj is an array fail if it's length differs from the tuple length
|
||||||
let array = v8::Local::<v8::Array>::try_from(self.input).unwrap();
|
let array = v8::Local::<v8::Array>::try_from(self.input).unwrap();
|
||||||
if array.length() as usize != len {
|
let array_len = array.length() as usize;
|
||||||
return Err(Error::LengthMismatch);
|
if array_len != len {
|
||||||
|
return Err(Error::LengthMismatch(array_len, len));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
visitor.visit_seq(SeqAccess::new(obj, self.scope, 0..len as u32))
|
visitor.visit_seq(SeqAccess::new(obj, self.scope, 0..len as u32))
|
||||||
|
@ -409,8 +410,9 @@ impl<'de, 'a, 'b, 's, 'x> de::Deserializer<'de>
|
||||||
let prop_names =
|
let prop_names =
|
||||||
obj.get_own_property_names(self.scope, Default::default());
|
obj.get_own_property_names(self.scope, Default::default());
|
||||||
let prop_names = prop_names.ok_or(Error::ExpectedEnum)?;
|
let prop_names = prop_names.ok_or(Error::ExpectedEnum)?;
|
||||||
if prop_names.length() != 1 {
|
let prop_names_len = prop_names.length();
|
||||||
return Err(Error::LengthMismatch);
|
if prop_names_len != 1 {
|
||||||
|
return Err(Error::LengthMismatch(prop_names_len as usize, 1));
|
||||||
}
|
}
|
||||||
prop_names.get_index(self.scope, 0).unwrap()
|
prop_names.get_index(self.scope, 0).unwrap()
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,56 +1,59 @@
|
||||||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
use std::fmt::{self};
|
|
||||||
|
|
||||||
use serde::de;
|
|
||||||
use serde::ser;
|
|
||||||
|
|
||||||
pub type Result<T> = std::result::Result<T, Error>;
|
pub type Result<T> = std::result::Result<T, Error>;
|
||||||
|
|
||||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
#[derive(Clone, Debug, Eq, PartialEq, thiserror::Error)]
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
|
#[error("{0}")]
|
||||||
Message(String),
|
Message(String),
|
||||||
|
|
||||||
|
#[error("serde_v8 error: invalid type, expected: boolean")]
|
||||||
ExpectedBoolean,
|
ExpectedBoolean,
|
||||||
|
#[error("serde_v8 error: invalid type, expected: integer")]
|
||||||
ExpectedInteger,
|
ExpectedInteger,
|
||||||
|
#[error("serde_v8 error: invalid type, expected: number")]
|
||||||
ExpectedNumber,
|
ExpectedNumber,
|
||||||
|
#[error("serde_v8 error: invalid type, expected: string")]
|
||||||
ExpectedString,
|
ExpectedString,
|
||||||
|
#[error("serde_v8 error: invalid type, expected: array")]
|
||||||
ExpectedArray,
|
ExpectedArray,
|
||||||
|
#[error("serde_v8 error: invalid type, expected: map")]
|
||||||
ExpectedMap,
|
ExpectedMap,
|
||||||
|
#[error("serde_v8 error: invalid type, expected: enum")]
|
||||||
ExpectedEnum,
|
ExpectedEnum,
|
||||||
|
#[error("serde_v8 error: invalid type, expected: object")]
|
||||||
ExpectedObject,
|
ExpectedObject,
|
||||||
|
#[error("serde_v8 error: invalid type, expected: buffer")]
|
||||||
ExpectedBuffer,
|
ExpectedBuffer,
|
||||||
|
#[error("serde_v8 error: invalid type, expected: detachable")]
|
||||||
ExpectedDetachable,
|
ExpectedDetachable,
|
||||||
|
#[error("serde_v8 error: invalid type, expected: external")]
|
||||||
ExpectedExternal,
|
ExpectedExternal,
|
||||||
|
#[error("serde_v8 error: invalid type, expected: bigint")]
|
||||||
ExpectedBigInt,
|
ExpectedBigInt,
|
||||||
|
|
||||||
|
#[error("serde_v8 error: invalid type, expected: utf8")]
|
||||||
ExpectedUtf8,
|
ExpectedUtf8,
|
||||||
|
#[error("serde_v8 error: invalid type, expected: latin1")]
|
||||||
ExpectedLatin1,
|
ExpectedLatin1,
|
||||||
|
|
||||||
|
#[error("serde_v8 error: unsupported type")]
|
||||||
UnsupportedType,
|
UnsupportedType,
|
||||||
LengthMismatch,
|
|
||||||
|
#[error("serde_v8 error: length mismatch, got: {0}, expected: {1}")]
|
||||||
|
LengthMismatch(usize, usize),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ser::Error for Error {
|
impl serde::ser::Error for Error {
|
||||||
fn custom<T: Display>(msg: T) -> Self {
|
fn custom<T: Display>(msg: T) -> Self {
|
||||||
Error::Message(msg.to_string())
|
Error::Message(msg.to_string())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl de::Error for Error {
|
impl serde::de::Error for Error {
|
||||||
fn custom<T: Display>(msg: T) -> Self {
|
fn custom<T: Display>(msg: T) -> Self {
|
||||||
Error::Message(msg.to_string())
|
Error::Message(msg.to_string())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for Error {
|
|
||||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
match self {
|
|
||||||
Error::Message(msg) => formatter.write_str(msg),
|
|
||||||
err => formatter.write_str(format!("serde_v8 error: {err:?}").as_ref()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::error::Error for Error {}
|
|
||||||
|
|
|
@ -115,13 +115,13 @@ defail!(
|
||||||
de_tuple_wrong_len_short,
|
de_tuple_wrong_len_short,
|
||||||
(u64, bool, ()),
|
(u64, bool, ()),
|
||||||
"[123, true]",
|
"[123, true]",
|
||||||
|e| e == Err(Error::LengthMismatch)
|
|e| e == Err(Error::LengthMismatch(2, 3))
|
||||||
);
|
);
|
||||||
defail!(
|
defail!(
|
||||||
de_tuple_wrong_len_long,
|
de_tuple_wrong_len_long,
|
||||||
(u64, bool, ()),
|
(u64, bool, ()),
|
||||||
"[123, true, null, 'extra']",
|
"[123, true, null, 'extra']",
|
||||||
|e| e == Err(Error::LengthMismatch)
|
|e| e == Err(Error::LengthMismatch(4, 3))
|
||||||
);
|
);
|
||||||
detest!(
|
detest!(
|
||||||
de_mathop,
|
de_mathop,
|
||||||
|
|
Loading…
Reference in a new issue