2019-01-01 19:58:40 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2018-08-14 16:50:53 -04:00
|
|
|
use hyper;
|
2018-09-09 19:21:22 -04:00
|
|
|
pub use msg::ErrorKind;
|
2018-08-15 23:36:48 -04:00
|
|
|
use std;
|
|
|
|
use std::fmt;
|
|
|
|
use std::io;
|
|
|
|
use url;
|
|
|
|
|
|
|
|
pub type DenoResult<T> = std::result::Result<T, DenoError>;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct DenoError {
|
|
|
|
repr: Repr,
|
|
|
|
}
|
|
|
|
|
2018-08-14 16:50:53 -04:00
|
|
|
#[derive(Debug)]
|
|
|
|
enum Repr {
|
2018-09-27 00:56:39 -04:00
|
|
|
Simple(ErrorKind, String),
|
2018-08-14 16:50:53 -04:00
|
|
|
IoErr(io::Error),
|
|
|
|
UrlErr(url::ParseError),
|
|
|
|
HyperErr(hyper::Error),
|
|
|
|
}
|
|
|
|
|
2018-09-27 00:56:39 -04:00
|
|
|
pub fn new(kind: ErrorKind, msg: String) -> DenoError {
|
|
|
|
DenoError {
|
|
|
|
repr: Repr::Simple(kind, msg),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-15 23:36:48 -04:00
|
|
|
impl DenoError {
|
|
|
|
pub fn kind(&self) -> ErrorKind {
|
|
|
|
match self.repr {
|
2018-09-27 00:56:39 -04:00
|
|
|
Repr::Simple(kind, ref _msg) => kind,
|
2018-08-15 23:36:48 -04:00
|
|
|
// Repr::Simple(kind) => kind,
|
|
|
|
Repr::IoErr(ref err) => {
|
|
|
|
use std::io::ErrorKind::*;
|
|
|
|
match err.kind() {
|
|
|
|
NotFound => ErrorKind::NotFound,
|
|
|
|
PermissionDenied => ErrorKind::PermissionDenied,
|
|
|
|
ConnectionRefused => ErrorKind::ConnectionRefused,
|
|
|
|
ConnectionReset => ErrorKind::ConnectionReset,
|
|
|
|
ConnectionAborted => ErrorKind::ConnectionAborted,
|
|
|
|
NotConnected => ErrorKind::NotConnected,
|
|
|
|
AddrInUse => ErrorKind::AddrInUse,
|
|
|
|
AddrNotAvailable => ErrorKind::AddrNotAvailable,
|
|
|
|
BrokenPipe => ErrorKind::BrokenPipe,
|
|
|
|
AlreadyExists => ErrorKind::AlreadyExists,
|
|
|
|
WouldBlock => ErrorKind::WouldBlock,
|
|
|
|
InvalidInput => ErrorKind::InvalidInput,
|
|
|
|
InvalidData => ErrorKind::InvalidData,
|
|
|
|
TimedOut => ErrorKind::TimedOut,
|
|
|
|
Interrupted => ErrorKind::Interrupted,
|
|
|
|
WriteZero => ErrorKind::WriteZero,
|
|
|
|
Other => ErrorKind::Other,
|
|
|
|
UnexpectedEof => ErrorKind::UnexpectedEof,
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Repr::UrlErr(ref err) => {
|
|
|
|
use url::ParseError::*;
|
|
|
|
match err {
|
|
|
|
EmptyHost => ErrorKind::EmptyHost,
|
|
|
|
IdnaError => ErrorKind::IdnaError,
|
|
|
|
InvalidPort => ErrorKind::InvalidPort,
|
|
|
|
InvalidIpv4Address => ErrorKind::InvalidIpv4Address,
|
|
|
|
InvalidIpv6Address => ErrorKind::InvalidIpv6Address,
|
|
|
|
InvalidDomainCharacter => ErrorKind::InvalidDomainCharacter,
|
|
|
|
RelativeUrlWithoutBase => ErrorKind::RelativeUrlWithoutBase,
|
|
|
|
RelativeUrlWithCannotBeABaseBase => {
|
|
|
|
ErrorKind::RelativeUrlWithCannotBeABaseBase
|
|
|
|
}
|
|
|
|
SetHostOnCannotBeABaseUrl => ErrorKind::SetHostOnCannotBeABaseUrl,
|
|
|
|
Overflow => ErrorKind::Overflow,
|
|
|
|
}
|
|
|
|
}
|
2018-08-14 16:50:53 -04:00
|
|
|
Repr::HyperErr(ref err) => {
|
|
|
|
// For some reason hyper::errors::Kind is private.
|
|
|
|
if err.is_parse() {
|
|
|
|
ErrorKind::HttpParse
|
|
|
|
} else if err.is_user() {
|
|
|
|
ErrorKind::HttpUser
|
|
|
|
} else if err.is_canceled() {
|
|
|
|
ErrorKind::HttpCanceled
|
|
|
|
} else if err.is_closed() {
|
|
|
|
ErrorKind::HttpClosed
|
|
|
|
} else {
|
|
|
|
ErrorKind::HttpOther
|
|
|
|
}
|
|
|
|
}
|
2018-08-15 23:36:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for DenoError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match self.repr {
|
2018-10-05 10:20:51 -04:00
|
|
|
Repr::Simple(_kind, ref err_str) => f.pad(err_str),
|
2018-08-15 23:36:48 -04:00
|
|
|
Repr::IoErr(ref err) => err.fmt(f),
|
|
|
|
Repr::UrlErr(ref err) => err.fmt(f),
|
2018-08-14 16:50:53 -04:00
|
|
|
Repr::HyperErr(ref err) => err.fmt(f),
|
2018-08-15 23:36:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::error::Error for DenoError {
|
|
|
|
fn description(&self) -> &str {
|
|
|
|
match self.repr {
|
2018-09-27 00:56:39 -04:00
|
|
|
Repr::Simple(_kind, ref msg) => msg.as_str(),
|
2018-08-15 23:36:48 -04:00
|
|
|
Repr::IoErr(ref err) => err.description(),
|
|
|
|
Repr::UrlErr(ref err) => err.description(),
|
2018-08-14 16:50:53 -04:00
|
|
|
Repr::HyperErr(ref err) => err.description(),
|
2018-08-15 23:36:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cause(&self) -> Option<&std::error::Error> {
|
|
|
|
match self.repr {
|
2018-09-27 00:56:39 -04:00
|
|
|
Repr::Simple(_kind, ref _msg) => None,
|
2018-08-15 23:36:48 -04:00
|
|
|
Repr::IoErr(ref err) => Some(err),
|
|
|
|
Repr::UrlErr(ref err) => Some(err),
|
2018-08-14 16:50:53 -04:00
|
|
|
Repr::HyperErr(ref err) => Some(err),
|
2018-08-15 23:36:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<io::Error> for DenoError {
|
|
|
|
#[inline]
|
2018-11-05 01:21:21 -05:00
|
|
|
fn from(err: io::Error) -> Self {
|
|
|
|
Self {
|
2018-08-15 23:36:48 -04:00
|
|
|
repr: Repr::IoErr(err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<url::ParseError> for DenoError {
|
|
|
|
#[inline]
|
2018-11-05 01:21:21 -05:00
|
|
|
fn from(err: url::ParseError) -> Self {
|
|
|
|
Self {
|
2018-08-15 23:36:48 -04:00
|
|
|
repr: Repr::UrlErr(err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-14 16:50:53 -04:00
|
|
|
|
|
|
|
impl From<hyper::Error> for DenoError {
|
|
|
|
#[inline]
|
2018-11-05 01:21:21 -05:00
|
|
|
fn from(err: hyper::Error) -> Self {
|
|
|
|
Self {
|
2018-08-14 16:50:53 -04:00
|
|
|
repr: Repr::HyperErr(err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-05 10:20:51 -04:00
|
|
|
|
|
|
|
pub fn bad_resource() -> DenoError {
|
2018-10-10 11:15:47 -04:00
|
|
|
new(ErrorKind::BadResource, String::from("bad resource id"))
|
2018-10-05 10:20:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn permission_denied() -> DenoError {
|
|
|
|
new(
|
|
|
|
ErrorKind::PermissionDenied,
|
|
|
|
String::from("permission denied"),
|
|
|
|
)
|
|
|
|
}
|