Skip to content

Commit

Permalink
Better error formatting and cli color (#586)
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat authored Jul 23, 2020
1 parent bbd7dd2 commit 795adc5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions boa/src/builtins/value/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,14 @@ pub(crate) fn display_obj(v: &Value, print_internals: bool) -> String {
// in-memory address in this set
let mut encounters = HashSet::new();

if let Value::Object(object) = v {
if object.borrow().is_error() {
let name = v.get_field("name");
let message = v.get_field("message");
return format!("{}: {}", name, message);
}
}

fn display_obj_internal(
data: &Value,
encounters: &mut HashSet<usize>,
Expand Down
1 change: 1 addition & 0 deletions boa_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Boa = { path = "../boa", features = ["serde"] }
rustyline = "6.2.0"
structopt = "0.3.15"
serde_json = "1.0.56"
colored = "2.0.0"

[target.x86_64-unknown-linux-gnu.dependencies]
jemallocator = "0.3.2"
Expand Down
7 changes: 5 additions & 2 deletions boa_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use boa::{
realm::Realm,
syntax::ast::{node::StatementList, token::Token},
};
use colored::*;
use rustyline::{config::Config, error::ReadlineError, EditMode, Editor};
use std::{fs::read_to_string, path::PathBuf};
use structopt::{clap::arg_enum, StructOpt};
Expand Down Expand Up @@ -208,8 +209,10 @@ pub fn main() -> Result<(), std::io::Error> {
let mut editor = Editor::<()>::with_config(config);
let _ = editor.load_history(CLI_HISTORY);

let readline = "> ".cyan().bold().to_string();

loop {
match editor.readline("> ") {
match editor.readline(&readline) {
Ok(line) if line == ".exit" => break,
Err(ReadlineError::Interrupted) | Err(ReadlineError::Eof) => break,

Expand All @@ -223,7 +226,7 @@ pub fn main() -> Result<(), std::io::Error> {
} else {
match forward_val(&mut engine, line.trim_end()) {
Ok(v) => println!("{}", v),
Err(v) => eprintln!("{}", v),
Err(v) => eprintln!("{}: {}", "Uncaught".red(), v.to_string().red()),
}
}
}
Expand Down

0 comments on commit 795adc5

Please sign in to comment.