Skip to content

Commit

Permalink
feat: Better error handling
Browse files Browse the repository at this point in the history
Instead of capturing errors from the REPL and restarting it (from
main.rs), we now attempt to capture all errors inside the REPL. This
should keep the REPL's state even if something were to go wrong.
  • Loading branch information
lupont committed Nov 5, 2022
1 parent 7f7e114 commit 84c92a2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
8 changes: 3 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ pub use crate::engine::{Engine, ExitStatus};
pub use crate::error::{Error, Result};

fn main() {
loop {
let mut repl = repl::Repl::new();
let mut repl = repl::Repl::new();

if let Err(e) = repl.run() {
eprintln!("rush: Encountered an error: {e}");
}
if let Err(e) = repl.run() {
eprintln!("rush: Unrecoverable error occurred: {e}");
}
}
2 changes: 1 addition & 1 deletion src/repl/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ fn read_line<W: Write>(engine: &mut Engine<W>) -> Result<String> {
execute!(engine.writer, cursor::MoveRight(1))?;
}

(KeyCode::Char(' '), KeyModifiers::NONE) => {
(KeyCode::Char(' '), KeyModifiers::NONE | KeyModifiers::SHIFT) => {
let (mut x, y) = cursor::position()?;

if let Some((expanded_line, diff)) = expand_abbreviation(&line) {
Expand Down
35 changes: 30 additions & 5 deletions src/repl/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub mod input;

use std::env;
use std::io::Stdout;
use std::io::{Stdout, Write};
use std::process;

use crossterm::{execute, style, terminal};
Expand All @@ -26,11 +26,36 @@ impl Repl {

pub fn run(&mut self) -> Result<()> {
loop {
self.prompt()?;

if let Some(command) = input::input(&mut self.engine)? {
self.last_status = Some(self.engine.execute(command)?);
if let Err(e) = self.prompt() {
writeln!(
self.engine.writer,
"rush: Error occurred when computing the prompt: {e}"
)?;
}

match input::input(&mut self.engine) {
Ok(Some(command)) => match self.engine.execute(command) {
Ok(status) => {
self.last_status = Some(status);
}

Err(e) => {
writeln!(
self.engine.writer,
"rush: Error occurred when executing command: {e}"
)?;
}
},

Ok(None) => {}

Err(e) => {
writeln!(
self.engine.writer,
"rush: Error occurred when reading input: {e}"
)?;
}
};
}
}

Expand Down

0 comments on commit 84c92a2

Please sign in to comment.