Skip to main content

intel_crashlog/
error.rs

1// Copyright (C) 2025 Intel Corporation
2// SPDX-License-Identifier: MIT
3
4#[cfg(feature = "collateral_manager")]
5use crate::{
6    collateral::{ItemPath, PVSS},
7    header::Version,
8};
9#[cfg(not(feature = "std"))]
10use alloc::{fmt, str};
11#[cfg(not(feature = "std"))]
12use core::num;
13#[cfg(feature = "std")]
14use std::{fmt, io, num, str};
15
16/// Errors reported by the Crash Log extraction and decoding functions.
17#[derive(Debug)]
18pub enum Error {
19    Unsupported,
20    InternalError,
21    InvalidCrashLog,
22    NoCrashLogFound,
23    NoCrashLogSourceFound,
24    #[cfg(feature = "collateral_manager")]
25    MissingCollateral(PVSS, ItemPath),
26    #[cfg(feature = "collateral_manager")]
27    MissingDecodeDefinitions(Version),
28    InvalidBootErrorRecordRegion,
29    InvalidHeader,
30    EmptyRegion,
31    InvalidHeaderType(u16),
32    InvalidRecordType(u8),
33    InvalidProductID(u32),
34    #[cfg(feature = "serialize")]
35    JsonError(serde_json::Error),
36    Utf8Error(str::Utf8Error),
37    ParseIntError(num::ParseIntError),
38    #[cfg(feature = "std")]
39    IOError(io::Error),
40    #[cfg(feature = "std")]
41    OsStringError(std::ffi::OsString),
42}
43
44#[cfg(feature = "std")]
45impl std::error::Error for Error {}
46
47impl fmt::Display for Error {
48    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
49        match self {
50            Error::Unsupported => write!(f, "Feature unsupported on this device"),
51            Error::InternalError => write!(f, "Internal error in the Crash Log library"),
52            Error::NoCrashLogFound => write!(f, "No Crash Log could be found"),
53            Error::NoCrashLogSourceFound => write!(f, "No Crash Log source could be found"),
54            Error::InvalidCrashLog => write!(f, "The Crash Log is invalid"),
55            #[cfg(feature = "collateral_manager")]
56            Error::MissingCollateral(pvss, item) => {
57                write!(f, "Missing {item} collateral file for {pvss}")
58            }
59            #[cfg(feature = "collateral_manager")]
60            Error::MissingDecodeDefinitions(version) => {
61                write!(f, "Missing decode definitions for {version}")
62            }
63            Error::InvalidBootErrorRecordRegion => write!(f, "Invalid Boot Error Record region"),
64            Error::InvalidHeader => write!(f, "Invalid Crash Log Header"),
65            Error::EmptyRegion => write!(f, "The Crash Log Region is not populated"),
66            Error::InvalidHeaderType(ht) => write!(f, "Invalid Crash Log Header Type: {ht}"),
67            Error::InvalidRecordType(rt) => write!(f, "Unknown Crash Log Record Type: {rt:#x}"),
68            Error::InvalidProductID(pid) => write!(f, "Unknown Crash Log Product ID: {pid:#x}"),
69            #[cfg(feature = "serialize")]
70            Error::JsonError(err) => write!(f, "Invalid JSON file: {err}"),
71            Error::Utf8Error(err) => write!(f, "UTF8 Error: {err}"),
72            Error::ParseIntError(err) => write!(f, "Error while parsing integer: {err}"),
73            #[cfg(feature = "std")]
74            Error::IOError(err) => write!(f, "Encountered IO error: {err}"),
75            #[cfg(feature = "std")]
76            Error::OsStringError(s) => write!(f, "Cannot convert OS string: {s:?}"),
77        }
78    }
79}
80
81#[cfg(feature = "std")]
82impl From<io::Error> for Error {
83    fn from(err: io::Error) -> Self {
84        Error::IOError(err)
85    }
86}
87
88#[cfg(feature = "std")]
89impl From<std::ffi::OsString> for Error {
90    fn from(err: std::ffi::OsString) -> Self {
91        Error::OsStringError(err)
92    }
93}
94
95#[cfg(feature = "serialize")]
96impl From<serde_json::Error> for Error {
97    fn from(err: serde_json::Error) -> Self {
98        Error::JsonError(err)
99    }
100}
101
102impl From<str::Utf8Error> for Error {
103    fn from(err: str::Utf8Error) -> Self {
104        Error::Utf8Error(err)
105    }
106}
107
108impl From<num::ParseIntError> for Error {
109    fn from(err: num::ParseIntError) -> Self {
110        Error::ParseIntError(err)
111    }
112}