mirror of
https://github.com/denoland/deno.git
synced 2025-09-29 21:54:48 +00:00
refactor(ops): return BadResource errors in ResourceTable calls (#11710)
* refactor(ops): return BadResource errors in ResourceTable calls Instead of relying on callers to map Options to Results via `.ok_or_else(bad_resource_id)` at over 176 different call sites ...
This commit is contained in:
parent
18ff6bb053
commit
2ca454b402
34 changed files with 337 additions and 551 deletions
|
@ -230,8 +230,7 @@ async fn op_seek_async(
|
|||
let resource = state
|
||||
.borrow_mut()
|
||||
.resource_table
|
||||
.get::<StdFileResource>(rid)
|
||||
.ok_or_else(bad_resource_id)?;
|
||||
.get::<StdFileResource>(rid)?;
|
||||
|
||||
if resource.fs_file.is_none() {
|
||||
return Err(bad_resource_id());
|
||||
|
@ -265,8 +264,7 @@ async fn op_fdatasync_async(
|
|||
let resource = state
|
||||
.borrow_mut()
|
||||
.resource_table
|
||||
.get::<StdFileResource>(rid)
|
||||
.ok_or_else(bad_resource_id)?;
|
||||
.get::<StdFileResource>(rid)?;
|
||||
|
||||
if resource.fs_file.is_none() {
|
||||
return Err(bad_resource_id());
|
||||
|
@ -300,8 +298,7 @@ async fn op_fsync_async(
|
|||
let resource = state
|
||||
.borrow_mut()
|
||||
.resource_table
|
||||
.get::<StdFileResource>(rid)
|
||||
.ok_or_else(bad_resource_id)?;
|
||||
.get::<StdFileResource>(rid)?;
|
||||
|
||||
if resource.fs_file.is_none() {
|
||||
return Err(bad_resource_id());
|
||||
|
@ -335,8 +332,7 @@ async fn op_fstat_async(
|
|||
let resource = state
|
||||
.borrow_mut()
|
||||
.resource_table
|
||||
.get::<StdFileResource>(rid)
|
||||
.ok_or_else(bad_resource_id)?;
|
||||
.get::<StdFileResource>(rid)?;
|
||||
|
||||
if resource.fs_file.is_none() {
|
||||
return Err(bad_resource_id());
|
||||
|
@ -1298,8 +1294,7 @@ async fn op_ftruncate_async(
|
|||
let resource = state
|
||||
.borrow_mut()
|
||||
.resource_table
|
||||
.get::<StdFileResource>(rid)
|
||||
.ok_or_else(bad_resource_id)?;
|
||||
.get::<StdFileResource>(rid)?;
|
||||
|
||||
if resource.fs_file.is_none() {
|
||||
return Err(bad_resource_id());
|
||||
|
@ -1580,8 +1575,7 @@ async fn op_futime_async(
|
|||
let resource = state
|
||||
.borrow_mut()
|
||||
.resource_table
|
||||
.get::<StdFileResource>(rid)
|
||||
.ok_or_else(bad_resource_id)?;
|
||||
.get::<StdFileResource>(rid)?;
|
||||
|
||||
if resource.fs_file.is_none() {
|
||||
return Err(bad_resource_id());
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
use crate::permissions::Permissions;
|
||||
use deno_core::error::bad_resource_id;
|
||||
use deno_core::error::AnyError;
|
||||
use deno_core::parking_lot::Mutex;
|
||||
use deno_core::AsyncRefCell;
|
||||
|
@ -135,11 +134,7 @@ async fn op_fs_events_poll(
|
|||
rid: ResourceId,
|
||||
_: (),
|
||||
) -> Result<Option<FsEvent>, AnyError> {
|
||||
let resource = state
|
||||
.borrow()
|
||||
.resource_table
|
||||
.get::<FsEventsResource>(rid)
|
||||
.ok_or_else(bad_resource_id)?;
|
||||
let resource = state.borrow().resource_table.get::<FsEventsResource>(rid)?;
|
||||
let mut receiver = RcRef::map(&resource, |r| &r.receiver).borrow_mut().await;
|
||||
let cancel = RcRef::map(resource, |r| &r.cancel);
|
||||
let maybe_result = receiver.recv().or_cancel(cancel).await?;
|
||||
|
|
|
@ -20,7 +20,7 @@ fn op_http_start(
|
|||
tcp_stream_rid: ResourceId,
|
||||
_: (),
|
||||
) -> Result<ResourceId, AnyError> {
|
||||
if let Some(resource_rc) = state
|
||||
if let Ok(resource_rc) = state
|
||||
.resource_table
|
||||
.take::<TcpStreamResource>(tcp_stream_rid)
|
||||
{
|
||||
|
@ -32,7 +32,7 @@ fn op_http_start(
|
|||
return deno_http::start_http(state, tcp_stream, addr, "http");
|
||||
}
|
||||
|
||||
if let Some(resource_rc) = state
|
||||
if let Ok(resource_rc) = state
|
||||
.resource_table
|
||||
.take::<TlsStreamResource>(tcp_stream_rid)
|
||||
{
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
use deno_core::error::not_supported;
|
||||
use deno_core::error::null_opbuf;
|
||||
use deno_core::error::resource_unavailable;
|
||||
use deno_core::error::AnyError;
|
||||
use deno_core::error::{bad_resource_id, not_supported};
|
||||
use deno_core::op_async;
|
||||
use deno_core::op_sync;
|
||||
use deno_core::AsyncMutFuture;
|
||||
|
@ -334,10 +334,7 @@ impl StdFileResource {
|
|||
F: FnMut(Result<&mut std::fs::File, ()>) -> Result<R, AnyError>,
|
||||
{
|
||||
// First we look up the rid in the resource table.
|
||||
let resource = state
|
||||
.resource_table
|
||||
.get::<StdFileResource>(rid)
|
||||
.ok_or_else(bad_resource_id)?;
|
||||
let resource = state.resource_table.get::<StdFileResource>(rid)?;
|
||||
|
||||
// Sync write only works for FsFile. It doesn't make sense to do this
|
||||
// for non-blocking sockets. So we error out if not FsFile.
|
||||
|
@ -408,11 +405,7 @@ async fn op_read_async(
|
|||
buf: Option<ZeroCopyBuf>,
|
||||
) -> Result<u32, AnyError> {
|
||||
let buf = &mut buf.ok_or_else(null_opbuf)?;
|
||||
let resource = state
|
||||
.borrow()
|
||||
.resource_table
|
||||
.get_any(rid)
|
||||
.ok_or_else(bad_resource_id)?;
|
||||
let resource = state.borrow().resource_table.get_any(rid)?;
|
||||
let nread = if let Some(s) = resource.downcast_rc::<ChildStdoutResource>() {
|
||||
s.read(buf).await?
|
||||
} else if let Some(s) = resource.downcast_rc::<ChildStderrResource>() {
|
||||
|
@ -452,11 +445,7 @@ async fn op_write_async(
|
|||
buf: Option<ZeroCopyBuf>,
|
||||
) -> Result<u32, AnyError> {
|
||||
let buf = &buf.ok_or_else(null_opbuf)?;
|
||||
let resource = state
|
||||
.borrow()
|
||||
.resource_table
|
||||
.get_any(rid)
|
||||
.ok_or_else(bad_resource_id)?;
|
||||
let resource = state.borrow().resource_table.get_any(rid)?;
|
||||
let nwritten = if let Some(s) = resource.downcast_rc::<ChildStdinResource>() {
|
||||
s.write(buf).await?
|
||||
} else if let Some(s) = resource.downcast_rc::<TcpStreamResource>() {
|
||||
|
@ -478,11 +467,7 @@ async fn op_shutdown(
|
|||
rid: ResourceId,
|
||||
_: (),
|
||||
) -> Result<(), AnyError> {
|
||||
let resource = state
|
||||
.borrow()
|
||||
.resource_table
|
||||
.get_any(rid)
|
||||
.ok_or_else(bad_resource_id)?;
|
||||
let resource = state.borrow().resource_table.get_any(rid)?;
|
||||
if let Some(s) = resource.downcast_rc::<ChildStdinResource>() {
|
||||
s.shutdown().await?;
|
||||
} else if let Some(s) = resource.downcast_rc::<TcpStreamResource>() {
|
||||
|
|
|
@ -212,8 +212,7 @@ async fn op_run_status(
|
|||
let resource = state
|
||||
.borrow_mut()
|
||||
.resource_table
|
||||
.get::<ChildResource>(rid)
|
||||
.ok_or_else(bad_resource_id)?;
|
||||
.get::<ChildResource>(rid)?;
|
||||
let mut child = resource.borrow_mut().await;
|
||||
let run_status = child.wait().await?;
|
||||
let code = run_status.code();
|
||||
|
|
|
@ -7,8 +7,6 @@ use deno_core::OpState;
|
|||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
#[cfg(unix)]
|
||||
use deno_core::error::bad_resource_id;
|
||||
#[cfg(unix)]
|
||||
use deno_core::AsyncRefCell;
|
||||
#[cfg(unix)]
|
||||
|
@ -81,8 +79,7 @@ async fn op_signal_poll(
|
|||
let resource = state
|
||||
.borrow_mut()
|
||||
.resource_table
|
||||
.get::<SignalStreamResource>(rid)
|
||||
.ok_or_else(bad_resource_id)?;
|
||||
.get::<SignalStreamResource>(rid)?;
|
||||
let cancel = RcRef::map(&resource, |r| &r.cancel);
|
||||
let mut signal = RcRef::map(&resource, |r| &r.signal).borrow_mut().await;
|
||||
|
||||
|
@ -99,10 +96,7 @@ pub fn op_signal_unbind(
|
|||
_: (),
|
||||
) -> Result<(), AnyError> {
|
||||
super::check_unstable(state, "Deno.signal");
|
||||
state
|
||||
.resource_table
|
||||
.close(rid)
|
||||
.ok_or_else(bad_resource_id)?;
|
||||
state.resource_table.close(rid)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
|
@ -89,10 +89,7 @@ fn op_set_raw(
|
|||
use winapi::shared::minwindef::FALSE;
|
||||
use winapi::um::{consoleapi, handleapi};
|
||||
|
||||
let resource = state
|
||||
.resource_table
|
||||
.get::<StdFileResource>(rid)
|
||||
.ok_or_else(bad_resource_id)?;
|
||||
let resource = state.resource_table.get::<StdFileResource>(rid)?;
|
||||
|
||||
if cbreak {
|
||||
return Err(not_supported());
|
||||
|
@ -156,10 +153,7 @@ fn op_set_raw(
|
|||
{
|
||||
use std::os::unix::io::AsRawFd;
|
||||
|
||||
let resource = state
|
||||
.resource_table
|
||||
.get::<StdFileResource>(rid)
|
||||
.ok_or_else(bad_resource_id)?;
|
||||
let resource = state.resource_table.get::<StdFileResource>(rid)?;
|
||||
|
||||
if resource.fs_file.is_none() {
|
||||
return Err(not_supported());
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue