Skip to content

Commit 8d636e3

Browse files
committed
add path to error handling
1 parent fa7dd2a commit 8d636e3

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

src/oxide_controller/config.rs

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use dropshot::ConfigLogging;
88
use serde::Deserialize;
99
use serde::Serialize;
1010
use std::fmt;
11-
use std::path::Path;
11+
use std::path::{Path, PathBuf};
1212

1313
/**
1414
* Configuration for an OXC server
@@ -24,34 +24,45 @@ pub struct ConfigController {
2424
}
2525

2626
#[derive(Debug)]
27-
pub enum LoadError {
27+
pub struct LoadError {
28+
path: PathBuf,
29+
kind: LoadErrorKind,
30+
}
31+
#[derive(Debug)]
32+
pub enum LoadErrorKind {
2833
Io(std::io::Error),
2934
Parse(toml::de::Error),
3035
}
3136

32-
impl From<std::io::Error> for LoadError {
33-
fn from(err: std::io::Error) -> Self {
34-
LoadError::Io(err)
37+
impl From<(PathBuf, std::io::Error)> for LoadError {
38+
fn from((path, err): (PathBuf, std::io::Error)) -> Self {
39+
LoadError {
40+
path,
41+
kind: LoadErrorKind::Io(err),
42+
}
3543
}
3644
}
37-
impl From<toml::de::Error> for LoadError {
38-
fn from(err: toml::de::Error) -> Self {
39-
LoadError::Parse(err)
45+
46+
impl From<(PathBuf, toml::de::Error)> for LoadError {
47+
fn from((path, err): (PathBuf, toml::de::Error)) -> Self {
48+
LoadError {
49+
path,
50+
kind: LoadErrorKind::Parse(err),
51+
}
4052
}
4153
}
4254

4355
impl std::error::Error for LoadError {}
4456

4557
impl fmt::Display for LoadError {
4658
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
47-
// default to debug for now
48-
write!(f, "{:?}", self)
59+
write!(f, "read {}: {:?}", self.path.display(), self.kind)
4960
}
5061
}
5162

5263
impl std::cmp::PartialEq<std::io::Error> for LoadError {
5364
fn eq(&self, other: &std::io::Error) -> bool {
54-
if let LoadError::Io(e) = self {
65+
if let LoadErrorKind::Io(e) = &self.kind {
5566
e.kind() == other.kind()
5667
} else {
5768
false
@@ -67,16 +78,18 @@ impl ConfigController {
6778
* The format is described in the README.
6879
*/
6980
pub fn from_file(path: &Path) -> Result<ConfigController, LoadError> {
70-
let file_contents = std::fs::read_to_string(path)?;
71-
let config_parsed: ConfigController = toml::from_str(&file_contents)?;
81+
let file_contents = std::fs::read_to_string(path)
82+
.map_err(|e| (path.to_path_buf(), e))?;
83+
let config_parsed: ConfigController = toml::from_str(&file_contents)
84+
.map_err(|e| (path.to_path_buf(), e))?;
7285
Ok(config_parsed)
7386
}
7487
}
7588

7689
#[cfg(test)]
7790
mod test {
7891
use super::ConfigController;
79-
use super::LoadError;
92+
use super::{LoadError, LoadErrorKind};
8093
use dropshot::ConfigDropshot;
8194
use dropshot::ConfigLogging;
8295
use dropshot::ConfigLoggingIfExists;
@@ -141,7 +154,7 @@ mod test {
141154
fn test_config_bad_toml() {
142155
let error =
143156
read_config("bad_toml", "foo =").expect_err("expected failure");
144-
if let LoadError::Parse(error) = error {
157+
if let LoadErrorKind::Parse(error) = &error.kind {
145158
assert_eq!(error.line_col(), Some((0, 5)));
146159
} else {
147160
panic!(
@@ -159,7 +172,7 @@ mod test {
159172
#[test]
160173
fn test_config_empty() {
161174
let error = read_config("empty", "").expect_err("expected failure");
162-
if let LoadError::Parse(error) = error {
175+
if let LoadErrorKind::Parse(error) = &error.kind {
163176
assert_eq!(error.line_col(), None);
164177
} else {
165178
panic!(

0 commit comments

Comments
 (0)