diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2018-08-15 23:36:48 -0400 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2018-08-21 15:37:45 -0400 |
commit | 18d495c7d17cf3fce3835e732094d058f51eddaa (patch) | |
tree | f7244cfd83dbff9d8aaf67203feb0f3a24fe95f3 /src/errors.rs | |
parent | cb1393cdaea4bfbee69efbf7ce86a4adfc4593b3 (diff) |
Better error handling in src/handlers.rs
Introduces error codes that are shared between JS/RS
Fixes #526.
Diffstat (limited to 'src/errors.rs')
-rw-r--r-- | src/errors.rs | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/src/errors.rs b/src/errors.rs new file mode 100644 index 000000000..f556ba1c2 --- /dev/null +++ b/src/errors.rs @@ -0,0 +1,117 @@ +// Copyright 2018 the Deno authors. All rights reserved. MIT license. +use msg_generated::deno as msg; +use std; +use std::fmt; +use std::io; +use url; + +pub use self::msg::ErrorKind; + +pub type DenoResult<T> = std::result::Result<T, DenoError>; + +#[derive(Debug)] +pub struct DenoError { + repr: Repr, +} + +impl DenoError { + pub fn kind(&self) -> ErrorKind { + match self.repr { + // 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, + } + } + } + } +} + +#[derive(Debug)] +enum Repr { + // Simple(ErrorKind), + IoErr(io::Error), + UrlErr(url::ParseError), +} + +impl fmt::Display for DenoError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self.repr { + Repr::IoErr(ref err) => err.fmt(f), + Repr::UrlErr(ref err) => err.fmt(f), + // Repr::Simple(..) => Ok(()), + } + } +} + +impl std::error::Error for DenoError { + fn description(&self) -> &str { + match self.repr { + Repr::IoErr(ref err) => err.description(), + Repr::UrlErr(ref err) => err.description(), + // Repr::Simple(..) => "FIXME", + } + } + + fn cause(&self) -> Option<&std::error::Error> { + match self.repr { + Repr::IoErr(ref err) => Some(err), + Repr::UrlErr(ref err) => Some(err), + // Repr::Simple(..) => None, + } + } +} + +impl From<io::Error> for DenoError { + #[inline] + fn from(err: io::Error) -> DenoError { + DenoError { + repr: Repr::IoErr(err), + } + } +} + +impl From<url::ParseError> for DenoError { + #[inline] + fn from(err: url::ParseError) -> DenoError { + DenoError { + repr: Repr::UrlErr(err), + } + } +} |