Skip to content

Commit

Permalink
Check if GET to URL is success before downloading
Browse files Browse the repository at this point in the history
  • Loading branch information
russellbanks committed Mar 24, 2024
1 parent 5459a79 commit d2be18a
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/download_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::io::Cursor;

use camino::Utf8Path;
use chrono::{DateTime, NaiveDate};
use color_eyre::eyre::{eyre, Result, WrapErr};
use color_eyre::eyre::{bail, eyre, Result};
use futures_util::{stream, StreamExt, TryStreamExt};
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
use itertools::Itertools;
Expand Down Expand Up @@ -40,11 +40,15 @@ async fn download_file(
}
}

let res = client
.get(url.as_str())
.send()
.await
.wrap_err_with(|| format!("Failed to GET from '{url}'"))?;
let res = client.get(url.as_str()).send().await?;

if let Err(err) = res.error_for_status_ref() {
bail!(
"{} returned {}",
err.url().unwrap().as_str(),
err.status().unwrap()
)
}

let content_disposition = res.headers().get(CONTENT_DISPOSITION);
let file_name = get_file_name(&url, res.url(), content_disposition);
Expand Down

0 comments on commit d2be18a

Please sign in to comment.