From c976d50cc5bf4af2c3d3a567e65634728cd0d81c Mon Sep 17 00:00:00 2001 From: Max Campbell <41460735+maxall41@users.noreply.github.com> Date: Mon, 5 Jun 2023 08:19:49 -0700 Subject: [PATCH] Input: Add HTTP Status Code Checks (#190) `HttpRequest`s will now return an `AudioStreamError::Fail` on receipt of a non-2xx status code from a server. This has the advantage of making it clearer *why* a failure occurred rather than leaving users to piece the truth together from a Symphonia parsing error. Closes #184. --- src/input/sources/http.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/input/sources/http.rs b/src/input/sources/http.rs index e03a323..efcec23 100644 --- a/src/input/sources/http.rs +++ b/src/input/sources/http.rs @@ -13,6 +13,7 @@ use reqwest::{ header::{HeaderMap, ACCEPT_RANGES, CONTENT_LENGTH, CONTENT_TYPE, RANGE, RETRY_AFTER}, Client, }; +use std::fmt::format; use std::{ io::{Error as IoError, ErrorKind as IoErrorKind, Result as IoResult, SeekFrom}, pin::Pin, @@ -82,6 +83,12 @@ impl HttpRequest { .await .map_err(|e| AudioStreamError::Fail(Box::new(e)))?; + if !resp.status().is_success() { + let msg: Box = + format!("failed with http status code: {}", resp.status()).into(); + return Err(AudioStreamError::Fail(msg)); + } + if let Some(t) = resp.headers().get(RETRY_AFTER) { t.to_str() .map_err(|_| {