| 1 |
use alloc::{ |
| 2 |
format, |
| 3 |
str::Utf8Error, |
| 4 |
string::{FromUtf8Error, String, ToString}, |
| 5 |
}; |
| 6 |
use core::{fmt::Display, num::ParseIntError}; |
| 7 |
|
| 8 |
#[cfg(feature = "std")] |
| 9 |
use std::sync::Arc; |
| 10 |
|
| 11 |
use thiserror::Error; |
| 12 |
|
| 13 |
use crate::state_tracker; |
| 14 |
|
| 15 |
#[derive(Debug, Clone)] |
| 16 |
pub struct Error { |
| 17 |
context: Option<String>, |
| 18 |
kind: ErrorKind, |
| 19 |
} |
| 20 |
|
| 21 |
impl core::fmt::Display for Error { |
| 22 |
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 23 |
write!(f, "{}", self.kind)?; |
| 24 |
if let Some(c) = &self.context { |
| 25 |
write!(f, "\nin context:\n\t{}", c)?; |
| 26 |
} |
| 27 |
Ok(()) |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
impl core::error::Error for Error { |
| 32 |
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> { |
| 33 |
self.kind.source() |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
// An enumeration of potential errors that appear during bencode deserialization. |
| 38 |
#[derive(Debug, Clone, Error)] |
| 39 |
#[non_exhaustive] |
| 40 |
pub enum ErrorKind { |
| 41 |
/// Error that occurs if the serialized structure contains invalid semantics. |
| 42 |
#[cfg(feature = "std")] |
| 43 |
#[error("malformed content discovered: {source}")] |
| 44 |
MalformedContent { |
| 45 |
#[source] |
| 46 |
source: Arc<dyn std::error::Error + Send + Sync>, |
| 47 |
}, |
| 48 |
|
| 49 |
/// Error that occurs if the serialized structure contains invalid semantics. |
| 50 |
#[cfg(not(feature = "std"))] |
| 51 |
#[error("malformed content discovered")] |
| 52 |
MalformedContent, |
| 53 |
|
| 54 |
/// Error that occurs if the serialized structure is incomplete. |
| 55 |
#[error("missing field: {field}")] |
| 56 |
MissingField { field: String }, |
| 57 |
|
| 58 |
/// Error in the bencode structure (e.g. a missing field and seperator). |
| 59 |
#[error("bencode encoding corrupted ({source})")] |
| 60 |
StructureError { |
| 61 |
source: state_tracker::StructureError, |
| 62 |
}, |
| 63 |
|
| 64 |
/// Error that occurs if the serialized structure contains an unexpected field. |
| 65 |
#[error("unexpected field: {field}")] |
| 66 |
UnexpectedField { field: String }, |
| 67 |
|
| 68 |
/// Error through an unexpected bencode token during deserialization. |
| 69 |
#[error("discovered {discovered} but expected {expected}")] |
| 70 |
UnexpectedToken { |
| 71 |
expected: String, |
| 72 |
discovered: String, |
| 73 |
}, |
| 74 |
} |
| 75 |
|
| 76 |
pub trait ResultExt { |
| 77 |
fn context(self, context: impl Display) -> Self; |
| 78 |
} |
| 79 |
|
| 80 |
impl Error { |
| 81 |
pub fn context(mut self, context: impl Display) -> Self { |
| 82 |
if let Some(current) = self.context.as_mut() { |
| 83 |
*current = format!("{current}\n\t{context}"); |
| 84 |
} else { |
| 85 |
self.context = Some(context.to_string()); |
| 86 |
} |
| 87 |
|
| 88 |
self |
| 89 |
} |
| 90 |
|
| 91 |
/// Raised when there is a general error while deserializing a type. |
| 92 |
/// The message should not be capitalized and should not end with a period. |
| 93 |
#[cfg(feature = "std")] |
| 94 |
pub fn malformed_content<SourceT>(source: SourceT) -> Self |
| 95 |
where |
| 96 |
SourceT: std::error::Error + Send + Sync + 'static, |
| 97 |
{ |
| 98 |
let error = Arc::new(source); |
| 99 |
ErrorKind::MalformedContent { source: error }.into() |
| 100 |
} |
| 101 |
|
| 102 |
/// Raised when there is a general error while deserializing a type. |
| 103 |
/// The message should not be capitalized and should not end with a period. |
| 104 |
#[cfg(not(feature = "std"))] |
| 105 |
pub fn malformed_content<T>(_cause: T) -> Self { |
| 106 |
Self::from(ErrorKind::MalformedContent) |
| 107 |
} |
| 108 |
|
| 109 |
/// Returns a `Error::MissingField` which contains the name of the field. |
| 110 |
pub fn missing_field(field_name: impl Display) -> Self { |
| 111 |
Error::from(ErrorKind::MissingField { |
| 112 |
field: field_name.to_string(), |
| 113 |
}) |
| 114 |
} |
| 115 |
|
| 116 |
/// Returns a `Error::UnexpectedField` which contains the name of the field. |
| 117 |
pub fn unexpected_field(field_name: impl Display) -> Self { |
| 118 |
Error::from(ErrorKind::UnexpectedField { |
| 119 |
field: field_name.to_string(), |
| 120 |
}) |
| 121 |
} |
| 122 |
|
| 123 |
/// Returns a `Error::UnexpectedToken` which contains a custom error message. |
| 124 |
pub fn unexpected_token(expected: impl Display, discovered: impl Display) -> Self { |
| 125 |
Error::from(ErrorKind::UnexpectedToken { |
| 126 |
expected: expected.to_string(), |
| 127 |
discovered: discovered.to_string(), |
| 128 |
}) |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
impl From<ErrorKind> for Error { |
| 133 |
fn from(kind: ErrorKind) -> Self { |
| 134 |
Self { |
| 135 |
context: None, |
| 136 |
kind, |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
impl From<state_tracker::StructureError> for Error { |
| 142 |
fn from(error: state_tracker::StructureError) -> Self { |
| 143 |
Self::from(ErrorKind::StructureError { source: error }) |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
impl From<FromUtf8Error> for Error { |
| 148 |
fn from(err: FromUtf8Error) -> Self { |
| 149 |
Self::malformed_content(err) |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
impl From<Utf8Error> for Error { |
| 154 |
fn from(err: Utf8Error) -> Self { |
| 155 |
Self::malformed_content(err) |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
impl From<ParseIntError> for Error { |
| 160 |
fn from(err: ParseIntError) -> Self { |
| 161 |
Self::malformed_content(err) |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
impl<T> ResultExt for Result<T, Error> { |
| 166 |
fn context(self, context: impl Display) -> Self { |
| 167 |
self.map_err(|err| err.context(context)) |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
#[test] |
| 172 |
fn decoding_errors_are_sync_send() { |
| 173 |
use crate::decoding::error::{Error, ErrorKind}; |
| 174 |
fn is_send<T: Send>() {} |
| 175 |
fn is_sync<T: Sync>() {} |
| 176 |
is_send::<Error>(); |
| 177 |
is_send::<ErrorKind>(); |
| 178 |
is_sync::<Error>(); |
| 179 |
is_sync::<ErrorKind>(); |
| 180 |
} |
| 181 |
|
| 182 |
#[test] |
| •183 |
#[cfg(feature = "std")] |
| 184 |
fn error_context_is_displayed() { |
| 185 |
let e:Result<(), Error> = Err(Error::missing_field("aaa")).context("bbb").context("ccc"); |
| 186 |
let e = e.unwrap_err().to_string(); |
| 187 |
println!("{}", &e); |
| 188 |
assert!(e.contains("aaa\n")); |
| 189 |
assert!(e.contains("in context:\n")); |
| 190 |
assert!(e.contains("bbb\n")); |
| 191 |
assert!(e.contains("ccc")); |
| 192 |
} |