Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include stack in RuntimeError #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions ducc/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ pub enum ErrorKind {
code: RuntimeErrorCode,
/// A string representation of the type of error.
name: String,
/// Printable traceback related to the error
stack: Option<String>
},
/// A mutable callback has triggered JavaScript code that has called the same mutable callback
/// again.
Expand Down Expand Up @@ -155,8 +157,12 @@ impl fmt::Display for Error {
ErrorKind::FromJsConversionError { from, to } => {
write!(fmt, "error converting JavaScript {} to {}", from, to)
},
ErrorKind::RuntimeError { ref name, .. } => {
write!(fmt, "JavaScript runtime error ({})", name)
ErrorKind::RuntimeError { ref name, ref stack, .. } => {
let stack = match stack {
Some(s) => s,
None => ""
};
write!(fmt, "JavaScript runtime error ({}): {}", name, stack)
},
ErrorKind::RecursiveMutCallback => write!(fmt, "mutable callback called recursively"),
ErrorKind::NotAFunction => write!(fmt, "tried to a call a non-function"),
Expand Down
13 changes: 13 additions & 0 deletions ducc/src/tests/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use ducc::{Ducc, ExecSettings};
use ffi;
use value::Value;
use util::*;
use error::{Error, ErrorKind};

#[test]
fn test_assert_stack() {
Expand Down Expand Up @@ -82,3 +83,15 @@ fn test_throw_non_object_error() {
assert!(ducc.exec::<Value>("throw undefined", None, ExecSettings::default()).is_err());
assert!(ducc.exec::<Value>("throw null", None, ExecSettings::default()).is_err());
}

#[test]
fn test_pop_error_stack() {
let ducc = Ducc::new();
let stack = match ducc.exec::<Value>("(function woah() { throw new TypeError('nope') })()", Some("file"), ExecSettings::default()) {
Err(Error { kind: ErrorKind::RuntimeError { stack: Some(stack), ..}, .. }) => {
stack
},
_ => panic!()
};
assert!(stack.contains("woah"));
}
11 changes: 10 additions & 1 deletion ducc/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ pub(crate) unsafe fn pop_error(ctx: *mut ffi::duk_context) -> Error {
kind: ErrorKind::RuntimeError {
code: RuntimeErrorCode::Error,
name: "Error".to_string(),
stack: None
},
context: vec![],
};
Expand Down Expand Up @@ -204,6 +205,9 @@ pub(crate) unsafe fn pop_error(ctx: *mut ffi::duk_context) -> Error {
ffi::duk_get_prop_string(ctx, -1, cstr!("message"));
let message = get_string(ctx, -1);
ffi::duk_pop(ctx);
ffi::duk_get_prop_string(ctx, -1, cstr!("stack"));
let stack = get_string(ctx, -1);
ffi::duk_pop(ctx);

let name = match name.is_empty() {
false => name,
Expand All @@ -215,10 +219,15 @@ pub(crate) unsafe fn pop_error(ctx: *mut ffi::duk_context) -> Error {
true => vec![],
};

let stack = match stack.is_empty() {
false => Some(stack),
true => None,
};

ffi::duk_pop(ctx);

Error {
kind: ErrorKind::RuntimeError { code, name },
kind: ErrorKind::RuntimeError { code, name, stack },
context: message,
}
})
Expand Down