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

refactor(ext/io): use concrete error types (#26187)

This commit is contained in:
Leo Kettmeir 2024-10-15 15:36:11 -07:00 committed by GitHub
parent ee904ec06c
commit 82d13fd45b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 36 additions and 43 deletions

View file

@ -2,7 +2,6 @@
use std::rc::Rc; use std::rc::Rc;
use deno_core::error::AnyError;
use deno_core::AsyncRefCell; use deno_core::AsyncRefCell;
use deno_core::AsyncResult; use deno_core::AsyncResult;
use deno_core::CancelHandle; use deno_core::CancelHandle;
@ -71,13 +70,16 @@ impl BiPipeResource {
pub async fn read( pub async fn read(
self: Rc<Self>, self: Rc<Self>,
data: &mut [u8], data: &mut [u8],
) -> Result<usize, AnyError> { ) -> Result<usize, std::io::Error> {
let mut rd = RcRef::map(&self, |r| &r.read_half).borrow_mut().await; let mut rd = RcRef::map(&self, |r| &r.read_half).borrow_mut().await;
let cancel_handle = RcRef::map(&self, |r| &r.cancel); let cancel_handle = RcRef::map(&self, |r| &r.cancel);
Ok(rd.read(data).try_or_cancel(cancel_handle).await?) rd.read(data).try_or_cancel(cancel_handle).await
} }
pub async fn write(self: Rc<Self>, data: &[u8]) -> Result<usize, AnyError> { pub async fn write(
self: Rc<Self>,
data: &[u8],
) -> Result<usize, std::io::Error> {
let mut wr = RcRef::map(self, |r| &r.write_half).borrow_mut().await; let mut wr = RcRef::map(self, |r| &r.write_half).borrow_mut().await;
let nwritten = wr.write(data).await?; let nwritten = wr.write(data).await?;
wr.flush().await?; wr.flush().await?;
@ -270,8 +272,8 @@ impl_async_write!(for BiPipe -> self.write_end);
/// Creates both sides of a bidirectional pipe, returning the raw /// Creates both sides of a bidirectional pipe, returning the raw
/// handles to the underlying OS resources. /// handles to the underlying OS resources.
pub fn bi_pipe_pair_raw() -> Result<(RawBiPipeHandle, RawBiPipeHandle), AnyError> pub fn bi_pipe_pair_raw(
{ ) -> Result<(RawBiPipeHandle, RawBiPipeHandle), std::io::Error> {
#[cfg(unix)] #[cfg(unix)]
{ {
// SockFlag is broken on macOS // SockFlag is broken on macOS
@ -293,7 +295,7 @@ pub fn bi_pipe_pair_raw() -> Result<(RawBiPipeHandle, RawBiPipeHandle), AnyError
) )
}; };
if ret != 0 { if ret != 0 {
return Err(std::io::Error::last_os_error().into()); return Err(std::io::Error::last_os_error());
} }
if cfg!(target_os = "macos") { if cfg!(target_os = "macos") {
@ -389,7 +391,7 @@ pub fn bi_pipe_pair_raw() -> Result<(RawBiPipeHandle, RawBiPipeHandle), AnyError
continue; continue;
} }
return Err(err.into()); return Err(err);
} }
break (path, hd1); break (path, hd1);
@ -411,7 +413,7 @@ pub fn bi_pipe_pair_raw() -> Result<(RawBiPipeHandle, RawBiPipeHandle), AnyError
0, 0,
); );
if hd2 == INVALID_HANDLE_VALUE { if hd2 == INVALID_HANDLE_VALUE {
return Err(io::Error::last_os_error().into()); return Err(io::Error::last_os_error());
} }
// Will not block because we have create the pair. // Will not block because we have create the pair.
@ -419,7 +421,7 @@ pub fn bi_pipe_pair_raw() -> Result<(RawBiPipeHandle, RawBiPipeHandle), AnyError
let err = std::io::Error::last_os_error(); let err = std::io::Error::last_os_error();
if err.raw_os_error() != Some(ERROR_PIPE_CONNECTED as i32) { if err.raw_os_error() != Some(ERROR_PIPE_CONNECTED as i32) {
CloseHandle(hd2); CloseHandle(hd2);
return Err(err.into()); return Err(err);
} }
} }

View file

@ -6,10 +6,6 @@ use std::rc::Rc;
use std::time::SystemTime; use std::time::SystemTime;
use std::time::UNIX_EPOCH; use std::time::UNIX_EPOCH;
use deno_core::error::custom_error;
use deno_core::error::not_supported;
use deno_core::error::resource_unavailable;
use deno_core::error::AnyError;
use deno_core::BufMutView; use deno_core::BufMutView;
use deno_core::BufView; use deno_core::BufView;
use deno_core::OpState; use deno_core::OpState;
@ -59,15 +55,16 @@ impl From<io::ErrorKind> for FsError {
} }
} }
impl From<FsError> for AnyError { impl From<FsError> for deno_core::error::AnyError {
fn from(err: FsError) -> Self { fn from(err: FsError) -> Self {
match err { match err {
FsError::Io(err) => AnyError::from(err), FsError::Io(err) => err.into(),
FsError::FileBusy => resource_unavailable(), FsError::FileBusy => deno_core::error::resource_unavailable(),
FsError::NotSupported => not_supported(), FsError::NotSupported => deno_core::error::not_supported(),
FsError::NotCapable(err) => { FsError::NotCapable(err) => deno_core::error::custom_error(
custom_error("NotCapable", format!("permission denied: {err}")) "NotCapable",
} format!("permission denied: {err}"),
),
} }
} }
} }
@ -266,9 +263,9 @@ impl FileResource {
state: &OpState, state: &OpState,
rid: ResourceId, rid: ResourceId,
f: F, f: F,
) -> Result<R, AnyError> ) -> Result<R, deno_core::error::AnyError>
where where
F: FnOnce(Rc<FileResource>) -> Result<R, AnyError>, F: FnOnce(Rc<FileResource>) -> Result<R, deno_core::error::AnyError>,
{ {
let resource = state.resource_table.get::<FileResource>(rid)?; let resource = state.resource_table.get::<FileResource>(rid)?;
f(resource) f(resource)
@ -277,7 +274,7 @@ impl FileResource {
pub fn get_file( pub fn get_file(
state: &OpState, state: &OpState,
rid: ResourceId, rid: ResourceId,
) -> Result<Rc<dyn File>, AnyError> { ) -> Result<Rc<dyn File>, deno_core::error::AnyError> {
let resource = state.resource_table.get::<FileResource>(rid)?; let resource = state.resource_table.get::<FileResource>(rid)?;
Ok(resource.file()) Ok(resource.file())
} }
@ -286,9 +283,9 @@ impl FileResource {
state: &OpState, state: &OpState,
rid: ResourceId, rid: ResourceId,
f: F, f: F,
) -> Result<R, AnyError> ) -> Result<R, deno_core::error::AnyError>
where where
F: FnOnce(Rc<dyn File>) -> Result<R, AnyError>, F: FnOnce(Rc<dyn File>) -> Result<R, deno_core::error::AnyError>,
{ {
Self::with_resource(state, rid, |r| f(r.file.clone())) Self::with_resource(state, rid, |r| f(r.file.clone()))
} }
@ -303,10 +300,7 @@ impl deno_core::Resource for FileResource {
Cow::Borrowed(&self.name) Cow::Borrowed(&self.name)
} }
fn read( fn read(self: Rc<Self>, limit: usize) -> deno_core::AsyncResult<BufView> {
self: Rc<Self>,
limit: usize,
) -> deno_core::AsyncResult<deno_core::BufView> {
Box::pin(async move { Box::pin(async move {
self self
.file .file
@ -319,8 +313,8 @@ impl deno_core::Resource for FileResource {
fn read_byob( fn read_byob(
self: Rc<Self>, self: Rc<Self>,
buf: deno_core::BufMutView, buf: BufMutView,
) -> deno_core::AsyncResult<(usize, deno_core::BufMutView)> { ) -> deno_core::AsyncResult<(usize, BufMutView)> {
Box::pin(async move { Box::pin(async move {
self self
.file .file
@ -333,17 +327,14 @@ impl deno_core::Resource for FileResource {
fn write( fn write(
self: Rc<Self>, self: Rc<Self>,
buf: deno_core::BufView, buf: BufView,
) -> deno_core::AsyncResult<deno_core::WriteOutcome> { ) -> deno_core::AsyncResult<deno_core::WriteOutcome> {
Box::pin(async move { Box::pin(async move {
self.file.clone().write(buf).await.map_err(|err| err.into()) self.file.clone().write(buf).await.map_err(|err| err.into())
}) })
} }
fn write_all( fn write_all(self: Rc<Self>, buf: BufView) -> deno_core::AsyncResult<()> {
self: Rc<Self>,
buf: deno_core::BufView,
) -> deno_core::AsyncResult<()> {
Box::pin(async move { Box::pin(async move {
self self
.file .file

View file

@ -1,6 +1,5 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
use deno_core::error::AnyError;
use deno_core::op2; use deno_core::op2;
use deno_core::unsync::spawn_blocking; use deno_core::unsync::spawn_blocking;
use deno_core::unsync::TaskQueue; use deno_core::unsync::TaskQueue;
@ -48,6 +47,7 @@ use winapi::um::processenv::GetStdHandle;
#[cfg(windows)] #[cfg(windows)]
use winapi::um::winbase; use winapi::um::winbase;
use deno_core::futures::TryFutureExt;
#[cfg(windows)] #[cfg(windows)]
use parking_lot::Condvar; use parking_lot::Condvar;
#[cfg(windows)] #[cfg(windows)]
@ -348,13 +348,13 @@ where
RcRef::map(self, |r| &r.stream).borrow_mut() RcRef::map(self, |r| &r.stream).borrow_mut()
} }
async fn write(self: Rc<Self>, data: &[u8]) -> Result<usize, AnyError> { async fn write(self: Rc<Self>, data: &[u8]) -> Result<usize, io::Error> {
let mut stream = self.borrow_mut().await; let mut stream = self.borrow_mut().await;
let nwritten = stream.write(data).await?; let nwritten = stream.write(data).await?;
Ok(nwritten) Ok(nwritten)
} }
async fn shutdown(self: Rc<Self>) -> Result<(), AnyError> { async fn shutdown(self: Rc<Self>) -> Result<(), io::Error> {
let mut stream = self.borrow_mut().await; let mut stream = self.borrow_mut().await;
stream.shutdown().await?; stream.shutdown().await?;
Ok(()) Ok(())
@ -396,7 +396,7 @@ where
self.cancel_handle.cancel() self.cancel_handle.cancel()
} }
async fn read(self: Rc<Self>, data: &mut [u8]) -> Result<usize, AnyError> { async fn read(self: Rc<Self>, data: &mut [u8]) -> Result<usize, io::Error> {
let mut rd = self.borrow_mut().await; let mut rd = self.borrow_mut().await;
let nread = rd.read(data).try_or_cancel(self.cancel_handle()).await?; let nread = rd.read(data).try_or_cancel(self.cancel_handle()).await?;
Ok(nread) Ok(nread)
@ -417,7 +417,7 @@ impl Resource for ChildStdinResource {
deno_core::impl_writable!(); deno_core::impl_writable!();
fn shutdown(self: Rc<Self>) -> AsyncResult<()> { fn shutdown(self: Rc<Self>) -> AsyncResult<()> {
Box::pin(self.shutdown()) Box::pin(self.shutdown().map_err(|e| e.into()))
} }
} }
@ -1010,7 +1010,7 @@ pub fn op_print(
state: &mut OpState, state: &mut OpState,
#[string] msg: &str, #[string] msg: &str,
is_err: bool, is_err: bool,
) -> Result<(), AnyError> { ) -> Result<(), deno_core::error::AnyError> {
let rid = if is_err { 2 } else { 1 }; let rid = if is_err { 2 } else { 1 };
FileResource::with_file(state, rid, move |file| { FileResource::with_file(state, rid, move |file| {
Ok(file.write_all_sync(msg.as_bytes())?) Ok(file.write_all_sync(msg.as_bytes())?)