From 12f9fe7763b20c9e6da7de2e784b5aa0e49ef68b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20M=C3=BCller?= Date: Sat, 2 Dec 2023 09:32:05 -0800 Subject: [PATCH] Add a few more error contexts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This change adds context to two more error paths in an attempt to improve error reporting. Refs: #266 Signed-off-by: Daniel Müller --- src/elf/parser.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/elf/parser.rs b/src/elf/parser.rs index 75e2917f..934e4faa 100644 --- a/src/elf/parser.rs +++ b/src/elf/parser.rs @@ -430,7 +430,9 @@ pub(crate) struct ElfParser { impl ElfParser { /// Create an `ElfParser` from an open file. pub fn open_file(file: &File) -> Result { - Mmap::map(file).map(Self::from_mmap) + Mmap::map(file) + .map(Self::from_mmap) + .context("failed to memory map file") } /// Create an `ElfParser` from mmap'ed data. @@ -450,13 +452,9 @@ impl ElfParser { /// Create an `ElfParser` for a path. pub fn open(filename: &Path) -> Result { - let file = File::open(filename)?; - let parser = Self::open_file(&file); - if let Ok(parser) = parser { - Ok(parser) - } else { - parser - } + let file = File::open(filename) + .with_context(|| format!("failed to open {}", filename.display()))?; + Self::open_file(&file) } /// Retrieve the data corresponding to the ELF section at index `idx`.