diff --git a/src/handle/mod.rs b/src/handle/mod.rs index ca6752327..5820c9113 100644 --- a/src/handle/mod.rs +++ b/src/handle/mod.rs @@ -52,7 +52,6 @@ impl<'a, T: Managed + 'a> Handle<'a, T> { pub struct DowncastError { phantom_from: PhantomData, phantom_to: PhantomData, - description: String } impl Debug for DowncastError { @@ -66,22 +65,17 @@ impl DowncastError { DowncastError { phantom_from: PhantomData, phantom_to: PhantomData, - description: format!("failed downcast to {}", T::name()) } } } impl Display for DowncastError { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { - write!(f, "{}", self.to_string()) + write!(f, "failed to downcast {} to {}", F::name(), T::name()) } } -impl Error for DowncastError { - fn description(&self) -> &str { - &self.description - } -} +impl Error for DowncastError {} /// The result of a call to `Handle::downcast()`. pub type DowncastResult<'a, F, T> = Result, DowncastError>; @@ -90,7 +84,7 @@ impl<'a, F: Value, T: Value> JsResultExt<'a, T> for DowncastResult<'a, F, T> { fn or_throw<'b, C: Context<'b>>(self, cx: &mut C) -> JsResult<'a, T> { match self { Ok(v) => Ok(v), - Err(e) => cx.throw_type_error(&e.description) + Err(e) => cx.throw_type_error(&e.to_string()) } } } diff --git a/test/dynamic/lib/functions.js b/test/dynamic/lib/functions.js index 35aa42efa..95ab2bf48 100644 --- a/test/dynamic/lib/functions.js +++ b/test/dynamic/lib/functions.js @@ -60,6 +60,11 @@ describe('JsFunction', function() { assert.throw(addon.unexpected_throw_and_catch, Error, /^internal error in Neon module: try_catch: unexpected Err\(Throw\) when VM is not in a throwing state$/); }) + it('should be able to stringify a downcast error', function () { + let msg = addon.downcast_error(); + assert.strictEqual(msg, "failed to downcast string to number"); + }); + it('computes the right number of arguments', function() { assert.equal(addon.num_arguments(), 0); assert.equal(addon.num_arguments('a'), 1); diff --git a/test/dynamic/native/src/js/functions.rs b/test/dynamic/native/src/js/functions.rs index f7d8a9a94..bc6a0ff6f 100644 --- a/test/dynamic/native/src/js/functions.rs +++ b/test/dynamic/native/src/js/functions.rs @@ -127,3 +127,12 @@ pub fn unexpected_throw_and_catch(mut cx: FunctionContext) -> JsResult Ok(cx.try_catch(|_| { Err(Throw) }) .unwrap_or_else(|err| err)) } + +pub fn downcast_error(mut cx: FunctionContext) -> JsResult { + let s = cx.string("hi"); + if let Err(e) = s.downcast::() { + Ok(cx.string(format!("{}", e))) + } else { + panic!() + } +} diff --git a/test/dynamic/native/src/lib.rs b/test/dynamic/native/src/lib.rs index 770749ddb..3f2c9372d 100644 --- a/test/dynamic/native/src/lib.rs +++ b/test/dynamic/native/src/lib.rs @@ -75,6 +75,7 @@ register_module!(mut cx, { cx.export_function("call_and_catch", call_and_catch)?; cx.export_function("panic_and_catch", panic_and_catch)?; cx.export_function("unexpected_throw_and_catch", unexpected_throw_and_catch)?; + cx.export_function("downcast_error", downcast_error)?; cx.export_class::("Emitter")?; cx.export_class::("TestEmitter")?; diff --git a/test/napi/lib/errors.js b/test/napi/lib/errors.js index d818b9aff..20ed817e3 100644 --- a/test/napi/lib/errors.js +++ b/test/napi/lib/errors.js @@ -33,4 +33,10 @@ describe('errors', function() { assert.throws(() => addon.throw_error(msg), msg); }); + + it('should be able to stringify a downcast error', function () { + let msg = addon.downcast_error(); + assert.strictEqual(msg, "failed to downcast string to number"); + }); + }); diff --git a/test/napi/native/src/js/errors.rs b/test/napi/native/src/js/errors.rs index fbbeaff59..c22e4da87 100644 --- a/test/napi/native/src/js/errors.rs +++ b/test/napi/native/src/js/errors.rs @@ -23,3 +23,12 @@ pub fn throw_error(mut cx: FunctionContext) -> JsResult { cx.throw_error(msg) } + +pub fn downcast_error(mut cx: FunctionContext) -> JsResult { + let s = cx.string("hi"); + if let Err(e) = s.downcast::(&mut cx) { + Ok(cx.string(format!("{}", e))) + } else { + panic!() + } +} diff --git a/test/napi/native/src/lib.rs b/test/napi/native/src/lib.rs index 0b107052f..d11e7a127 100644 --- a/test/napi/native/src/lib.rs +++ b/test/napi/native/src/lib.rs @@ -158,6 +158,7 @@ register_module!(|mut cx| { cx.export_function("new_type_error", new_type_error)?; cx.export_function("new_range_error", new_range_error)?; cx.export_function("throw_error", throw_error)?; + cx.export_function("downcast_error", downcast_error)?; cx.export_function("panic", panic)?; cx.export_function("panic_after_throw", panic_after_throw)?;