1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

Allow BadResource errors to take a custom message (#4251)

This commit is contained in:
Ryan Dahl 2020-03-05 08:30:41 -05:00 committed by GitHub
parent 444b1ab68e
commit 54a1688868
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 40 additions and 27 deletions

View file

@ -91,8 +91,13 @@ impl OpError {
Self::new(ErrorKind::PermissionDenied, msg)
}
pub fn bad_resource() -> OpError {
Self::new(ErrorKind::BadResource, "bad resource id".to_string())
pub fn bad_resource(msg: String) -> OpError {
Self::new(ErrorKind::BadResource, msg)
}
// BadResource usually needs no additional detail, hence this helper.
pub fn bad_resource_id() -> OpError {
Self::new(ErrorKind::BadResource, "Bad resource ID".to_string())
}
}
@ -444,10 +449,18 @@ mod tests {
#[test]
fn test_bad_resource() {
let err = OpError::bad_resource();
let err = OpError::bad_resource("Resource has been closed".to_string());
assert_eq!(err.kind, ErrorKind::BadResource);
assert_eq!(err.to_string(), "bad resource id");
assert_eq!(err.to_string(), "Resource has been closed");
}
#[test]
fn test_bad_resource_id() {
let err = OpError::bad_resource_id();
assert_eq!(err.kind, ErrorKind::BadResource);
assert_eq!(err.to_string(), "Bad resource ID");
}
#[test]
fn test_permission_denied() {
let err = OpError::permission_denied(

View file

@ -156,7 +156,7 @@ fn op_close(
state
.resource_table
.close(args.rid as u32)
.ok_or_else(OpError::bad_resource)?;
.ok_or_else(OpError::bad_resource_id)?;
Ok(JsonOp::Sync(json!({})))
}
@ -195,11 +195,11 @@ fn op_seek(
let resource = state
.resource_table
.get::<StreamResource>(rid)
.ok_or_else(OpError::bad_resource)?;
.ok_or_else(OpError::bad_resource_id)?;
let tokio_file = match resource {
StreamResource::FsFile(ref file, _) => file,
_ => return Err(OpError::bad_resource()),
_ => return Err(OpError::bad_resource_id()),
};
let mut file = futures::executor::block_on(tokio_file.try_clone())?;

View file

@ -109,7 +109,7 @@ pub fn op_fs_events_poll(
let resource_table = &mut state.borrow_mut().resource_table;
let watcher = resource_table
.get_mut::<FsEventsResource>(rid)
.ok_or_else(OpError::bad_resource)?;
.ok_or_else(OpError::bad_resource_id)?;
watcher
.receiver
.poll_recv(cx)

View file

@ -127,7 +127,7 @@ impl DenoAsyncRead for StreamResource {
ChildStdout(f) => Box::pin(f),
ChildStderr(f) => Box::pin(f),
HttpBody(f) => Box::pin(f),
_ => return Err(OpError::bad_resource()).into(),
_ => return Err(OpError::bad_resource_id()).into(),
};
let v = ready!(f.as_mut().poll_read(cx, buf))?;
@ -152,7 +152,7 @@ pub fn op_read(
let resource_table = &mut state.borrow_mut().resource_table;
let resource = resource_table
.get_mut::<StreamResource>(rid as u32)
.ok_or_else(OpError::bad_resource)?;
.ok_or_else(OpError::bad_resource_id)?;
let nread = ready!(resource.poll_read(cx, &mut buf.as_mut()[..]))?;
Poll::Ready(Ok(nread as i32))
})
@ -188,7 +188,7 @@ impl DenoAsyncWrite for StreamResource {
ClientTlsStream(f) => Box::pin(f),
ServerTlsStream(f) => Box::pin(f),
ChildStdin(f) => Box::pin(f),
_ => return Err(OpError::bad_resource()).into(),
_ => return Err(OpError::bad_resource_id()).into(),
};
let v = ready!(f.as_mut().poll_write(cx, buf))?;
@ -205,7 +205,7 @@ impl DenoAsyncWrite for StreamResource {
ClientTlsStream(f) => Box::pin(f),
ServerTlsStream(f) => Box::pin(f),
ChildStdin(f) => Box::pin(f),
_ => return Err(OpError::bad_resource()).into(),
_ => return Err(OpError::bad_resource_id()).into(),
};
ready!(f.as_mut().poll_flush(cx))?;
@ -235,7 +235,7 @@ pub fn op_write(
let resource_table = &mut state.borrow_mut().resource_table;
let resource = resource_table
.get_mut::<StreamResource>(rid as u32)
.ok_or_else(OpError::bad_resource)?;
.ok_or_else(OpError::bad_resource_id)?;
resource.poll_write(cx, &buf.as_ref()[..])
})
.await?;
@ -248,7 +248,7 @@ pub fn op_write(
let resource_table = &mut state.borrow_mut().resource_table;
let resource = resource_table
.get_mut::<StreamResource>(rid as u32)
.ok_or_else(OpError::bad_resource)?;
.ok_or_else(OpError::bad_resource_id)?;
resource.poll_flush(cx)
})
.await?;

View file

@ -45,7 +45,7 @@ fn op_accept(
state
.resource_table
.get::<TcpListenerResource>(rid)
.ok_or_else(OpError::bad_resource)?;
.ok_or_else(OpError::bad_resource_id)?;
}
let state = state.clone();
@ -250,12 +250,12 @@ fn op_shutdown(
let resource = state
.resource_table
.get_mut::<StreamResource>(rid)
.ok_or_else(OpError::bad_resource)?;
.ok_or_else(OpError::bad_resource_id)?;
match resource {
StreamResource::TcpStream(ref mut stream) => {
TcpStream::shutdown(stream, shutdown_mode).map_err(OpError::from)?;
}
_ => return Err(OpError::bad_resource()),
_ => return Err(OpError::bad_resource_id()),
}
Ok(JsonOp::Sync(json!({})))

View file

@ -27,10 +27,10 @@ fn clone_file(rid: u32, state: &State) -> Result<std::fs::File, OpError> {
let repr = state
.resource_table
.get_mut::<StreamResource>(rid)
.ok_or_else(OpError::bad_resource)?;
.ok_or_else(OpError::bad_resource_id)?;
let file = match repr {
StreamResource::FsFile(ref mut file, _) => file,
_ => return Err(OpError::bad_resource()),
_ => return Err(OpError::bad_resource_id()),
};
let tokio_file = futures::executor::block_on(file.try_clone())?;
let std_file = futures::executor::block_on(tokio_file.into_std());
@ -190,7 +190,7 @@ fn op_run_status(
let resource_table = &mut state.borrow_mut().resource_table;
let child_resource = resource_table
.get_mut::<ChildResource>(rid)
.ok_or_else(OpError::bad_resource)?;
.ok_or_else(OpError::bad_resource_id)?;
let child = &mut child_resource.child;
child.map_err(OpError::from).poll_unpin(cx)
})

View file

@ -57,7 +57,7 @@ fn op_repl_readline(
let resource = state
.resource_table
.get::<ReplResource>(rid)
.ok_or_else(OpError::bad_resource)?;
.ok_or_else(OpError::bad_resource_id)?;
let repl = resource.0.clone();
blocking_json(false, move || {

View file

@ -103,7 +103,7 @@ pub fn op_signal_unbind(
state
.resource_table
.close(rid)
.ok_or_else(OpError::bad_resource)?;
.ok_or_else(OpError::bad_resource_id)?;
Ok(JsonOp::Sync(json!({})))
}

View file

@ -309,7 +309,7 @@ fn op_accept_tls(
let resource = state
.resource_table
.get::<TlsListenerResource>(rid)
.ok_or_else(OpError::bad_resource)
.ok_or_else(OpError::bad_resource_id)
.expect("Can't find tls listener");
resource.tls_acceptor.clone()
};

View file

@ -68,7 +68,7 @@ pub fn op_set_raw(
let state = state_.borrow_mut();
let resource = state.resource_table.get::<StreamResource>(rid);
if resource.is_none() {
return Err(OpError::bad_resource());
return Err(OpError::bad_resource_id());
}
// For now, only stdin.
@ -113,7 +113,7 @@ pub fn op_set_raw(
let mut state = state_.borrow_mut();
let resource = state.resource_table.get_mut::<StreamResource>(rid);
if resource.is_none() {
return Err(OpError::bad_resource());
return Err(OpError::bad_resource_id());
}
if is_raw {
@ -197,7 +197,7 @@ pub fn op_isatty(
let state = state_.borrow_mut();
if !state.resource_table.has(rid) {
return Err(OpError::bad_resource());
return Err(OpError::bad_resource_id());
}
let resource = state.resource_table.get::<StreamResource>(rid);

View file

@ -1,5 +1,5 @@
[WILDCARD]
error: Uncaught BadResource: bad resource id
error: Uncaught BadResource: Bad resource ID
[WILDCARD]dispatch_json.ts:[WILDCARD]
at BadResource ([WILDCARD]errors.ts:[WILDCARD])
at unwrapResponse ([WILDCARD]dispatch_json.ts:[WILDCARD])