Skip to content

Commit 2492b09

Browse files
authored
Merge pull request #36 from siefkenj/bettererror
Forward error messages when converting to js types
2 parents 9e2203b + 110d3d1 commit 2492b09

File tree

10 files changed

+299
-15
lines changed

10 files changed

+299
-15
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# tsify-next Changelog
22

3+
## v0.5.3
4+
5+
- Propagate errors encountered during serialization.
6+
- More fixes for missing `From` trait implementations.
7+
38
## v0.5.2
49

510
- Fix missing trait bounds for implemented `From` traits.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "tsify-next"
3-
version = "0.5.2"
3+
version = "0.5.3"
44
edition = "2021"
55
authors = [
66
"Madono Haru <[email protected]>",
@@ -14,7 +14,7 @@ keywords = ["wasm", "wasm-bindgen", "typescript"]
1414
categories = ["wasm"]
1515

1616
[dependencies]
17-
tsify-next-macros = { path = "tsify-next-macros", version = "0.5.2" }
17+
tsify-next-macros = { path = "tsify-next-macros", version = "0.5.3" }
1818
wasm-bindgen = { version = "0.2.86", optional = true }
1919
serde = { version = "1.0", optional = true }
2020
serde_json = { version = "1.0", optional = true }

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Click to show Cargo.toml.
1515

1616
```toml
1717
[dependencies]
18-
tsify-next = "0.5.2"
18+
tsify-next = "0.5.3"
1919
serde = { version = "1.0", features = ["derive"] }
2020
wasm-bindgen = { version = "0.2" }
2121
```

tests/expand/borrow.expanded.rs

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,34 @@ const _: () = {
192192
type Abi = <JsType as IntoWasmAbi>::Abi;
193193
#[inline]
194194
fn into_abi(self) -> Self::Abi {
195-
self.into_js().unwrap_throw().into_abi()
195+
match self.into_js() {
196+
Ok(js) => js.into_abi(),
197+
Err(err) => {
198+
let loc = core::panic::Location::caller();
199+
let msg = {
200+
let res = ::alloc::fmt::format(
201+
format_args!(
202+
"(Converting type failed) {0} ({1}:{2}:{3})", err, loc
203+
.file(), loc.line(), loc.column(),
204+
),
205+
);
206+
res
207+
};
208+
{
209+
#[cold]
210+
#[track_caller]
211+
#[inline(never)]
212+
#[rustc_const_panic_str]
213+
#[rustc_do_not_const_check]
214+
const fn panic_cold_display<T: ::core::fmt::Display>(
215+
arg: &T,
216+
) -> ! {
217+
::core::panicking::panic_display(arg)
218+
}
219+
panic_cold_display(&msg);
220+
};
221+
}
222+
}
196223
}
197224
}
198225
impl<'a> OptionIntoWasmAbi for Borrow<'a>
@@ -210,7 +237,34 @@ const _: () = {
210237
{
211238
#[inline]
212239
fn from(value: Borrow<'a>) -> Self {
213-
value.into_js().unwrap_throw().into()
240+
match value.into_js() {
241+
Ok(js) => js.into(),
242+
Err(err) => {
243+
let loc = core::panic::Location::caller();
244+
let msg = {
245+
let res = ::alloc::fmt::format(
246+
format_args!(
247+
"(Converting type failed) {0} ({1}:{2}:{3})", err, loc
248+
.file(), loc.line(), loc.column(),
249+
),
250+
);
251+
res
252+
};
253+
{
254+
#[cold]
255+
#[track_caller]
256+
#[inline(never)]
257+
#[rustc_const_panic_str]
258+
#[rustc_do_not_const_check]
259+
const fn panic_cold_display<T: ::core::fmt::Display>(
260+
arg: &T,
261+
) -> ! {
262+
::core::panicking::panic_display(arg)
263+
}
264+
panic_cold_display(&msg);
265+
};
266+
}
267+
}
214268
}
215269
}
216270
impl<'a> FromWasmAbi for Borrow<'a>

tests/expand/generic_enum.expanded.rs

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,34 @@ const _: () = {
198198
type Abi = <JsType as IntoWasmAbi>::Abi;
199199
#[inline]
200200
fn into_abi(self) -> Self::Abi {
201-
self.into_js().unwrap_throw().into_abi()
201+
match self.into_js() {
202+
Ok(js) => js.into_abi(),
203+
Err(err) => {
204+
let loc = core::panic::Location::caller();
205+
let msg = {
206+
let res = ::alloc::fmt::format(
207+
format_args!(
208+
"(Converting type failed) {0} ({1}:{2}:{3})", err, loc
209+
.file(), loc.line(), loc.column(),
210+
),
211+
);
212+
res
213+
};
214+
{
215+
#[cold]
216+
#[track_caller]
217+
#[inline(never)]
218+
#[rustc_const_panic_str]
219+
#[rustc_do_not_const_check]
220+
const fn panic_cold_display<T: ::core::fmt::Display>(
221+
arg: &T,
222+
) -> ! {
223+
::core::panicking::panic_display(arg)
224+
}
225+
panic_cold_display(&msg);
226+
};
227+
}
228+
}
202229
}
203230
}
204231
impl<T, U> OptionIntoWasmAbi for GenericEnum<T, U>
@@ -216,7 +243,34 @@ const _: () = {
216243
{
217244
#[inline]
218245
fn from(value: GenericEnum<T, U>) -> Self {
219-
value.into_js().unwrap_throw().into()
246+
match value.into_js() {
247+
Ok(js) => js.into(),
248+
Err(err) => {
249+
let loc = core::panic::Location::caller();
250+
let msg = {
251+
let res = ::alloc::fmt::format(
252+
format_args!(
253+
"(Converting type failed) {0} ({1}:{2}:{3})", err, loc
254+
.file(), loc.line(), loc.column(),
255+
),
256+
);
257+
res
258+
};
259+
{
260+
#[cold]
261+
#[track_caller]
262+
#[inline(never)]
263+
#[rustc_const_panic_str]
264+
#[rustc_do_not_const_check]
265+
const fn panic_cold_display<T: ::core::fmt::Display>(
266+
arg: &T,
267+
) -> ! {
268+
::core::panicking::panic_display(arg)
269+
}
270+
panic_cold_display(&msg);
271+
};
272+
}
273+
}
220274
}
221275
}
222276
impl<T, U> FromWasmAbi for GenericEnum<T, U>

tests/expand/generic_struct.expanded.rs

Lines changed: 112 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,34 @@ const _: () = {
197197
type Abi = <JsType as IntoWasmAbi>::Abi;
198198
#[inline]
199199
fn into_abi(self) -> Self::Abi {
200-
self.into_js().unwrap_throw().into_abi()
200+
match self.into_js() {
201+
Ok(js) => js.into_abi(),
202+
Err(err) => {
203+
let loc = core::panic::Location::caller();
204+
let msg = {
205+
let res = ::alloc::fmt::format(
206+
format_args!(
207+
"(Converting type failed) {0} ({1}:{2}:{3})", err, loc
208+
.file(), loc.line(), loc.column(),
209+
),
210+
);
211+
res
212+
};
213+
{
214+
#[cold]
215+
#[track_caller]
216+
#[inline(never)]
217+
#[rustc_const_panic_str]
218+
#[rustc_do_not_const_check]
219+
const fn panic_cold_display<T: ::core::fmt::Display>(
220+
arg: &T,
221+
) -> ! {
222+
::core::panicking::panic_display(arg)
223+
}
224+
panic_cold_display(&msg);
225+
};
226+
}
227+
}
201228
}
202229
}
203230
impl<T> OptionIntoWasmAbi for GenericStruct<T>
@@ -215,7 +242,34 @@ const _: () = {
215242
{
216243
#[inline]
217244
fn from(value: GenericStruct<T>) -> Self {
218-
value.into_js().unwrap_throw().into()
245+
match value.into_js() {
246+
Ok(js) => js.into(),
247+
Err(err) => {
248+
let loc = core::panic::Location::caller();
249+
let msg = {
250+
let res = ::alloc::fmt::format(
251+
format_args!(
252+
"(Converting type failed) {0} ({1}:{2}:{3})", err, loc
253+
.file(), loc.line(), loc.column(),
254+
),
255+
);
256+
res
257+
};
258+
{
259+
#[cold]
260+
#[track_caller]
261+
#[inline(never)]
262+
#[rustc_const_panic_str]
263+
#[rustc_do_not_const_check]
264+
const fn panic_cold_display<T: ::core::fmt::Display>(
265+
arg: &T,
266+
) -> ! {
267+
::core::panicking::panic_display(arg)
268+
}
269+
panic_cold_display(&msg);
270+
};
271+
}
272+
}
219273
}
220274
}
221275
impl<T> FromWasmAbi for GenericStruct<T>
@@ -460,7 +514,34 @@ const _: () = {
460514
type Abi = <JsType as IntoWasmAbi>::Abi;
461515
#[inline]
462516
fn into_abi(self) -> Self::Abi {
463-
self.into_js().unwrap_throw().into_abi()
517+
match self.into_js() {
518+
Ok(js) => js.into_abi(),
519+
Err(err) => {
520+
let loc = core::panic::Location::caller();
521+
let msg = {
522+
let res = ::alloc::fmt::format(
523+
format_args!(
524+
"(Converting type failed) {0} ({1}:{2}:{3})", err, loc
525+
.file(), loc.line(), loc.column(),
526+
),
527+
);
528+
res
529+
};
530+
{
531+
#[cold]
532+
#[track_caller]
533+
#[inline(never)]
534+
#[rustc_const_panic_str]
535+
#[rustc_do_not_const_check]
536+
const fn panic_cold_display<T: ::core::fmt::Display>(
537+
arg: &T,
538+
) -> ! {
539+
::core::panicking::panic_display(arg)
540+
}
541+
panic_cold_display(&msg);
542+
};
543+
}
544+
}
464545
}
465546
}
466547
impl<T> OptionIntoWasmAbi for GenericNewtype<T>
@@ -478,7 +559,34 @@ const _: () = {
478559
{
479560
#[inline]
480561
fn from(value: GenericNewtype<T>) -> Self {
481-
value.into_js().unwrap_throw().into()
562+
match value.into_js() {
563+
Ok(js) => js.into(),
564+
Err(err) => {
565+
let loc = core::panic::Location::caller();
566+
let msg = {
567+
let res = ::alloc::fmt::format(
568+
format_args!(
569+
"(Converting type failed) {0} ({1}:{2}:{3})", err, loc
570+
.file(), loc.line(), loc.column(),
571+
),
572+
);
573+
res
574+
};
575+
{
576+
#[cold]
577+
#[track_caller]
578+
#[inline(never)]
579+
#[rustc_const_panic_str]
580+
#[rustc_do_not_const_check]
581+
const fn panic_cold_display<T: ::core::fmt::Display>(
582+
arg: &T,
583+
) -> ! {
584+
::core::panicking::panic_display(arg)
585+
}
586+
panic_cold_display(&msg);
587+
};
588+
}
589+
}
482590
}
483591
}
484592
impl<T> FromWasmAbi for GenericNewtype<T>

tests/expandtest.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//! Generates expanded code for tests in `tests/expand/` directory.
2+
//! To update the expected output, run with `MACROTEST=overwrite cargo test`
3+
//! or delete the `.expanded.rs` files.
4+
15
#[test]
26
fn expandtest() {
37
macrotest::expand_args("tests/expand/*.rs", ["--features", "tsify-next/json"]);

tests/wasm.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use core::panic;
2+
13
use serde::{Deserialize, Serialize};
24
use tsify_next::Tsify;
35
use wasm_bindgen::prelude::*;
@@ -35,7 +37,9 @@ function validate(value, validation) {
3537
validation(value);
3638
}
3739
38-
module.exports = { validate };
40+
function noop(value) {}
41+
42+
module.exports = { validate, noop };
3943
"#)]
4044
extern "C" {
4145
#[wasm_bindgen(catch, js_name = "validate")]
@@ -49,6 +53,9 @@ extern "C" {
4953
value: SimpleData,
5054
validation: &dyn Fn(&SimpleData),
5155
) -> Result<(), JsValue>;
56+
57+
#[wasm_bindgen(catch, js_name = "noop")]
58+
pub fn do_not_serialize(value: CantBeSerialized) -> Result<(), JsValue>;
5259
}
5360

5461
#[wasm_bindgen_test]
@@ -68,3 +75,29 @@ fn test_convert_simple_value_type() {
6875
})
6976
.unwrap_throw();
7077
}
78+
79+
// Test that the error message encountered during serialization is propagated to the caller
80+
#[derive(Debug, PartialEq, Deserialize, Tsify, Clone)]
81+
#[tsify(into_wasm_abi)]
82+
struct CantBeSerialized {
83+
value: i32,
84+
}
85+
86+
impl Serialize for CantBeSerialized {
87+
fn serialize<S>(&self, _serializer: S) -> Result<S::Ok, S::Error>
88+
where
89+
S: serde::Serializer,
90+
{
91+
Err(serde::ser::Error::custom(
92+
"This type can't be serialized NO_SERIALIZE",
93+
))
94+
}
95+
}
96+
97+
#[wasm_bindgen_test]
98+
#[should_panic(expected = "NO_SERIALIZE")]
99+
fn test_data_that_cant_be_serialized_throws_an_appropriate_error() {
100+
let val = CantBeSerialized { value: 42 };
101+
102+
let _ = do_not_serialize(val);
103+
}

tsify-next-macros/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "tsify-next-macros"
3-
version = "0.5.2"
3+
version = "0.5.3"
44
edition = "2021"
55
authors = [
66
"Madono Haru <[email protected]>",

0 commit comments

Comments
 (0)