1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//! Own errors
use reqwest::header::HeaderMap;
use std::fmt;
/// Possible megalodon errors.
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// ParseError from [`url::ParseError`].
/// This error will be raised when provided URL is invalid.
#[error(transparent)]
ParseError(#[from] url::ParseError),
/// RequestError from [`reqwest::Error`].
/// This error will be raised when the request is invalid or failed to parse the response in reqwest.
#[error(transparent)]
RequestError(#[from] reqwest::Error),
/// StandardError from [`std::io::Error`].
/// This error will be raised when some standard error has occur.
#[error(transparent)]
StandardError(#[from] std::io::Error),
/// WebSocketError from [`tungstenite::error::Error`].
/// This error will be raised when tungstenite WebSocket raises an error.
#[error(transparent)]
WebSocketError(#[from] tokio_tungstenite::tungstenite::error::Error),
/// JsonError from [`serde_json::Error`].
/// This error will be raised when failed to parse some json.
#[error(transparent)]
JsonError(#[from] serde_json::Error),
/// OwnError is megalodon own errors.
#[error(transparent)]
OwnError(#[from] OwnError),
}
/// Megalodon own errors.
#[derive(thiserror::Error)]
#[error("{kind}: {message} {} {}", .url.as_ref().map(AsRef::as_ref).unwrap_or(""), .status.map(|u| u.to_string()).unwrap_or("".to_string()))]
pub struct OwnError {
pub url: Option<String>,
pub status: Option<u16>,
pub message: String,
pub kind: Kind,
/// Headers of the response.
pub header: Option<HeaderMap>,
}
/// Error kind of [`OwnError`].
#[derive(Debug, thiserror::Error)]
pub enum Kind {
/// The implementation is not found.
/// When this error is raised, the method has not yet implemented.
#[error("no implemented error")]
NoImplementedError,
/// Failed to parse something.
#[error("parse error")]
ParseError,
/// The request responds http response with error code.
#[error("http status error")]
HTTPStatusError,
/// The request is not completed error.
#[error("partial content error")]
HTTPPartialContentError,
/// Failed to find nodeinfo.
#[error("nodeinfo error")]
NodeinfoError,
/// The SNS is unknown.
#[error("unknown sns")]
UnknownSNSError,
/// The entity does not satisfy required parameters.
#[error("unsatisfied error")]
UnsatisfiedError,
}
impl Error {
/// Create a new [`OwnError`] struct.
pub fn new_own(
message: String,
kind: Kind,
url: Option<String>,
status: Option<u16>,
header: Option<HeaderMap>,
) -> Error {
Error::OwnError(OwnError {
message,
kind,
url,
status,
header,
})
}
}
impl fmt::Debug for OwnError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut builder = f.debug_struct("megalodon::OwnError");
builder.field("kind", &self.kind);
builder.field("message", &self.message);
if let Some(ref url) = self.url {
builder.field("url", url);
}
if let Some(ref status) = self.status {
builder.field("status", status);
}
if let Some(ref header) = self.header {
builder.field("header", header);
}
builder.finish()
}
}