Skip to content

Commit

Permalink
Input: Add HTTP Status Code Checks (#190)
Browse files Browse the repository at this point in the history
`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.
  • Loading branch information
maxall41 authored and FelixMcFelix committed Nov 20, 2023
1 parent 9fa063f commit c976d50
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/input/sources/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -82,6 +83,12 @@ impl HttpRequest {
.await
.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) {
t.to_str()
.map_err(|_| {
Expand Down

0 comments on commit c976d50

Please sign in to comment.