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    ControlCommandRejected,
43    CrashLogPending,
44    SourceBusy,
45}
46
47#[cfg(feature = "std")]
48impl std::error::Error for Error {}
49
50impl fmt::Display for Error {
51    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
52        match self {
53            Error::Unsupported => write!(f, "Feature unsupported on this device"),
54            Error::InternalError => write!(f, "Internal error in the Crash Log library"),
55            Error::NoCrashLogFound => write!(f, "No Crash Log could be found"),
56            Error::NoCrashLogSourceFound => write!(f, "No Crash Log source could be found"),
57            Error::InvalidCrashLog => write!(f, "The Crash Log is invalid"),
58            #[cfg(feature = "collateral_manager")]
59            Error::MissingCollateral(pvss, item) => {
60                write!(f, "Missing {item} collateral file for {pvss}")
61            }
62            #[cfg(feature = "collateral_manager")]
63            Error::MissingDecodeDefinitions(version) => {
64                write!(f, "Missing decode definitions for {version}")
65            }
66            Error::InvalidBootErrorRecordRegion => write!(f, "Invalid Boot Error Record region"),
67            Error::InvalidHeader => write!(f, "Invalid Crash Log Header"),
68            Error::EmptyRegion => write!(f, "The Crash Log Region is not populated"),
69            Error::InvalidHeaderType(ht) => write!(f, "Invalid Crash Log Header Type: {ht}"),
70            Error::InvalidRecordType(rt) => write!(f, "Unknown Crash Log Record Type: {rt:#x}"),
71            Error::InvalidProductID(pid) => write!(f, "Unknown Crash Log Product ID: {pid:#x}"),
72            #[cfg(feature = "serialize")]
73            Error::JsonError(err) => write!(f, "Invalid JSON file: {err}"),
74            Error::Utf8Error(err) => write!(f, "UTF8 Error: {err}"),
75            Error::ParseIntError(err) => write!(f, "Error while parsing integer: {err}"),
76            #[cfg(feature = "std")]
77            Error::IOError(err) => write!(f, "Encountered IO error: {err}"),
78            #[cfg(feature = "std")]
79            Error::OsStringError(s) => write!(f, "Cannot convert OS string: {s:?}"),
80            Error::ControlCommandRejected => write!(f, "Control command was rejected"),
81            Error::CrashLogPending => write!(f, "A Crash Log collection is already pending"),
82            Error::SourceBusy => write!(f, "Crash Log source is busy"),
83        }
84    }
85}
86
87#[cfg(feature = "std")]
88impl From<io::Error> for Error {
89    fn from(err: io::Error) -> Self {
90        Error::IOError(err)
91    }
92}
93
94#[cfg(feature = "std")]
95impl From<std::ffi::OsString> for Error {
96    fn from(err: std::ffi::OsString) -> Self {
97        Error::OsStringError(err)
98    }
99}
100
101#[cfg(feature = "serialize")]
102impl From<serde_json::Error> for Error {
103    fn from(err: serde_json::Error) -> Self {
104        Error::JsonError(err)
105    }
106}
107
108impl From<str::Utf8Error> for Error {
109    fn from(err: str::Utf8Error) -> Self {
110        Error::Utf8Error(err)
111    }
112}
113
114impl From<num::ParseIntError> for Error {
115    fn from(err: num::ParseIntError) -> Self {
116        Error::ParseIntError(err)
117    }
118}