Skip to content

Commit 550e72c

Browse files
committed
add path to error handling
1 parent fa7dd2a commit 550e72c

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

src/oxide_controller/config.rs

Lines changed: 31 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,20 @@ 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).map_err(|e| {
82+
(path.to_path_buf(), e)
83+
})?;
84+
let config_parsed: ConfigController = toml::from_str(&file_contents).map_err(|e| {
85+
(path.to_path_buf(), e)
86+
})?;
7287
Ok(config_parsed)
7388
}
7489
}
7590

7691
#[cfg(test)]
7792
mod test {
7893
use super::ConfigController;
79-
use super::LoadError;
94+
use super::{LoadErrorKind, LoadError};
8095
use dropshot::ConfigDropshot;
8196
use dropshot::ConfigLogging;
8297
use dropshot::ConfigLoggingIfExists;
@@ -141,7 +156,7 @@ mod test {
141156
fn test_config_bad_toml() {
142157
let error =
143158
read_config("bad_toml", "foo =").expect_err("expected failure");
144-
if let LoadError::Parse(error) = error {
159+
if let LoadErrorKind::Parse(error) = &error.kind {
145160
assert_eq!(error.line_col(), Some((0, 5)));
146161
} else {
147162
panic!(
@@ -159,7 +174,7 @@ mod test {
159174
#[test]
160175
fn test_config_empty() {
161176
let error = read_config("empty", "").expect_err("expected failure");
162-
if let LoadError::Parse(error) = error {
177+
if let LoadErrorKind::Parse(error) = &error.kind {
163178
assert_eq!(error.line_col(), None);
164179
} else {
165180
panic!(

0 commit comments

Comments
 (0)