From c204b692253d9bab18292495d68de2391fb73cf2 Mon Sep 17 00:00:00 2001 From: Bulat Kurbanov Date: Mon, 12 Dec 2022 19:09:23 +0100 Subject: [PATCH] Fix 204 status code when download --- .../approved_bot/services/book_cache/mod.rs | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/bots/approved_bot/services/book_cache/mod.rs b/src/bots/approved_bot/services/book_cache/mod.rs index bccd54e..02226bf 100644 --- a/src/bots/approved_bot/services/book_cache/mod.rs +++ b/src/bots/approved_bot/services/book_cache/mod.rs @@ -1,9 +1,25 @@ +use std::fmt; +use reqwest::StatusCode; + use crate::{bots::approved_bot::modules::download::DownloadData, config}; use self::types::{CachedMessage, DownloadFile}; pub mod types; +#[derive(Debug, Clone)] +struct DownloadError { + status_code: StatusCode +} + +impl fmt::Display for DownloadError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "Status code is {0}", self.status_code) + } +} + +impl std::error::Error for DownloadError {} + pub async fn get_cached_message( download_data: &DownloadData, ) -> Result> { @@ -83,6 +99,12 @@ pub async fn download_file( match response.error_for_status() { Ok(response) => { + if response.status() != StatusCode::OK { + return Err(Box::new(DownloadError { + status_code: response.status() + })); + } + let headers = response.headers(); let filename = headers .get("content-disposition")