diff options
Diffstat (limited to 'cli/ops/tls.rs')
-rw-r--r-- | cli/ops/tls.rs | 62 |
1 files changed, 23 insertions, 39 deletions
diff --git a/cli/ops/tls.rs b/cli/ops/tls.rs index 6dbe99c85..8c648faaf 100644 --- a/cli/ops/tls.rs +++ b/cli/ops/tls.rs @@ -1,9 +1,7 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. use super::dispatch_json::{Deserialize, JsonOp, Value}; use super::io::StreamResource; -use crate::deno_error::bad_resource; -use crate::deno_error::DenoError; -use crate::deno_error::ErrorKind; +use crate::op_error::OpError; use crate::ops::json_op; use crate::resolve_addr::resolve_addr; use crate::state::State; @@ -63,7 +61,7 @@ pub fn op_connect_tls( state: &State, args: Value, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<JsonOp, ErrBox> { +) -> Result<JsonOp, OpError> { let args: ConnectTLSArgs = serde_json::from_value(args)?; let cert_file = args.cert_file.clone(); let state_ = state.clone(); @@ -118,35 +116,31 @@ pub fn op_connect_tls( Ok(JsonOp::Async(op.boxed_local())) } -fn load_certs(path: &str) -> Result<Vec<Certificate>, ErrBox> { +fn load_certs(path: &str) -> Result<Vec<Certificate>, OpError> { let cert_file = File::open(path)?; let reader = &mut BufReader::new(cert_file); - let certs = certs(reader).map_err(|_| { - DenoError::new(ErrorKind::Other, "Unable to decode certificate".to_string()) - })?; + let certs = certs(reader) + .map_err(|_| OpError::other("Unable to decode certificate".to_string()))?; if certs.is_empty() { - let e = DenoError::new( - ErrorKind::Other, - "No certificates found in cert file".to_string(), - ); - return Err(ErrBox::from(e)); + let e = OpError::other("No certificates found in cert file".to_string()); + return Err(e); } Ok(certs) } -fn key_decode_err() -> DenoError { - DenoError::new(ErrorKind::Other, "Unable to decode key".to_string()) +fn key_decode_err() -> OpError { + OpError::other("Unable to decode key".to_string()) } -fn key_not_found_err() -> DenoError { - DenoError::new(ErrorKind::Other, "No keys found in key file".to_string()) +fn key_not_found_err() -> OpError { + OpError::other("No keys found in key file".to_string()) } /// Starts with -----BEGIN RSA PRIVATE KEY----- -fn load_rsa_keys(path: &str) -> Result<Vec<PrivateKey>, ErrBox> { +fn load_rsa_keys(path: &str) -> Result<Vec<PrivateKey>, OpError> { let key_file = File::open(path)?; let reader = &mut BufReader::new(key_file); let keys = rsa_private_keys(reader).map_err(|_| key_decode_err())?; @@ -154,14 +148,14 @@ fn load_rsa_keys(path: &str) -> Result<Vec<PrivateKey>, ErrBox> { } /// Starts with -----BEGIN PRIVATE KEY----- -fn load_pkcs8_keys(path: &str) -> Result<Vec<PrivateKey>, ErrBox> { +fn load_pkcs8_keys(path: &str) -> Result<Vec<PrivateKey>, OpError> { let key_file = File::open(path)?; let reader = &mut BufReader::new(key_file); let keys = pkcs8_private_keys(reader).map_err(|_| key_decode_err())?; Ok(keys) } -fn load_keys(path: &str) -> Result<Vec<PrivateKey>, ErrBox> { +fn load_keys(path: &str) -> Result<Vec<PrivateKey>, OpError> { let path = path.to_string(); let mut keys = load_rsa_keys(&path)?; @@ -170,7 +164,7 @@ fn load_keys(path: &str) -> Result<Vec<PrivateKey>, ErrBox> { } if keys.is_empty() { - return Err(ErrBox::from(key_not_found_err())); + return Err(key_not_found_err()); } Ok(keys) @@ -195,17 +189,13 @@ impl TlsListenerResource { /// can be notified when listener is closed. /// /// Throws an error if another task is already tracked. - pub fn track_task(&mut self, cx: &Context) -> Result<(), ErrBox> { + pub fn track_task(&mut self, cx: &Context) -> Result<(), OpError> { // Currently, we only allow tracking a single accept task for a listener. // This might be changed in the future with multiple workers. // Caveat: TcpListener by itself also only tracks an accept task at a time. // See https://github.com/tokio-rs/tokio/issues/846#issuecomment-454208883 if self.waker.is_some() { - let e = std::io::Error::new( - std::io::ErrorKind::Other, - "Another accept task is ongoing", - ); - return Err(ErrBox::from(e)); + return Err(OpError::other("Another accept task is ongoing".to_string())); } let waker = futures::task::AtomicWaker::new(); @@ -244,7 +234,7 @@ fn op_listen_tls( state: &State, args: Value, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<JsonOp, ErrBox> { +) -> Result<JsonOp, OpError> { let args: ListenTlsArgs = serde_json::from_value(args)?; assert_eq!(args.transport, "tcp"); @@ -308,7 +298,7 @@ pub struct AcceptTls { } impl Future for AcceptTls { - type Output = Result<(TcpStream, SocketAddr), ErrBox>; + type Output = Result<(TcpStream, SocketAddr), OpError>; fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> { let inner = self.get_mut(); @@ -320,17 +310,11 @@ impl Future for AcceptTls { let listener_resource = state .resource_table .get_mut::<TlsListenerResource>(inner.rid) - .ok_or_else(|| { - let e = std::io::Error::new( - std::io::ErrorKind::Other, - "Listener has been closed", - ); - ErrBox::from(e) - })?; + .ok_or_else(|| OpError::other("Listener has been closed".to_string()))?; let listener = &mut listener_resource.listener; - match listener.poll_accept(cx).map_err(ErrBox::from) { + match listener.poll_accept(cx).map_err(OpError::from) { Poll::Ready(Ok((stream, addr))) => { listener_resource.untrack_task(); inner.accept_state = AcceptTlsState::Done; @@ -358,7 +342,7 @@ fn op_accept_tls( state: &State, args: Value, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<JsonOp, ErrBox> { +) -> Result<JsonOp, OpError> { let args: AcceptTlsArgs = serde_json::from_value(args)?; let rid = args.rid as u32; let state = state.clone(); @@ -371,7 +355,7 @@ fn op_accept_tls( let resource = state .resource_table .get::<TlsListenerResource>(rid) - .ok_or_else(bad_resource) + .ok_or_else(OpError::bad_resource) .expect("Can't find tls listener"); resource.tls_acceptor.clone() }; |