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.
This commit is contained in:
Max Campbell
2023-06-05 08:19:49 -07:00
committed by Kyle Simpson
parent 9fa063ff0e
commit c976d50cc5

View File

@@ -13,6 +13,7 @@ use reqwest::{
header::{HeaderMap, ACCEPT_RANGES, CONTENT_LENGTH, CONTENT_TYPE, RANGE, RETRY_AFTER}, header::{HeaderMap, ACCEPT_RANGES, CONTENT_LENGTH, CONTENT_TYPE, RANGE, RETRY_AFTER},
Client, Client,
}; };
use std::fmt::format;
use std::{ use std::{
io::{Error as IoError, ErrorKind as IoErrorKind, Result as IoResult, SeekFrom}, io::{Error as IoError, ErrorKind as IoErrorKind, Result as IoResult, SeekFrom},
pin::Pin, pin::Pin,
@@ -82,6 +83,12 @@ impl HttpRequest {
.await .await
.map_err(|e| AudioStreamError::Fail(Box::new(e)))?; .map_err(|e| AudioStreamError::Fail(Box::new(e)))?;
if !resp.status().is_success() {
let msg: Box<dyn std::error::Error + Send + Sync + 'static> =
format!("failed with http status code: {}", resp.status()).into();
return Err(AudioStreamError::Fail(msg));
}
if let Some(t) = resp.headers().get(RETRY_AFTER) { if let Some(t) = resp.headers().get(RETRY_AFTER) {
t.to_str() t.to_str()
.map_err(|_| { .map_err(|_| {