12 changed files with 324 additions and 163 deletions
-
1Cargo.lock
-
1Cargo.toml
-
188src/client.rs
-
65src/client/error.rs
-
83src/client/error/download_error.rs
-
88src/client/error/request_error.rs
-
9src/client_actor.rs
-
30src/client_actor/error.rs
-
7src/client_actor/message.rs
-
8src/client_actor/util.rs
-
3src/m3u8_download.rs
-
4src/main.rs
@ -1,63 +1,10 @@ |
|||
use std::{error, fmt};
|
|||
mod request_error;
|
|||
mod download_error;
|
|||
|
|||
use http::Response;
|
|||
|
|||
use super::DownloadState;
|
|||
pub(crate) use request_error::ClientRequestError;
|
|||
pub(crate) use download_error::DownloadError;
|
|||
|
|||
|
|||
#[derive(Debug)]
|
|||
pub(super) struct DownloadError {
|
|||
pub(super) state: DownloadState,
|
|||
pub(super) source: anyhow::Error,
|
|||
}
|
|||
|
|||
#[derive(Debug)]
|
|||
pub(super) struct RequestError {
|
|||
pub(super) response: Option<Response<()>>,
|
|||
pub(super) source: Option<anyhow::Error>,
|
|||
}
|
|||
|
|||
|
|||
impl DownloadError {
|
|||
pub(super) fn new( state: DownloadState
|
|||
, source: anyhow::Error ) -> Self {
|
|||
Self { state, source }
|
|||
}
|
|||
}
|
|||
|
|||
impl error::Error for DownloadError {
|
|||
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
|
|||
Some(self.source.as_ref())
|
|||
}
|
|||
}
|
|||
|
|||
impl fmt::Display for DownloadError {
|
|||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|||
write!(f, "download error ({:?}): {}", self.state, self.source)
|
|||
}
|
|||
}
|
|||
|
|||
impl RequestError {
|
|||
pub(super) fn new( response: Option<Response<()>>
|
|||
, source: Option<anyhow::Error> ) -> Self {
|
|||
Self { response, source }
|
|||
}
|
|||
}
|
|||
|
|||
impl error::Error for RequestError {
|
|||
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
|
|||
match &self.source {
|
|||
None => None,
|
|||
Some(e) => Some(e.as_ref()),
|
|||
}
|
|||
}
|
|||
}
|
|||
|
|||
impl fmt::Display for RequestError {
|
|||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|||
match &self.source {
|
|||
None => write!(f, "request error: {:?}", self.response),
|
|||
Some(err) => write!(f, "request error ({:?}): {}", self.response, err),
|
|||
}
|
|||
}
|
|||
}
|
|||
pub(super) type ClientRequestResult<T> = Result<T, ClientRequestError>;
|
|||
pub(super) type DownloadResult<T> = Result<T, DownloadError>;
|
|||
@ -0,0 +1,83 @@ |
|||
use std::{error, fmt, io};
|
|||
|
|||
use tokio::time::error::Elapsed;
|
|||
|
|||
use crate::client::DownloadState;
|
|||
|
|||
use super::ClientRequestError;
|
|||
|
|||
|
|||
#[derive(Debug)]
|
|||
pub(crate) enum DownloadErrorSource {
|
|||
#[allow(dead_code)]
|
|||
Request(ClientRequestError),
|
|||
Timeout(Elapsed),
|
|||
#[allow(dead_code)]
|
|||
IoError(io::Error),
|
|||
#[allow(dead_code)]
|
|||
Other(anyhow::Error),
|
|||
}
|
|||
|
|||
#[derive(Debug)]
|
|||
pub(crate) struct DownloadError {
|
|||
state: DownloadState,
|
|||
source: Option<DownloadErrorSource>,
|
|||
}
|
|||
|
|||
|
|||
impl DownloadError {
|
|||
pub(crate) fn new( state: DownloadState
|
|||
, source: Option<DownloadErrorSource> )
|
|||
-> Self {
|
|||
|
|||
Self { state, source }
|
|||
}
|
|||
|
|||
pub(crate) fn set_state(mut self, state: &DownloadState) -> Self {
|
|||
self.state = state.to_owned();
|
|||
self
|
|||
}
|
|||
}
|
|||
|
|||
impl error::Error for DownloadError {}
|
|||
|
|||
impl fmt::Display for DownloadError {
|
|||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|||
write!( f
|
|||
, "download error: {{ state: {:?} / source: {:?} }}"
|
|||
, self.state
|
|||
, self.source )
|
|||
}
|
|||
}
|
|||
|
|||
impl From<ClientRequestError> for DownloadErrorSource {
|
|||
fn from(value: ClientRequestError) -> Self {
|
|||
Self::Request(value)
|
|||
}
|
|||
}
|
|||
|
|||
impl From<Elapsed> for DownloadErrorSource {
|
|||
fn from(value: Elapsed) -> Self {
|
|||
Self::Timeout(value)
|
|||
}
|
|||
}
|
|||
|
|||
impl From<io::Error> for DownloadErrorSource {
|
|||
fn from(value: io::Error) -> Self {
|
|||
Self::IoError(value)
|
|||
}
|
|||
}
|
|||
|
|||
impl From<Box<dyn error::Error + Send + Sync>> for DownloadErrorSource {
|
|||
fn from(value: Box<dyn error::Error + Send + Sync>) -> Self {
|
|||
Self::Other(anyhow::anyhow!(value))
|
|||
}
|
|||
}
|
|||
|
|||
impl<T: Into<DownloadErrorSource>> From<T> for DownloadError {
|
|||
fn from(value: T) -> Self {
|
|||
let state = DownloadState::None;
|
|||
let source = Some(value.into());
|
|||
Self::new(state, source)
|
|||
}
|
|||
}
|
|||
@ -0,0 +1,88 @@ |
|||
use std::{error, fmt};
|
|||
|
|||
use bytes::Bytes;
|
|||
use http::{request, response};
|
|||
|
|||
|
|||
#[derive(Debug)]
|
|||
pub(crate) struct ClientRequestError {
|
|||
#[allow(dead_code)]
|
|||
request: Option<request::Parts>,
|
|||
response: Option<response::Parts>,
|
|||
#[allow(dead_code)]
|
|||
response_body: Option<Bytes>,
|
|||
source: Option<anyhow::Error>,
|
|||
}
|
|||
|
|||
|
|||
impl ClientRequestError {
|
|||
pub(crate) fn new( request: Option<request::Parts>
|
|||
, response: Option<response::Parts>
|
|||
, response_body: Option<Bytes>
|
|||
, source: Option<anyhow::Error>) -> Self {
|
|||
Self { request, response, response_body, source }
|
|||
}
|
|||
|
|||
#[allow(dead_code)]
|
|||
pub(crate) fn request(&self) -> Option<&request::Parts> {
|
|||
self.request.as_ref()
|
|||
}
|
|||
|
|||
#[allow(dead_code)]
|
|||
pub(crate) fn response(&self) -> Option<&response::Parts> {
|
|||
self.response.as_ref()
|
|||
}
|
|||
|
|||
#[allow(dead_code)]
|
|||
pub(crate) fn response_body(&self) -> Option<&Bytes> {
|
|||
self.response_body.as_ref()
|
|||
}
|
|||
}
|
|||
|
|||
impl error::Error for ClientRequestError {
|
|||
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
|
|||
self.source.as_ref().map(|e| e.as_ref())
|
|||
}
|
|||
}
|
|||
|
|||
impl fmt::Display for ClientRequestError {
|
|||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|||
write!( f
|
|||
, "request error: {{
|
|||
request: {:?} / response: {:?} / source: {:?}
|
|||
}}"
|
|||
, self.request
|
|||
, self.response
|
|||
, self.source )
|
|||
}
|
|||
}
|
|||
|
|||
impl From<http::Error> for ClientRequestError {
|
|||
fn from(value: http::Error) -> Self {
|
|||
let request = None;
|
|||
let response = None;
|
|||
let response_body = None;
|
|||
let source = Some(value.into());
|
|||
Self { request, response, response_body, source }
|
|||
}
|
|||
}
|
|||
|
|||
impl From<anyhow::Error> for ClientRequestError {
|
|||
fn from(value: anyhow::Error) -> Self {
|
|||
let request = None;
|
|||
let response = None;
|
|||
let response_body = None;
|
|||
let source = Some(value);
|
|||
Self { request, response, response_body, source }
|
|||
}
|
|||
}
|
|||
|
|||
impl From<Box<dyn error::Error + Send + Sync>> for ClientRequestError {
|
|||
fn from(value: Box<dyn error::Error + Send + Sync>) -> Self {
|
|||
let request = None;
|
|||
let response = None;
|
|||
let response_body = None;
|
|||
let source = Some(anyhow::anyhow!(value));
|
|||
Self { request, response, response_body, source }
|
|||
}
|
|||
}
|
|||
@ -1,30 +1,44 @@ |
|||
use std::{error, fmt};
|
|||
|
|||
use crate::client::error as client_error;
|
|||
|
|||
use super::message::ClientActorMessageHandle;
|
|||
|
|||
|
|||
#[derive(Debug)]
|
|||
pub(crate) enum ClientActorErrorSource {
|
|||
#[allow(dead_code)]
|
|||
Download(client_error::DownloadError),
|
|||
}
|
|||
|
|||
#[derive(Debug)]
|
|||
pub(crate) struct ClientActorError {
|
|||
pub(super) action: ClientActorMessageHandle,
|
|||
pub(super) source: anyhow::Error,
|
|||
pub(super) source: Option<ClientActorErrorSource>,
|
|||
}
|
|||
|
|||
|
|||
impl From<client_error::DownloadError> for Option<ClientActorErrorSource> {
|
|||
fn from(value: client_error::DownloadError) -> Self {
|
|||
Some(ClientActorErrorSource::Download(value))
|
|||
}
|
|||
}
|
|||
|
|||
impl ClientActorError {
|
|||
pub(super) fn new( action: &ClientActorMessageHandle
|
|||
, source: anyhow::Error ) -> Self {
|
|||
, source: Option<ClientActorErrorSource> ) -> Self {
|
|||
let action = action.to_owned();
|
|||
Self { action, source }
|
|||
}
|
|||
}
|
|||
|
|||
impl error::Error for ClientActorError {
|
|||
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
|
|||
Some(self.source.as_ref())
|
|||
}
|
|||
}
|
|||
impl error::Error for ClientActorError {}
|
|||
|
|||
impl fmt::Display for ClientActorError {
|
|||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|||
write!(f, "download error ({:?}): {}", self.action, self.source)
|
|||
write!( f
|
|||
, "client actor error: {{ action: {:?} / source: {:?} }}"
|
|||
, self.action
|
|||
, self.source )
|
|||
}
|
|||
}
|
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue