Use anyhow for error handling

Summary:
This is a nice solution if you don't want to spend
too much time thinking about error handling.
You have one common return type and can basically use `?`
everywhere while still maintaining the flexibility to
create custom error types where needed.

Reviewed By: jknoxville

Differential Revision: D21349046

fbshipit-source-id: 073539ce8422cdb3e0141886e95321052bc0c7a3
This commit is contained in:
Pascal Hartig
2020-05-01 09:41:13 -07:00
committed by Facebook GitHub Bot
parent 6f9d82117e
commit 2c20f016d4
4 changed files with 20 additions and 17 deletions

View File

@@ -7,27 +7,19 @@
use crate::types::{PackType, Platform};
use std::fmt;
use std::io;
use std::path::PathBuf;
#[derive(Debug)]
pub enum Error {
IOError(io::Error),
MissingPackFile(Platform, PackType, PathBuf),
MissingPackDefinition(Platform, PackType),
}
impl From<io::Error> for Error {
fn from(e: io::Error) -> Self {
Error::IOError(e)
}
}
impl std::error::Error for Error {}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use Error::*;
match self {
IOError(e) => write!(f, "IO Error: {}", e),
Error::MissingPackFile(platform, pack_type, path) => write!(
f,
"Couldn't open file to pack for platform {:?} and type {:?}: {}",
@@ -35,6 +27,11 @@ impl fmt::Display for Error {
pack_type,
path.to_string_lossy()
),
Error::MissingPackDefinition(platform, pack_type) => write!(
f,
"Missing packlist definition for platform {:?} and pack type {:?}.",
platform, pack_type,
),
}
}
}