mirror of
https://github.com/lifegpc/pixiv_downloader.git
synced 2026-07-08 01:32:41 +08:00
Impl Display for downloader error
This commit is contained in:
@@ -1,19 +1,61 @@
|
||||
use crate::downloader::pd_file::PdFileError;
|
||||
use crate::gettext;
|
||||
use http::status::StatusCode;
|
||||
use tokio::time::error::Elapsed;
|
||||
use tokio::task::JoinError;
|
||||
use std::fmt::Display;
|
||||
|
||||
/// File downloader's error
|
||||
#[derive(Debug, derive_more::From)]
|
||||
pub enum DownloaderError {
|
||||
/// Request error
|
||||
ReqwestError(reqwest::Error),
|
||||
/// [PdFileError]
|
||||
PdFileError(PdFileError),
|
||||
/// Io Error
|
||||
IoError(std::io::Error),
|
||||
/// String type message
|
||||
String(String),
|
||||
/// HTTP Error
|
||||
ErrorStatusCode(StatusCode),
|
||||
/// Timed out
|
||||
Timeout(Elapsed),
|
||||
/// Failed to join
|
||||
JoinError(JoinError),
|
||||
}
|
||||
|
||||
impl Display for DownloaderError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::ReqwestError(e) => {
|
||||
f.write_str(gettext("Errors occured when requesting: "))?;
|
||||
e.fmt(f)
|
||||
}
|
||||
Self::PdFileError(e) => {
|
||||
f.write_str(gettext("Errors occured when operating pd file: "))?;
|
||||
e.fmt(f)
|
||||
}
|
||||
Self::IoError(e) => {
|
||||
f.write_str(gettext("Errors occured when operating files: "))?;
|
||||
e.fmt(f)
|
||||
}
|
||||
Self::String(e) => {
|
||||
f.write_str(e)
|
||||
}
|
||||
Self::ErrorStatusCode(e) => {
|
||||
f.write_str("HTTP ERROR ")?;
|
||||
e.fmt(f)
|
||||
}
|
||||
Self::Timeout(e) => {
|
||||
e.fmt(f)
|
||||
}
|
||||
Self::JoinError(e) => {
|
||||
e.fmt(f)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for DownloaderError {
|
||||
fn from(v: &str) -> Self {
|
||||
Self::String(String::from(v))
|
||||
|
||||
@@ -131,6 +131,7 @@ pub async fn handle_download<T: Seek + Write + Send + Sync + ClearFile + GetTarg
|
||||
d.pd.clear()?;
|
||||
if d.enabled_progress_bar() {
|
||||
d.set_progress_bar_position(0);
|
||||
d.set_progress_bar_message(format!("{} {}", gettext("Error when downloading file:"), e));
|
||||
}
|
||||
}
|
||||
return Err(DownloaderError::from(e));
|
||||
@@ -153,6 +154,7 @@ pub async fn handle_download<T: Seek + Write + Send + Sync + ClearFile + GetTarg
|
||||
d.pd.clear()?;
|
||||
if d.enabled_progress_bar() {
|
||||
d.set_progress_bar_position(0);
|
||||
d.set_progress_bar_message(format!("{} {}", gettext("Error when downloading file:"), e));
|
||||
}
|
||||
}
|
||||
return Err(DownloaderError::from(e));
|
||||
|
||||
Reference in New Issue
Block a user