From 6235a41862b975536db6023e20784f85e78e5c0d Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Sat, 16 Feb 2019 09:36:29 +0100 Subject: [PATCH] tools::file_read_firstline - improve error message --- src/tools.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/tools.rs b/src/tools.rs index 85c93da0..92261429 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -85,21 +85,23 @@ pub fn map_struct_mut(buffer: &mut [u8]) -> Result<&mut T, Error> { Ok(unsafe { &mut * (buffer.as_ptr() as *mut T) }) } -pub fn file_read_firstline>(path: P) -> Result { +pub fn file_read_firstline>(path: P) -> Result { let path = path.as_ref(); - let file = std::fs::File::open(path)?; + try_block!({ + let file = std::fs::File::open(path)?; - use std::io::{BufRead, BufReader}; + use std::io::{BufRead, BufReader}; - let mut reader = BufReader::new(file); + let mut reader = BufReader::new(file); - let mut line = String::new(); + let mut line = String::new(); - let _ = reader.read_line(&mut line)?; + let _ = reader.read_line(&mut line)?; - Ok(line) + Ok(line) + }).map_err(|err: Error| format_err!("unable to read {:?} - {}", path, err)) } pub fn file_get_contents>(path: P) -> Result, std::io::Error> {