winbrew_core\network/
error.rs

1use std::error::Error as StdError;
2use std::io;
3
4use reqwest::Error as ReqwestError;
5use thiserror::Error;
6
7pub type BoxError = Box<dyn StdError + Send + Sync + 'static>;
8
9#[derive(Debug, Error)]
10pub enum DownloadError {
11    #[error("failed to build HTTP client")]
12    BuildClient {
13        #[source]
14        source: ReqwestError,
15    },
16
17    #[error("failed to request {label} {url}")]
18    Request {
19        label: String,
20        url: String,
21        #[source]
22        source: ReqwestError,
23    },
24
25    #[error("{label} request failed")]
26    RequestFailed {
27        label: String,
28        #[source]
29        source: ReqwestError,
30    },
31
32    #[error("failed to create {label} download file at {path}")]
33    CreateTempFile {
34        label: String,
35        path: std::path::PathBuf,
36        #[source]
37        source: io::Error,
38    },
39
40    #[error("failed to pre-allocate {label} download file")]
41    Preallocate {
42        label: String,
43        #[source]
44        source: io::Error,
45    },
46
47    #[error("failed to read {label}")]
48    Read {
49        label: String,
50        #[source]
51        source: io::Error,
52    },
53
54    #[error("failed to write {label} to disk")]
55    Write {
56        label: String,
57        #[source]
58        source: io::Error,
59    },
60
61    #[error("failed to finalize {label} download buffer")]
62    FinalizeBuffer {
63        label: String,
64        #[source]
65        source: io::Error,
66    },
67
68    #[error("failed to sync {label} download file")]
69    Sync {
70        label: String,
71        #[source]
72        source: io::Error,
73    },
74
75    #[error("{label} size mismatch: expected {expected}, got {actual}")]
76    SizeMismatch {
77        label: String,
78        expected: u64,
79        actual: u64,
80    },
81
82    #[error("download callback failed")]
83    ChunkCallback {
84        #[from]
85        #[source]
86        source: BoxError,
87    },
88}
89
90pub type Result<T> = std::result::Result<T, DownloadError>;
91
92impl DownloadError {
93    pub(crate) fn build_client(source: ReqwestError) -> Self {
94        Self::BuildClient { source }
95    }
96
97    pub(crate) fn request(
98        label: impl Into<String>,
99        url: impl Into<String>,
100        source: ReqwestError,
101    ) -> Self {
102        Self::Request {
103            label: label.into(),
104            url: url.into(),
105            source,
106        }
107    }
108
109    pub(crate) fn request_failed(label: impl Into<String>, source: ReqwestError) -> Self {
110        Self::RequestFailed {
111            label: label.into(),
112            source,
113        }
114    }
115
116    pub(crate) fn create_temp_file(
117        label: impl Into<String>,
118        path: std::path::PathBuf,
119        source: io::Error,
120    ) -> Self {
121        Self::CreateTempFile {
122            label: label.into(),
123            path,
124            source,
125        }
126    }
127
128    pub(crate) fn preallocate(label: impl Into<String>, source: io::Error) -> Self {
129        Self::Preallocate {
130            label: label.into(),
131            source,
132        }
133    }
134
135    pub(crate) fn read(label: impl Into<String>, source: io::Error) -> Self {
136        Self::Read {
137            label: label.into(),
138            source,
139        }
140    }
141
142    pub(crate) fn write(label: impl Into<String>, source: io::Error) -> Self {
143        Self::Write {
144            label: label.into(),
145            source,
146        }
147    }
148
149    pub(crate) fn finalize_buffer(label: impl Into<String>, source: io::Error) -> Self {
150        Self::FinalizeBuffer {
151            label: label.into(),
152            source,
153        }
154    }
155
156    pub(crate) fn sync(label: impl Into<String>, source: io::Error) -> Self {
157        Self::Sync {
158            label: label.into(),
159            source,
160        }
161    }
162
163    pub(crate) fn size_mismatch(label: impl Into<String>, expected: u64, actual: u64) -> Self {
164        Self::SizeMismatch {
165            label: label.into(),
166            expected,
167            actual,
168        }
169    }
170}