2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2023-01-14 23:18:58 -05:00
|
|
|
use std::fmt::Display;
|
2021-10-19 18:00:45 -04:00
|
|
|
|
|
|
|
pub type Result<T> = std::result::Result<T, Error>;
|
|
|
|
|
2023-04-12 20:03:56 -04:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, thiserror::Error)]
|
2022-07-28 06:46:10 -04:00
|
|
|
#[non_exhaustive]
|
2021-10-19 18:00:45 -04:00
|
|
|
pub enum Error {
|
2023-04-12 20:03:56 -04:00
|
|
|
#[error("{0}")]
|
2021-10-19 18:00:45 -04:00
|
|
|
Message(String),
|
|
|
|
|
2023-04-26 20:12:39 -04:00
|
|
|
#[error("serde_v8 error: invalid type; expected: boolean, got: {0}")]
|
|
|
|
ExpectedBoolean(&'static str),
|
|
|
|
|
|
|
|
#[error("serde_v8 error: invalid type; expected: integer, got: {0}")]
|
|
|
|
ExpectedInteger(&'static str),
|
|
|
|
|
|
|
|
#[error("serde_v8 error: invalid type; expected: number, got: {0}")]
|
|
|
|
ExpectedNumber(&'static str),
|
|
|
|
|
|
|
|
#[error("serde_v8 error: invalid type; expected: string, got: {0}")]
|
|
|
|
ExpectedString(&'static str),
|
|
|
|
|
|
|
|
#[error("serde_v8 error: invalid type; expected: array, got: {0}")]
|
|
|
|
ExpectedArray(&'static str),
|
|
|
|
|
|
|
|
#[error("serde_v8 error: invalid type; expected: map, got: {0}")]
|
|
|
|
ExpectedMap(&'static str),
|
|
|
|
|
|
|
|
#[error("serde_v8 error: invalid type; expected: enum, got: {0}")]
|
|
|
|
ExpectedEnum(&'static str),
|
|
|
|
|
|
|
|
#[error("serde_v8 error: invalid type; expected: object, got: {0}")]
|
|
|
|
ExpectedObject(&'static str),
|
|
|
|
|
|
|
|
#[error("serde_v8 error: invalid type; expected: buffer, got: {0}")]
|
|
|
|
ExpectedBuffer(&'static str),
|
|
|
|
|
|
|
|
#[error("serde_v8 error: invalid type; expected: detachable, got: {0}")]
|
|
|
|
ExpectedDetachable(&'static str),
|
|
|
|
|
|
|
|
#[error("serde_v8 error: invalid type; expected: external, got: {0}")]
|
|
|
|
ExpectedExternal(&'static str),
|
|
|
|
|
|
|
|
#[error("serde_v8 error: invalid type; expected: bigint, got: {0}")]
|
|
|
|
ExpectedBigInt(&'static str),
|
2021-10-19 18:00:45 -04:00
|
|
|
|
2023-04-12 20:03:56 -04:00
|
|
|
#[error("serde_v8 error: invalid type, expected: utf8")]
|
2021-10-19 18:00:45 -04:00
|
|
|
ExpectedUtf8,
|
2023-04-12 20:03:56 -04:00
|
|
|
#[error("serde_v8 error: invalid type, expected: latin1")]
|
2022-03-07 05:12:36 -05:00
|
|
|
ExpectedLatin1,
|
2021-10-19 18:00:45 -04:00
|
|
|
|
2023-04-12 20:03:56 -04:00
|
|
|
#[error("serde_v8 error: unsupported type")]
|
2023-03-22 00:13:24 -04:00
|
|
|
UnsupportedType,
|
2023-04-12 20:03:56 -04:00
|
|
|
|
|
|
|
#[error("serde_v8 error: length mismatch, got: {0}, expected: {1}")]
|
|
|
|
LengthMismatch(usize, usize),
|
2023-04-27 14:50:46 -04:00
|
|
|
|
|
|
|
#[error("serde_v8 error: can't create slice from resizable ArrayBuffer")]
|
|
|
|
ResizableBackingStoreNotSupported,
|
2021-10-19 18:00:45 -04:00
|
|
|
}
|
|
|
|
|
2023-04-12 20:03:56 -04:00
|
|
|
impl serde::ser::Error for Error {
|
2021-10-19 18:00:45 -04:00
|
|
|
fn custom<T: Display>(msg: T) -> Self {
|
|
|
|
Error::Message(msg.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-12 20:03:56 -04:00
|
|
|
impl serde::de::Error for Error {
|
2021-10-19 18:00:45 -04:00
|
|
|
fn custom<T: Display>(msg: T) -> Self {
|
|
|
|
Error::Message(msg.to_string())
|
|
|
|
}
|
|
|
|
}
|
2023-04-26 20:12:39 -04:00
|
|
|
|
|
|
|
pub(crate) fn value_to_type_str(value: v8::Local<v8::Value>) -> &'static str {
|
|
|
|
if value.is_module_namespace_object() {
|
|
|
|
"Module"
|
|
|
|
} else if value.is_wasm_module_object() {
|
|
|
|
"WASM module"
|
|
|
|
} else if value.is_wasm_memory_object() {
|
|
|
|
"WASM memory object"
|
|
|
|
} else if value.is_proxy() {
|
|
|
|
"Proxy"
|
|
|
|
} else if value.is_shared_array_buffer() {
|
|
|
|
"SharedArrayBuffer"
|
|
|
|
} else if value.is_data_view() {
|
|
|
|
"DataView"
|
|
|
|
} else if value.is_big_uint64_array() {
|
|
|
|
"BigUint64Array"
|
|
|
|
} else if value.is_big_int64_array() {
|
|
|
|
"BigInt64Array"
|
|
|
|
} else if value.is_float64_array() {
|
|
|
|
"Float64Array"
|
|
|
|
} else if value.is_float32_array() {
|
|
|
|
"Float32Array"
|
|
|
|
} else if value.is_int32_array() {
|
|
|
|
"Int32Array"
|
|
|
|
} else if value.is_uint32_array() {
|
|
|
|
"Uint32Array"
|
|
|
|
} else if value.is_int16_array() {
|
|
|
|
"Int16Array"
|
|
|
|
} else if value.is_uint16_array() {
|
|
|
|
"Uint16Array"
|
|
|
|
} else if value.is_int8_array() {
|
|
|
|
"Int8Array"
|
|
|
|
} else if value.is_uint8_clamped_array() {
|
|
|
|
"Uint8ClampedArray"
|
|
|
|
} else if value.is_uint8_array() {
|
|
|
|
"Uint8Array"
|
|
|
|
} else if value.is_typed_array() {
|
|
|
|
"TypedArray"
|
|
|
|
} else if value.is_array_buffer_view() {
|
|
|
|
"ArrayBufferView"
|
|
|
|
} else if value.is_array_buffer() {
|
|
|
|
"ArrayBuffer"
|
|
|
|
} else if value.is_weak_set() {
|
|
|
|
"WeakSet"
|
|
|
|
} else if value.is_weak_map() {
|
|
|
|
"WeakMap"
|
|
|
|
} else if value.is_set_iterator() {
|
|
|
|
"Set Iterator"
|
|
|
|
} else if value.is_map_iterator() {
|
|
|
|
"Map Iterator"
|
|
|
|
} else if value.is_set() {
|
|
|
|
"Set"
|
|
|
|
} else if value.is_map() {
|
|
|
|
"Map"
|
|
|
|
} else if value.is_promise() {
|
|
|
|
"Promise"
|
|
|
|
} else if value.is_generator_function() {
|
|
|
|
"Generator function"
|
|
|
|
} else if value.is_async_function() {
|
|
|
|
"Async function"
|
|
|
|
} else if value.is_reg_exp() {
|
|
|
|
"RegExp"
|
|
|
|
} else if value.is_date() {
|
|
|
|
"Date"
|
|
|
|
} else if value.is_number() {
|
|
|
|
"Number"
|
|
|
|
} else if value.is_boolean() {
|
|
|
|
"Boolean"
|
|
|
|
} else if value.is_big_int() {
|
|
|
|
"bigint"
|
|
|
|
} else if value.is_array() {
|
|
|
|
"array"
|
|
|
|
} else if value.is_function() {
|
|
|
|
"function"
|
|
|
|
} else if value.is_symbol() {
|
|
|
|
"symbol"
|
|
|
|
} else if value.is_string() {
|
|
|
|
"string"
|
|
|
|
} else if value.is_null() {
|
|
|
|
"null"
|
|
|
|
} else if value.is_undefined() {
|
|
|
|
"undefined"
|
|
|
|
} else {
|
|
|
|
"unknown"
|
|
|
|
}
|
|
|
|
}
|