symbolic_unreal/
error.rs

1use std::error::Error;
2use std::fmt;
3
4use thiserror::Error;
5
6/// The error type for [`Unreal4Error`].
7#[non_exhaustive]
8#[derive(Clone, Copy, Debug, PartialEq, Eq)]
9pub enum Unreal4ErrorKind {
10    /// Empty data blob received.
11    Empty,
12
13    /// Invalid compressed data.
14    BadCompression,
15
16    /// Invalid contents of the crash file container.
17    BadData,
18
19    /// The crash file contains unexpected trailing data after the footer.
20    TrailingData,
21
22    /// The crash file contents are too large during decompression.
23    TooLarge,
24
25    /// Can't process a log entry.
26    InvalidLogEntry,
27
28    /// Invalid XML.
29    InvalidXml,
30}
31
32impl fmt::Display for Unreal4ErrorKind {
33    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34        match self {
35            Self::Empty => write!(f, "empty crash"),
36            Self::BadCompression => write!(f, "bad compression"),
37            Self::BadData => write!(f, "invalid crash file contents"),
38            Self::TrailingData => write!(f, "unexpected trailing data"),
39            Self::TooLarge => write!(f, "crash file contents are too large"),
40            Self::InvalidLogEntry => write!(f, "invalid log entry"),
41            Self::InvalidXml => write!(f, "invalid xml"),
42        }
43    }
44}
45
46/// An error returned when handling an UE4 crash file.
47#[derive(Debug, Error)]
48#[error("{kind}")]
49pub struct Unreal4Error {
50    kind: Unreal4ErrorKind,
51    #[source]
52    source: Option<Box<dyn Error + Send + Sync + 'static>>,
53}
54
55impl Unreal4Error {
56    /// Creates a new Unreal4 error from a known kind of error as well as an
57    /// arbitrary error payload.
58    pub(crate) fn new<E>(kind: Unreal4ErrorKind, source: E) -> Self
59    where
60        E: Into<Box<dyn Error + Send + Sync>>,
61    {
62        let source = Some(source.into());
63        Self { kind, source }
64    }
65
66    /// Returns the corresponding [`Unreal4ErrorKind`] for this error.
67    pub fn kind(&self) -> Unreal4ErrorKind {
68        self.kind
69    }
70}
71
72impl From<Unreal4ErrorKind> for Unreal4Error {
73    fn from(kind: Unreal4ErrorKind) -> Self {
74        Self { kind, source: None }
75    }
76}
77
78impl From<elementtree::Error> for Unreal4Error {
79    fn from(source: elementtree::Error) -> Self {
80        Self::new(Unreal4ErrorKind::InvalidXml, source)
81    }
82}
83
84impl From<scroll::Error> for Unreal4Error {
85    fn from(source: scroll::Error) -> Self {
86        Self::new(Unreal4ErrorKind::BadData, source)
87    }
88}