Skip to content

Commit

Permalink
feat: Unpack errors received from macros if they are standard gluon e…
Browse files Browse the repository at this point in the history
…rrors
  • Loading branch information
Marwes committed Aug 14, 2016
1 parent 3f1ae8f commit c76760a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn print(s: RootStr) -> IO<()> {

struct GluonFile(Mutex<File>);

impl Userdata for GluonFile { }
impl Userdata for GluonFile {}

impl fmt::Debug for GluonFile {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down
41 changes: 40 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use vm::Error as VmError;
use vm::compiler::CompiledFunction;
use vm::thread::{RootedValue, ThreadInternal};
use vm::internal::ClosureDataDef;
use vm::macros;

quick_error! {
/// Error type wrapping all possible errors that can be generated from gluon
Expand Down Expand Up @@ -67,11 +68,49 @@ quick_error! {
from()
}
/// Error found when expanding macros
Macro(err: Errors<::vm::macros::Error>) {
Macro(err: macros::Error) {
description(err.description())
display("{}", err)
from()
}
/// Multiple errors where found
Multiple(err: Errors<Error>) {
description(err.description())
display("{}", err)
}
}
}

impl From<Errors<macros::Error>> for Error {
fn from(mut errors: Errors<macros::Error>) -> Error {
if errors.errors.len() == 1 {
let err = errors.errors.pop().unwrap();
match err.downcast::<Error>() {
Ok(err) => *err,
Err(err) => Error::Macro(err),
}
} else {
Error::Multiple(Errors {
errors: errors.errors
.into_iter()
.map(|err| match err.downcast::<Error>() {
Ok(err) => *err,
Err(err) => Error::Macro(err),
})
.collect(),
})
}
}
}


impl From<Errors<Error>> for Error {
fn from(mut errors: Errors<Error>) -> Error {
if errors.errors.len() == 1 {
errors.errors.pop().unwrap()
} else {
Error::Multiple(errors)
}
}
}

Expand Down
7 changes: 0 additions & 7 deletions tests/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -769,13 +769,6 @@ fn out_of_memory() {
match result {
// FIXME This should just need to match on the explicit out of memory error
Err(Error::VM(VMError::OutOfMemory { limit: 10, .. })) => (),
Err(Error::VM(VMError::Message(msg))) => assert!(msg.starts_with("Thread is out of memory: Limit 10"), "{}", msg),
Err(Error::Macro(errors)) => {
match *errors.errors[0].downcast_ref::<Error>().unwrap() {
Error::VM(VMError::OutOfMemory { limit: 10, .. }) => (),
_ => panic!("Unexpected error {:?}", errors.errors[0]),
}
}
Err(err) => panic!("Unexpected error `{:?}`", err),
Ok(_) => panic!("Expected an error"),
}
Expand Down

0 comments on commit c76760a

Please sign in to comment.