mirror of
https://github.com/denoland/deno.git
synced 2024-11-21 20:38:55 +00:00
refactor(ext/io): use concrete error types (#26187)
This commit is contained in:
parent
ee904ec06c
commit
82d13fd45b
@ -2,7 +2,6 @@
|
||||
|
||||
use std::rc::Rc;
|
||||
|
||||
use deno_core::error::AnyError;
|
||||
use deno_core::AsyncRefCell;
|
||||
use deno_core::AsyncResult;
|
||||
use deno_core::CancelHandle;
|
||||
@ -71,13 +70,16 @@ impl BiPipeResource {
|
||||
pub async fn read(
|
||||
self: Rc<Self>,
|
||||
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 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 nwritten = wr.write(data).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
|
||||
/// 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)]
|
||||
{
|
||||
// SockFlag is broken on macOS
|
||||
@ -293,7 +295,7 @@ pub fn bi_pipe_pair_raw() -> Result<(RawBiPipeHandle, RawBiPipeHandle), AnyError
|
||||
)
|
||||
};
|
||||
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") {
|
||||
@ -389,7 +391,7 @@ pub fn bi_pipe_pair_raw() -> Result<(RawBiPipeHandle, RawBiPipeHandle), AnyError
|
||||
continue;
|
||||
}
|
||||
|
||||
return Err(err.into());
|
||||
return Err(err);
|
||||
}
|
||||
|
||||
break (path, hd1);
|
||||
@ -411,7 +413,7 @@ pub fn bi_pipe_pair_raw() -> Result<(RawBiPipeHandle, RawBiPipeHandle), AnyError
|
||||
0,
|
||||
);
|
||||
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.
|
||||
@ -419,7 +421,7 @@ pub fn bi_pipe_pair_raw() -> Result<(RawBiPipeHandle, RawBiPipeHandle), AnyError
|
||||
let err = std::io::Error::last_os_error();
|
||||
if err.raw_os_error() != Some(ERROR_PIPE_CONNECTED as i32) {
|
||||
CloseHandle(hd2);
|
||||
return Err(err.into());
|
||||
return Err(err);
|
||||
}
|
||||
}
|
||||
|
||||
|
45
ext/io/fs.rs
45
ext/io/fs.rs
@ -6,10 +6,6 @@ use std::rc::Rc;
|
||||
use std::time::SystemTime;
|
||||
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::BufView;
|
||||
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 {
|
||||
match err {
|
||||
FsError::Io(err) => AnyError::from(err),
|
||||
FsError::FileBusy => resource_unavailable(),
|
||||
FsError::NotSupported => not_supported(),
|
||||
FsError::NotCapable(err) => {
|
||||
custom_error("NotCapable", format!("permission denied: {err}"))
|
||||
}
|
||||
FsError::Io(err) => err.into(),
|
||||
FsError::FileBusy => deno_core::error::resource_unavailable(),
|
||||
FsError::NotSupported => deno_core::error::not_supported(),
|
||||
FsError::NotCapable(err) => deno_core::error::custom_error(
|
||||
"NotCapable",
|
||||
format!("permission denied: {err}"),
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -266,9 +263,9 @@ impl FileResource {
|
||||
state: &OpState,
|
||||
rid: ResourceId,
|
||||
f: F,
|
||||
) -> Result<R, AnyError>
|
||||
) -> Result<R, deno_core::error::AnyError>
|
||||
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)?;
|
||||
f(resource)
|
||||
@ -277,7 +274,7 @@ impl FileResource {
|
||||
pub fn get_file(
|
||||
state: &OpState,
|
||||
rid: ResourceId,
|
||||
) -> Result<Rc<dyn File>, AnyError> {
|
||||
) -> Result<Rc<dyn File>, deno_core::error::AnyError> {
|
||||
let resource = state.resource_table.get::<FileResource>(rid)?;
|
||||
Ok(resource.file())
|
||||
}
|
||||
@ -286,9 +283,9 @@ impl FileResource {
|
||||
state: &OpState,
|
||||
rid: ResourceId,
|
||||
f: F,
|
||||
) -> Result<R, AnyError>
|
||||
) -> Result<R, deno_core::error::AnyError>
|
||||
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()))
|
||||
}
|
||||
@ -303,10 +300,7 @@ impl deno_core::Resource for FileResource {
|
||||
Cow::Borrowed(&self.name)
|
||||
}
|
||||
|
||||
fn read(
|
||||
self: Rc<Self>,
|
||||
limit: usize,
|
||||
) -> deno_core::AsyncResult<deno_core::BufView> {
|
||||
fn read(self: Rc<Self>, limit: usize) -> deno_core::AsyncResult<BufView> {
|
||||
Box::pin(async move {
|
||||
self
|
||||
.file
|
||||
@ -319,8 +313,8 @@ impl deno_core::Resource for FileResource {
|
||||
|
||||
fn read_byob(
|
||||
self: Rc<Self>,
|
||||
buf: deno_core::BufMutView,
|
||||
) -> deno_core::AsyncResult<(usize, deno_core::BufMutView)> {
|
||||
buf: BufMutView,
|
||||
) -> deno_core::AsyncResult<(usize, BufMutView)> {
|
||||
Box::pin(async move {
|
||||
self
|
||||
.file
|
||||
@ -333,17 +327,14 @@ impl deno_core::Resource for FileResource {
|
||||
|
||||
fn write(
|
||||
self: Rc<Self>,
|
||||
buf: deno_core::BufView,
|
||||
buf: BufView,
|
||||
) -> deno_core::AsyncResult<deno_core::WriteOutcome> {
|
||||
Box::pin(async move {
|
||||
self.file.clone().write(buf).await.map_err(|err| err.into())
|
||||
})
|
||||
}
|
||||
|
||||
fn write_all(
|
||||
self: Rc<Self>,
|
||||
buf: deno_core::BufView,
|
||||
) -> deno_core::AsyncResult<()> {
|
||||
fn write_all(self: Rc<Self>, buf: BufView) -> deno_core::AsyncResult<()> {
|
||||
Box::pin(async move {
|
||||
self
|
||||
.file
|
||||
|
@ -1,6 +1,5 @@
|
||||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
use deno_core::error::AnyError;
|
||||
use deno_core::op2;
|
||||
use deno_core::unsync::spawn_blocking;
|
||||
use deno_core::unsync::TaskQueue;
|
||||
@ -48,6 +47,7 @@ use winapi::um::processenv::GetStdHandle;
|
||||
#[cfg(windows)]
|
||||
use winapi::um::winbase;
|
||||
|
||||
use deno_core::futures::TryFutureExt;
|
||||
#[cfg(windows)]
|
||||
use parking_lot::Condvar;
|
||||
#[cfg(windows)]
|
||||
@ -348,13 +348,13 @@ where
|
||||
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 nwritten = stream.write(data).await?;
|
||||
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;
|
||||
stream.shutdown().await?;
|
||||
Ok(())
|
||||
@ -396,7 +396,7 @@ where
|
||||
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 nread = rd.read(data).try_or_cancel(self.cancel_handle()).await?;
|
||||
Ok(nread)
|
||||
@ -417,7 +417,7 @@ impl Resource for ChildStdinResource {
|
||||
deno_core::impl_writable!();
|
||||
|
||||
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,
|
||||
#[string] msg: &str,
|
||||
is_err: bool,
|
||||
) -> Result<(), AnyError> {
|
||||
) -> Result<(), deno_core::error::AnyError> {
|
||||
let rid = if is_err { 2 } else { 1 };
|
||||
FileResource::with_file(state, rid, move |file| {
|
||||
Ok(file.write_all_sync(msg.as_bytes())?)
|
||||
|
Loading…
Reference in New Issue
Block a user