core: Add better error handling for reading files.

This commit is contained in:
Dorian 2019-09-10 13:12:18 -04:00
parent 4b0b82c19a
commit e7f22164ac
3 changed files with 23 additions and 15 deletions

View File

@ -11,7 +11,7 @@ pub enum RookeriesError {
CopyFileFailure(io::Error, String, FileType),
WriteFileFailure(io::Error, String, FileType),
CreateDirectoryFailure(io::Error, FileType),
ReadMarkdownSourceFailure(io::Error, String),
ReadFileFailure(io::Error, String, FileType),
}
impl fmt::Display for RookeriesError {
@ -42,8 +42,8 @@ impl fmt::Display for RookeriesError {
writeln!(f, "Could not write {} {}.", file_type, filename.red())?;
write!(f, "Exact cause: {}", e)
}
RookeriesError::ReadMarkdownSourceFailure(ref e, ref filename) => {
writeln!(f, "Could not read Markdown source {}.", filename.red())?;
RookeriesError::ReadFileFailure(ref e, ref filename, ref file_type) => {
writeln!(f, "Could not read {} {}.", file_type, filename.red())?;
write!(f, "Exact cause: {}", e)
}
RookeriesError::CreateDirectoryFailure(ref e, ref directory_type) => {
@ -66,7 +66,7 @@ impl error::Error for RookeriesError {
RookeriesError::MissingSiteToml => None,
RookeriesError::CopyFileFailure(ref e, _, _) => Some(e),
RookeriesError::WriteFileFailure(ref e, _, _) => Some(e),
RookeriesError::ReadMarkdownSourceFailure(ref e, _) => Some(e),
RookeriesError::ReadFileFailure(ref e, _, _) => Some(e),
RookeriesError::CreateDirectoryFailure(ref e, _) => Some(e),
}
}

View File

@ -8,6 +8,7 @@ pub enum FileType {
Plugin,
HtmlTemplate,
SampleMarkdown,
SourceMarkdown,
PageAPI,
APIRoot,
ProjectRoot,
@ -28,6 +29,7 @@ impl fmt::Display for FileType {
FileType::HtmlTemplate => "HTML template",
FileType::Template => "template",
FileType::SampleMarkdown => "sample Markdown",
FileType::SourceMarkdown => "sample Markdown",
FileType::PageAPI => "API page root",
FileType::APIRoot => "API root",
FileType::ProjectRoot => "project root",

View File

@ -191,8 +191,9 @@ fn build_site(args: &Args) -> Result {
return Err(RookeriesError::MissingSiteToml);
}
let project_manifest_details =
read_to_string(project_manifest).expect("Unable to read file with toml.");
let project_manifest_details = read_to_string(project_manifest).map_err(|err| {
RookeriesError::ReadFileFailure(err, "site.toml".to_string(), FileType::SiteToml)
})?;
let project_site: Site =
toml::from_str(&project_manifest_details).expect("Unable to read from TOML.");
success_message(&format!(
@ -241,7 +242,11 @@ fn build_site(args: &Args) -> Result {
.clone()
.into_string()
.unwrap_or(ROOKERIES_UNKNOWN_PLACEHOLDER.to_string());
RookeriesError::ReadMarkdownSourceFailure(err, source_page_err)
RookeriesError::ReadMarkdownSourceFailure(
err,
source_page_err,
FileType::SourceMarkdown,
)
})
.unwrap_or(ROOKERIES_UNKNOWN_PLACEHOLDER.to_string());
@ -335,10 +340,7 @@ fn build_site(args: &Args) -> Result {
FileType::RenderedHtml,
)
})?;
success_message(&format!(
"Created the {} HTML page.",
&page.slug.green()
));
success_message(&format!("Created the {} HTML page.", &page.slug.green()));
}
// Create the API representations.
@ -370,7 +372,10 @@ fn build_site(args: &Args) -> Result {
FileType::RenderedSiteJson,
)
})?;
success_message(&format!("Created the {}.", FileType::RenderedSiteJson.to_string().green()));
success_message(&format!(
"Created the {}.",
FileType::RenderedSiteJson.to_string().green()
));
// Create all the page API reps.
let api_pages_path = &api_path.join("pages");
@ -379,8 +384,9 @@ fn build_site(args: &Args) -> Result {
let page_json = serde_json::to_string_pretty(&page)
.expect("Unable to create JSON representation of page.");
let page_json_filename = format!("{}.json", &page.slug);
write(api_pages_path.join(&page_json_filename), page_json)
.map_err(|err| RookeriesError::WriteFileFailure(err, page_json_filename, FileType::RenderedPageJson))?;
write(api_pages_path.join(&page_json_filename), page_json).map_err(|err| {
RookeriesError::WriteFileFailure(err, page_json_filename, FileType::RenderedPageJson)
})?;
success_message(&format!(
"Created the {} for the {} page.",
FileType::RenderedPageJson.to_string().green(),
@ -460,7 +466,7 @@ fn initialize_site(args: &Args) -> Result {
}
let project_directory = project_directory
.canonicalize()
.expect("Project directory fails.");
.expect("Unable to build canonical path to the project directory.");
// TODO: Make the sample pages happen.
let mut sample_pages: HashMap<String, Value> = HashMap::new();