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