summaryrefslogtreecommitdiff
path: root/cli/ops/tls.rs
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2020-02-23 14:51:29 -0500
committerGitHub <noreply@github.com>2020-02-23 14:51:29 -0500
commit4e1abb4f3a1fbdac25b1e7db0588572e4d5a6579 (patch)
tree644ace7dc1acac7b09bfab037e0ca589fa11987b /cli/ops/tls.rs
parent45eb2f9b37c2c7498c58eb45f76667aaa4a7d731 (diff)
refactor: use OpError instead of ErrBox for errors in ops (#4058)
To better reflect changes in error types in JS from #3662 this PR changes default error type used in ops from "ErrBox" to "OpError". "OpError" is a type that can be sent over to JSON; it has all information needed to construct error in JavaScript. That made "GetErrorKind" trait useless and so it was removed altogether. To provide compatibility with previous use of "ErrBox" an implementation of "From<ErrBox> for OpError" was added, however, it is an escape hatch and ops implementors should strive to use "OpError" directly.
Diffstat (limited to 'cli/ops/tls.rs')
-rw-r--r--cli/ops/tls.rs62
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()
};