modio/util/upload/
error.rs1use std::fmt;
2
3type Source = Box<dyn std::error::Error + Send + Sync>;
4
5pub struct Error {
6 kind: ErrorKind,
7 source: Option<Source>,
8}
9
10impl Error {
11 pub const fn kind(&self) -> &ErrorKind {
12 &self.kind
13 }
14}
15
16impl Error {
17 #[inline]
18 pub(crate) fn request<E: Into<Source>>(source: E) -> Self {
19 Self {
20 kind: ErrorKind::Request,
21 source: Some(source.into()),
22 }
23 }
24
25 #[inline]
26 pub(crate) fn body<E: Into<Source>>(source: E) -> Self {
27 Self {
28 kind: ErrorKind::Body,
29 source: Some(source.into()),
30 }
31 }
32}
33
34impl fmt::Debug for Error {
35 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36 let mut s = f.debug_struct("Error");
37
38 s.field("kind", &self.kind);
39 if let Some(ref source) = self.source {
40 s.field("source", source);
41 }
42 s.finish()
43 }
44}
45
46impl fmt::Display for Error {
47 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48 match self.kind {
49 ErrorKind::Request => f.write_str("request failed"),
50 ErrorKind::Body => f.write_str("failed to load response body"),
51 }
52 }
53}
54
55impl std::error::Error for Error {
56 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
57 self.source.as_ref().map(|e| &**e as _)
58 }
59}
60
61#[derive(Debug)]
62#[non_exhaustive]
63pub enum ErrorKind {
64 Request,
65 Body,
66}