mirror of
https://github.com/sigoden/aichat
synced 2024-11-18 09:28:27 +00:00
fix: abort by ctrlc unexpectedly prints error message (#49)
This commit is contained in:
parent
a62e461e38
commit
c7eb261abc
@ -123,8 +123,10 @@ impl ReplyStreamHandler {
|
||||
pub fn text(&mut self, text: &str) -> Result<()> {
|
||||
match self.sender.as_ref() {
|
||||
Some(tx) => {
|
||||
tx.send(ReplyStreamEvent::Text(text.to_string()))
|
||||
.with_context(|| "Failed to send StreamEvent:Text")?;
|
||||
let ret = tx
|
||||
.send(ReplyStreamEvent::Text(text.to_string()))
|
||||
.with_context(|| "Failed to send StreamEvent:Text");
|
||||
self.safe_ret(ret)?;
|
||||
}
|
||||
None => {
|
||||
print_now!("{}", text);
|
||||
@ -137,8 +139,10 @@ impl ReplyStreamHandler {
|
||||
pub fn done(&mut self) -> Result<()> {
|
||||
match self.sender.as_ref() {
|
||||
Some(tx) => {
|
||||
tx.send(ReplyStreamEvent::Done)
|
||||
.with_context(|| "Failed to send StreamEvent:Done")?;
|
||||
let ret = tx
|
||||
.send(ReplyStreamEvent::Done)
|
||||
.with_context(|| "Failed to send StreamEvent:Done");
|
||||
self.safe_ret(ret)?;
|
||||
}
|
||||
None => {
|
||||
if !self.buffer.ends_with('\n') {
|
||||
@ -162,6 +166,13 @@ impl ReplyStreamHandler {
|
||||
pub fn get_abort(&self) -> SharedAbortSignal {
|
||||
self.abort.clone()
|
||||
}
|
||||
|
||||
fn safe_ret(&self, ret: Result<()>) -> Result<()> {
|
||||
if ret.is_err() && self.abort.aborted() {
|
||||
return Ok(());
|
||||
}
|
||||
ret
|
||||
}
|
||||
}
|
||||
|
||||
pub enum ReplyStreamEvent {
|
||||
|
Loading…
Reference in New Issue
Block a user