libp2prs_core/identity/error.rs
1// Copyright 2019 Parity Technologies (UK) Ltd.
2// Copyright 2020 Netwarps Ltd.
3//
4// Permission is hereby granted, free of charge, to any person obtaining a
5// copy of this software and associated documentation files (the "Software"),
6// to deal in the Software without restriction, including without limitation
7// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8// and/or sell copies of the Software, and to permit persons to whom the
9// Software is furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE.
21
22//! Errors during identity key operations.
23
24use std::error::Error;
25use std::fmt;
26
27/// An error during decoding of key material.
28#[derive(Debug)]
29pub struct DecodingError {
30 msg: String,
31 source: Option<Box<dyn Error + Send + Sync>>,
32}
33
34impl DecodingError {
35 pub(crate) fn new<S: ToString>(msg: S) -> Self {
36 Self {
37 msg: msg.to_string(),
38 source: None,
39 }
40 }
41
42 pub(crate) fn source(self, source: impl Error + Send + Sync + 'static) -> Self {
43 Self {
44 source: Some(Box::new(source)),
45 ..self
46 }
47 }
48}
49
50impl fmt::Display for DecodingError {
51 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
52 write!(f, "Key decoding error: {}", self.msg)
53 }
54}
55
56impl Error for DecodingError {
57 fn source(&self) -> Option<&(dyn Error + 'static)> {
58 self.source.as_ref().map(|s| &**s as &dyn Error)
59 }
60}
61
62/// An error during signing of a message.
63#[derive(Debug)]
64pub struct SigningError {
65 msg: String,
66 source: Option<Box<dyn Error + Send + Sync>>,
67}
68
69/// An error during encoding of key material.
70impl SigningError {
71 pub(crate) fn new<S: ToString>(msg: S) -> Self {
72 Self {
73 msg: msg.to_string(),
74 source: None,
75 }
76 }
77
78 pub(crate) fn source(self, source: impl Error + Send + Sync + 'static) -> Self {
79 Self {
80 source: Some(Box::new(source)),
81 ..self
82 }
83 }
84}
85
86impl fmt::Display for SigningError {
87 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
88 write!(f, "Key signing error: {}", self.msg)
89 }
90}
91
92impl Error for SigningError {
93 fn source(&self) -> Option<&(dyn Error + 'static)> {
94 self.source.as_ref().map(|s| &**s as &dyn Error)
95 }
96}