Skip to content

Commit

Permalink
HttpsProxy::add_listener returns Result
Browse files Browse the repository at this point in the history
  • Loading branch information
Keksoj committed Feb 23, 2024
1 parent 129c0d7 commit ba8b51a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
16 changes: 10 additions & 6 deletions lib/src/https.rs
Original file line number Diff line number Diff line change
Expand Up @@ -793,15 +793,19 @@ impl HttpsProxy {
}
}

pub fn add_listener(&mut self, config: HttpsListenerConfig, token: Token) -> Option<Token> {
pub fn add_listener(
&mut self,
config: HttpsListenerConfig,
token: Token,
) -> Result<Token, ProxyError> {
match self.listeners.entry(token) {
Entry::Vacant(entry) => {
entry.insert(Rc::new(RefCell::new(
HttpsListener::try_new(config, token).ok()?,
)));
Some(token)
let https_listener =
HttpsListener::try_new(config, token).map_err(ProxyError::AddListener)?;
entry.insert(Rc::new(RefCell::new(https_listener)));
Ok(token)
}
_ => None,
_ => Err(ProxyError::ListenerAlreadyPresent),
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ pub enum ProxyError {
NoListenerFound(SocketAddr),
#[error("a listener is already present for this token")]
ListenerAlreadyPresent,
#[error("could not create add listener: {0}")]
#[error("could not add listener: {0}")]
AddListener(ListenerError),
#[error("failed to activate listener with address {address:?}: {listener_error}")]
ListenerActivation {
Expand Down
13 changes: 7 additions & 6 deletions lib/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1106,7 +1106,7 @@ impl Server {
WorkerResponse::ok(req_id)
}
Err(e) => {
let error = format!("Couldn't add HTTP listener: {e}");
let error = format!("Could not add HTTP listener: {e}");
error!("{}", error);
WorkerResponse::error(req_id, error)
}
Expand All @@ -1133,16 +1133,17 @@ impl Server {
.borrow_mut()
.add_listener(listener.clone(), token)
{
Some(_token) => {
Ok(_token) => {
entry.insert(Rc::new(RefCell::new(ListenSession {
protocol: Protocol::HTTPSListen,
})));
self.base_sessions_count += 1;
WorkerResponse::ok(req_id)
}
None => {
error!("Couldn't add HTTPS listener");
WorkerResponse::error(req_id, "cannot add HTTPS listener")
Err(e) => {
let error = format!("Could not add HTTPS listener: {e}");
error!("{}", error);
WorkerResponse::error(req_id, error)
}
}
}
Expand Down Expand Up @@ -1175,7 +1176,7 @@ impl Server {
WorkerResponse::ok(req_id)
}
Err(e) => {
let error = format!("Couldn't add TCP listener: {e}");
let error = format!("Could not add TCP listener: {e}");
error!("{}", error);
WorkerResponse::error(req_id, error)
}
Expand Down

0 comments on commit ba8b51a

Please sign in to comment.