energy_api/error.rs
1//! Unified error type for the `energy-api` crate.
2
3use thiserror::Error;
4
5/// All errors that can be returned by the energy-api crate.
6#[derive(Debug, Error)]
7pub enum Error {
8 /// The remote service responded with a non-success HTTP status.
9 #[error("HTTP {status}: {body}")]
10 Http {
11 /// HTTP status code (e.g. `400`, `503`).
12 status: u16,
13 /// Response body text.
14 body: String,
15 },
16
17 /// A requested directory record does not exist.
18 #[error("record not found")]
19 NotFound,
20
21 /// JSON serialization or deserialization failure.
22 #[error("JSON error: {0}")]
23 Json(#[from] serde_json::Error),
24
25 /// An invalid URL was supplied or returned by the server.
26 #[error("URL error: {0}")]
27 Url(#[from] url::ParseError),
28
29 /// JWS signature creation or verification failed.
30 #[error("signature error: {0}")]
31 Signature(String),
32
33 /// A directory record was refused because nothing proves it is the
34 /// provider's own (see [`RecordRefusal`]).
35 #[error("directory record refused: {0}")]
36 UnverifiedRecord(RecordRefusal),
37
38 /// Transport-level failure (TLS, DNS, connection reset, …).
39 #[error("transport error: {0}")]
40 Transport(String),
41
42 /// The remote endpoint violated the API protocol.
43 #[error("protocol error: {0}")]
44 Protocol(String),
45
46 /// A BDEW identifier failed validation (length, alphabet or check digit).
47 ///
48 /// Surfaced as HTTP 400: the caller supplied a malformed MaLo-, MeLo-,
49 /// NeLo-, SR- or TR-ID, and accepting it would let a bad identifier enter
50 /// the identification path.
51 #[error("invalid identifier: {0}")]
52 InvalidIdentifier(String),
53}
54
55/// Why a directory record was refused.
56///
57/// Regelungen zum Verzeichnisdienst 1.1, Kap. 2.2 (S. 6): „Die aktuelle Signatur
58/// eines Eintrags MUSS jederzeit anhand eines gültigen Signaturzertifikats der
59/// Klasse EMT.API des entsprechenden Marktpartners überprüft werden können" — the
60/// record is the provider's statement, so a record no trusted key of that
61/// provider signed says nothing about where its API lives.
62#[derive(Debug, Clone, PartialEq, Eq, Error)]
63pub enum RecordRefusal {
64 /// The caller supplied no key for the provider, so no signature could be
65 /// checked.
66 #[error("no trusted signing key for provider {provider_id}")]
67 NoTrustedKey {
68 /// The provider whose record was requested.
69 provider_id: String,
70 },
71 /// The response carried no `X-BDEW-SIGNATURE`, or more than one.
72 #[error("the record carries no single X-BDEW-SIGNATURE")]
73 MissingSignature,
74 /// The signature verifies against none of the trusted keys.
75 #[error("the record signature verifies against no trusted key of provider {provider_id}")]
76 BadSignature {
77 /// The provider whose record was requested.
78 provider_id: String,
79 },
80 /// The record names another entry than the one requested.
81 #[error("requested {requested}, the directory answered {received}")]
82 WrongEntry {
83 /// `providerId/apiId/majorVersion` of the request.
84 requested: String,
85 /// `providerId/apiId/majorVersion` of the record received.
86 received: String,
87 },
88 /// The directory redirected more often than the client follows.
89 #[error("more than {max} redirects, last to {url}")]
90 TooManyRedirects {
91 /// The redirect limit.
92 max: usize,
93 /// The last redirect target.
94 url: String,
95 },
96}
97
98#[cfg(feature = "client")]
99impl From<reqwest::Error> for Error {
100 fn from(e: reqwest::Error) -> Self {
101 if let Some(status) = e.status() {
102 Error::Http {
103 status: status.as_u16(),
104 body: e.to_string(),
105 }
106 } else {
107 Error::Transport(e.to_string())
108 }
109 }
110}
111
112#[cfg(feature = "websocket")]
113impl From<tokio_tungstenite::tungstenite::Error> for Error {
114 fn from(e: tokio_tungstenite::tungstenite::Error) -> Self {
115 Error::Transport(e.to_string())
116 }
117}