2019-09-17 23:26:49 +03:00
|
|
|
use std::fmt;
|
|
|
|
|
2019-09-10 22:55:32 +03:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
2019-09-12 18:39:25 +03:00
|
|
|
InvalidPath(crate::store::StorePath),
|
|
|
|
BadStorePath(std::path::PathBuf),
|
|
|
|
BadNarInfo,
|
2019-09-17 01:18:17 +03:00
|
|
|
BadBase32,
|
2019-09-17 23:26:49 +03:00
|
|
|
StorePathNameTooLong,
|
|
|
|
BadStorePathName,
|
2019-09-11 02:15:20 +03:00
|
|
|
IOError(std::io::Error),
|
2019-09-12 18:39:25 +03:00
|
|
|
HttpError(reqwest::Error),
|
2019-09-10 22:55:32 +03:00
|
|
|
Misc(String),
|
2019-09-11 02:15:20 +03:00
|
|
|
Foreign(CppException),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<std::io::Error> for Error {
|
|
|
|
fn from(err: std::io::Error) -> Self {
|
|
|
|
Error::IOError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-12 18:39:25 +03:00
|
|
|
impl From<reqwest::Error> for Error {
|
|
|
|
fn from(err: reqwest::Error) -> Self {
|
|
|
|
Error::HttpError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-17 23:26:49 +03:00
|
|
|
impl fmt::Display for Error {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
Error::InvalidPath(_) => write!(f, "invalid path"),
|
|
|
|
Error::BadNarInfo => write!(f, ".narinfo file is corrupt"),
|
|
|
|
Error::BadStorePath(path) => write!(f, "path '{}' is not a store path", path.display()),
|
|
|
|
Error::BadBase32 => write!(f, "invalid base32 string"),
|
|
|
|
Error::StorePathNameTooLong => {
|
|
|
|
write!(f, "store path name is longer than 211 characters")
|
|
|
|
}
|
|
|
|
Error::BadStorePathName => write!(f, "store path name contains forbidden character"),
|
|
|
|
Error::IOError(err) => write!(f, "I/O error: {}", err),
|
|
|
|
Error::HttpError(err) => write!(f, "HTTP error: {}", err),
|
|
|
|
Error::Foreign(_) => write!(f, "<C++ exception>"), // FIXME
|
|
|
|
Error::Misc(s) => write!(f, "{}", s),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-11 02:15:20 +03:00
|
|
|
impl From<Error> for CppException {
|
|
|
|
fn from(err: Error) -> Self {
|
|
|
|
match err {
|
|
|
|
Error::Foreign(ex) => ex,
|
2019-09-17 23:26:49 +03:00
|
|
|
_ => unsafe { make_error(&err.to_string()) },
|
2019-09-11 02:15:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct CppException(*const libc::c_void); // == std::exception_ptr*
|
|
|
|
|
|
|
|
extern "C" {
|
2019-09-11 13:44:31 +03:00
|
|
|
#[allow(improper_ctypes)] // YOLO
|
2019-09-11 02:15:20 +03:00
|
|
|
fn make_error(s: &str) -> CppException;
|
2019-09-10 22:55:32 +03:00
|
|
|
}
|