From 625f0343cbdc5a7bf11b649b98e239d9dcf53f6e Mon Sep 17 00:00:00 2001 From: sigoden Date: Sun, 5 Mar 2023 16:45:42 +0800 Subject: chore: refactor error encapsulation --- src/client.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'src/client.rs') diff --git a/src/client.rs b/src/client.rs index 3fe75dd..f2aef38 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,7 +1,7 @@ use crate::config::Config; use crate::repl::ReplyReceiver; -use anyhow::{anyhow, Result}; +use anyhow::{anyhow, Context, Result}; use eventsource_stream::Eventsource; use futures_util::StreamExt; use reqwest::{Client, Proxy, RequestBuilder}; @@ -26,13 +26,12 @@ impl ChatGptClient { pub fn init(config: Arc) -> Result { let mut builder = Client::builder(); if let Some(proxy) = config.proxy.as_ref() { - builder = builder - .proxy(Proxy::all(proxy).map_err(|err| anyhow!("Invalid config.proxy, {err}"))?); + builder = builder.proxy(Proxy::all(proxy).with_context(|| "Invalid config.proxy")?); } let client = builder .connect_timeout(CONNECT_TIMEOUT) .build() - .map_err(|err| anyhow!("Failed to init http client, {err}"))?; + .with_context(|| "Failed to init http client")?; let runtime = init_runtime()?; Ok(Self { @@ -181,5 +180,5 @@ fn init_runtime() -> Result { tokio::runtime::Builder::new_current_thread() .enable_all() .build() - .map_err(|err| anyhow!("Failed to init tokio, {err}")) + .with_context(|| "Failed to init tokio") } -- cgit v1.2.3