Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error when file cannot be read #70

Merged
merged 2 commits into from
Sep 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ impl FastlyConfig {
/// Parse a `fastly.toml` file into a `FastlyConfig`.
pub fn from_file(path: impl AsRef<Path>) -> Result<Self, FastlyConfigError> {
fs::read_to_string(path.as_ref())
.map_err(Into::into)
.map_err(|err| FastlyConfigError::IoError {
path: path.as_ref().display().to_string(),
err,
})
.and_then(Self::from_str)
}

Expand Down
10 changes: 9 additions & 1 deletion lib/src/config/unit_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@ use crate::config::dictionaries::DictionaryFormat;

use {
super::{FastlyConfig, LocalServerConfig, RawLocalServerConfig},
crate::config::DictionaryName,
crate::{config::DictionaryName, error::FastlyConfigError},
std::{fs::File, io::Write},
tempfile::tempdir,
};

#[test]
fn error_when_fastly_toml_files_cannot_be_read() {
match FastlyConfig::from_file("nonexistent.toml") {
Err(FastlyConfigError::IoError { path, .. }) if path == "nonexistent.toml" => {}
res => panic!("unexpected result: {:?}", res),
}
}

#[test]
fn fastly_toml_files_can_be_read() {
// Parse a valid `fastly.toml`, check that it succeeds.
Expand Down
10 changes: 7 additions & 3 deletions lib/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,16 @@ pub(crate) enum ExecutionError {
Instantiation(anyhow::Error),
}

/// Errors that can occur while parsing a `fastly.coml` file.
/// Errors that can occur while parsing a `fastly.toml` file.
cratelyn marked this conversation as resolved.
Show resolved Hide resolved
#[derive(Debug, thiserror::Error)]
pub enum FastlyConfigError {
/// An I/O error that occured while reading the file.
#[error("error reading `fastly.toml`: {0}")]
IoError(#[from] std::io::Error),
#[error("error reading '{path}': {err}")]
IoError {
path: String,
#[source]
err: std::io::Error,
},

#[error("invalid configuration for '{name}': {err}")]
InvalidBackendDefinition {
Expand Down