fix: abort by ctrlc unexpectedly prints error message (#49)

This commit is contained in:
sigoden 2023-03-09 11:05:42 +08:00 committed by GitHub
parent a62e461e38
commit c7eb261abc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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 {