mirror of
https://github.com/denoland/deno
synced 2024-10-30 02:21:11 +00:00
Clean up helpers in src/errors.rs
* Add errors::bad_resource() * Move permission_denied() to errors.rs * Make op_symlink's not_implemented() into a runtime panic.
This commit is contained in:
parent
899a62d5b0
commit
c3bce178b8
2 changed files with 21 additions and 34 deletions
|
@ -94,7 +94,7 @@ impl DenoError {
|
|||
impl fmt::Display for DenoError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self.repr {
|
||||
Repr::Simple(_kind, ref _msg) => panic!("todo"),
|
||||
Repr::Simple(_kind, ref err_str) => f.pad(err_str),
|
||||
Repr::IoErr(ref err) => err.fmt(f),
|
||||
Repr::UrlErr(ref err) => err.fmt(f),
|
||||
Repr::HyperErr(ref err) => err.fmt(f),
|
||||
|
@ -109,7 +109,6 @@ impl std::error::Error for DenoError {
|
|||
Repr::IoErr(ref err) => err.description(),
|
||||
Repr::UrlErr(ref err) => err.description(),
|
||||
Repr::HyperErr(ref err) => err.description(),
|
||||
// Repr::Simple(..) => "FIXME",
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -119,7 +118,6 @@ impl std::error::Error for DenoError {
|
|||
Repr::IoErr(ref err) => Some(err),
|
||||
Repr::UrlErr(ref err) => Some(err),
|
||||
Repr::HyperErr(ref err) => Some(err),
|
||||
// Repr::Simple(..) => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -150,3 +148,17 @@ impl From<hyper::Error> for DenoError {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn bad_resource() -> DenoError {
|
||||
new(
|
||||
ErrorKind::BadFileDescriptor, // TODO Rename to BadResource
|
||||
String::from("bad resource id"),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn permission_denied() -> DenoError {
|
||||
new(
|
||||
ErrorKind::PermissionDenied,
|
||||
String::from("permission denied"),
|
||||
)
|
||||
}
|
||||
|
|
37
src/ops.rs
37
src/ops.rs
|
@ -1,6 +1,7 @@
|
|||
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
use errors;
|
||||
use errors::permission_denied;
|
||||
use errors::DenoError;
|
||||
use errors::DenoResult;
|
||||
use fs as deno_fs;
|
||||
|
@ -153,20 +154,6 @@ pub fn dispatch(
|
|||
return (base.sync(), boxed_op);
|
||||
}
|
||||
|
||||
fn permission_denied() -> DenoError {
|
||||
DenoError::from(std::io::Error::new(
|
||||
std::io::ErrorKind::PermissionDenied,
|
||||
"permission denied",
|
||||
))
|
||||
}
|
||||
|
||||
fn not_implemented() -> DenoError {
|
||||
DenoError::from(std::io::Error::new(
|
||||
std::io::ErrorKind::Other,
|
||||
"Not implemented",
|
||||
))
|
||||
}
|
||||
|
||||
fn op_exit(
|
||||
_config: Arc<IsolateState>,
|
||||
base: &msg::Base,
|
||||
|
@ -605,10 +592,7 @@ fn op_close(
|
|||
let inner = base.inner_as_close().unwrap();
|
||||
let rid = inner.rid();
|
||||
match resources::lookup(rid) {
|
||||
None => odd_future(errors::new(
|
||||
errors::ErrorKind::BadFileDescriptor,
|
||||
String::from("Bad File Descriptor"),
|
||||
)),
|
||||
None => odd_future(errors::bad_resource()),
|
||||
Some(mut resource) => {
|
||||
resource.close();
|
||||
ok_future(empty_buf())
|
||||
|
@ -655,10 +639,7 @@ fn op_read(
|
|||
let rid = inner.rid();
|
||||
|
||||
match resources::lookup(rid) {
|
||||
None => odd_future(errors::new(
|
||||
errors::ErrorKind::BadFileDescriptor,
|
||||
String::from("Bad File Descriptor"),
|
||||
)),
|
||||
None => odd_future(errors::bad_resource()),
|
||||
Some(resource) => {
|
||||
let op = tokio_io::io::read(resource, data)
|
||||
.map_err(|err| DenoError::from(err))
|
||||
|
@ -697,10 +678,7 @@ fn op_write(
|
|||
let rid = inner.rid();
|
||||
|
||||
match resources::lookup(rid) {
|
||||
None => odd_future(errors::new(
|
||||
errors::ErrorKind::BadFileDescriptor,
|
||||
String::from("Bad File Descriptor"),
|
||||
)),
|
||||
None => odd_future(errors::bad_resource()),
|
||||
Some(resource) => {
|
||||
let len = data.len();
|
||||
let op = tokio_io::io::write_all(resource, data)
|
||||
|
@ -989,7 +967,7 @@ fn op_symlink(
|
|||
}
|
||||
// TODO Use type for Windows.
|
||||
if cfg!(windows) {
|
||||
return odd_future(not_implemented());
|
||||
panic!("symlink for windows is not yet implemented")
|
||||
}
|
||||
|
||||
let inner = base.inner_as_symlink().unwrap();
|
||||
|
@ -1140,10 +1118,7 @@ fn op_accept(
|
|||
let server_rid = inner.rid();
|
||||
|
||||
match resources::lookup(server_rid) {
|
||||
None => odd_future(errors::new(
|
||||
errors::ErrorKind::BadFileDescriptor,
|
||||
String::from("Bad File Descriptor"),
|
||||
)),
|
||||
None => odd_future(errors::bad_resource()),
|
||||
Some(server_resource) => {
|
||||
let op = tokio_util::accept(server_resource)
|
||||
.map_err(|err| DenoError::from(err))
|
||||
|
|
Loading…
Reference in a new issue