diff options
| author | sigoden <sigoden@gmail.com> | 2023-03-05 16:45:42 +0800 |
|---|---|---|
| committer | sigoden <sigoden@gmail.com> | 2023-03-05 16:45:42 +0800 |
| commit | 625f0343cbdc5a7bf11b649b98e239d9dcf53f6e (patch) | |
| tree | 8fffbe1a36cbea637308f6d65cef8fde45fa0ee3 /src/repl.rs | |
| parent | bec8787a57821fd1fcc06b493f44e155fd7a7e84 (diff) | |
| download | aichat-625f0343cbdc5a7bf11b649b98e239d9dcf53f6e.tar.gz | |
chore: refactor error encapsulation
Diffstat (limited to 'src/repl.rs')
| -rw-r--r-- | src/repl.rs | 14 |
1 files changed, 6 insertions, 8 deletions
diff --git a/src/repl.rs b/src/repl.rs index cd9e990..2a5947f 100644 --- a/src/repl.rs +++ b/src/repl.rs @@ -3,7 +3,7 @@ use crate::config::{Config, Role}; use crate::render::{self, MarkdownRender}; use crate::term; use crate::utils::{copy, dump}; -use anyhow::{anyhow, Result}; +use anyhow::{Context, Result}; use crossbeam::channel::{unbounded, Sender}; use crossbeam::sync::WaitGroup; use reedline::{ @@ -69,8 +69,7 @@ impl Repl { handler.ctrlc.store(false, Ordering::SeqCst); current_ctrlc = true } - let sig = self.editor.read_line(&self.prompt); - match sig { + match self.editor.read_line(&self.prompt) { Ok(Signal::Success(line)) => { current_ctrlc = false; match self.handle_line(handler.clone(), line) { @@ -80,7 +79,8 @@ impl Repl { } } Err(err) => { - dump(format!("{err:?}"), 1); + let err = format!("{err:?}"); + dump(err.trim(), 2); } } } @@ -118,9 +118,7 @@ impl Repl { Some("screen") => term::clear_screen(0)?, Some("history") => { let history = Box::new(self.editor.history_mut()); - history - .clear() - .map_err(|err| anyhow!("Failed to clear history, {err}"))?; + history.clear().with_context(|| "Failed to clear history")?; dump("", 1); } Some("role") => handler.handle(ReplCmd::ClearRole)?, @@ -205,7 +203,7 @@ impl Repl { fn create_history() -> Result<Box<FileBackedHistory>> { Ok(Box::new( FileBackedHistory::with_file(1000, Config::history_file()?) - .map_err(|err| anyhow!("Failed to setup history file, {err}"))?, + .with_context(|| "Failed to setup history file")?, )) } } |
