Skip to content

Commit c87a9bc

Browse files
committed
errors: Improve error types and implement Debug
As our error types are enums, we can include the "source" error in our later error types. This allows for more information when debugging failures. To this end, we also implement Debug. Signed-off-by: Joe Richey <joerichey@google.com>
1 parent 5f42401 commit c87a9bc

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

src/bzimage.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,17 @@
1414

1515
use crate::fat::{self, Read};
1616

17+
#[derive(Debug)]
1718
pub enum Error {
18-
FileError,
19+
FileError(fat::Error),
1920
KernelOld,
2021
MagicMissing,
2122
NotRelocatable,
2223
}
2324

2425
impl From<fat::Error> for Error {
25-
fn from(_: fat::Error) -> Error {
26-
Error::FileError
26+
fn from(e: fat::Error) -> Error {
27+
Error::FileError(e)
2728
}
2829
}
2930

@@ -85,7 +86,7 @@ pub fn load_initrd(f: &mut dyn Read) -> Result<(), Error> {
8586
let mut data: [u8; 512] = [0; 512];
8687
match f.read(&mut data) {
8788
Err(crate::fat::Error::EndOfFile) => break,
88-
Err(_) => return Err(Error::FileError),
89+
Err(e) => return Err(Error::FileError(e)),
8990
Ok(_) => {}
9091
}
9192
let dst = initrd_region.as_mut_slice(u64::from(offset), u64::from(bytes_remaining));
@@ -96,7 +97,7 @@ pub fn load_initrd(f: &mut dyn Read) -> Result<(), Error> {
9697
let dst = initrd_region.as_mut_slice(u64::from(offset), 512);
9798
match f.read(dst) {
9899
Err(crate::fat::Error::EndOfFile) => break,
99-
Err(_) => return Err(Error::FileError),
100+
Err(e) => return Err(Error::FileError(e)),
100101
Ok(_) => {}
101102
}
102103

@@ -202,7 +203,7 @@ pub fn load_kernel(f: &mut dyn Read) -> Result<u64, Error> {
202203
// 0x200 is the startup_64 offset
203204
return Ok(u64::from(KERNEL_LOCATION) + 0x200);
204205
}
205-
Err(_) => return Err(Error::FileError),
206+
Err(e) => return Err(Error::FileError(e)),
206207
Ok(_) => {}
207208
};
208209

src/loader.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,21 @@ pub struct LoaderConfig {
2323
pub cmdline: [u8; 4096],
2424
}
2525

26+
#[derive(Debug)]
2627
pub enum Error {
27-
FileError,
28-
BzImageError,
28+
FileError(fat::Error),
29+
BzImageError(bzimage::Error),
2930
}
3031

3132
impl From<fat::Error> for Error {
32-
fn from(_: fat::Error) -> Error {
33-
Error::FileError
33+
fn from(e: fat::Error) -> Error {
34+
Error::FileError(e)
3435
}
3536
}
3637

3738
impl From<bzimage::Error> for Error {
38-
fn from(_: bzimage::Error) -> Error {
39-
Error::BzImageError
39+
fn from(e: bzimage::Error) -> Error {
40+
Error::BzImageError(e)
4041
}
4142
}
4243

src/virtio.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
/// Virtio related errors
16+
#[derive(Debug)]
1617
pub enum Error {
1718
VirtioUnsupportedDevice,
1819
VirtioLegacyOnly,

0 commit comments

Comments
 (0)