I need to translate my error types (enums) into Throws.
How should this be done with Throw being an empty struct?
For example I'd expect to be able to do something like:
impl From<::errors::ReadError> for neon::vm::Throw {
fn from(e: ::errors::ReadError) -> Self {
neon::vm::Throw(e.message)
// or, neon::vm::Throw(e.some_trait_obj)
}
}
I haven't found an example of this in the wild thusfar.
@Phrohdoh Hi Taryn, have you made any progress on this?
I want to use serde_json with Neon and as you I also need to figure out how to implement From
use neon::vm::{ Throw };
use serde_json::{ Error };
struct CustomThrow(Throw);
impl From<Error> for CustomThrow {
fn from(error: Error) -> Self {
Self.0
}
}
Sorry @hunterlester, I have not. I stopped using Neon not too long after opening this ticket.
Why do you need Throw for that and not JsError?
@RReverser The compiler is asking me to impl for Throw:
error[E0277]: the trait bound `neon::vm::Throw: std::convert::From<serde_json::Error>` is not satisfied
--> src\lib.rs:23:25
|
23 | let app_info: Value = serde_json::from_str(&app_info_string)?;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::convert::From<serde_json::Error>` is not implemented for `neon::vm::Throw`
|
= note: required by `std::convert::From::from`
@hunterlester Ah it's because you're using ? which tries to return serde error as a Throw (if you're inside of method that returns JsResult).
Instead, I think you can use e.g. JsError::throw in the error part which will return VmResult directly, and then use can use ? on top of it, like:
serde_json::from_str(&app_info_string).or_else(|e| JsError::throw(...your preferred kind and message...))?
Can't check if this actually works at the moment, but I think it should.
Similarly, you can wrap JsError::throw into the From / Into trait implementation to use ? directly on serde results, but then you will likely need to use .unwrap_err() to unwrap VmResult back into a Throw, which can be later wrapped back into VmResult by ? operator.
For anyone else finding this thread with the same issue, please know that the solution explained above by @RReverser worked perfectly.
This issue can be closed now.
Most helpful comment
@hunterlester Ah it's because you're using
?which tries to return serde error as aThrow(if you're inside of method that returnsJsResult).Instead, I think you can use e.g.
JsError::throwin the error part which will returnVmResultdirectly, and then use can use?on top of it, like:Can't check if this actually works at the moment, but I think it should.
Similarly, you can wrap
JsError::throwinto theFrom/Intotrait implementation to use?directly on serde results, but then you will likely need to use.unwrap_err()to unwrapVmResultback into aThrow, which can be later wrapped back intoVmResultby?operator.