Skip to content

Commit

Permalink
Add failable type
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamRagstad committed Apr 6, 2024
1 parent 9d8cd1e commit 86d3f76
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/engine/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ use crate::{file::project::ProjectConfig, reporting::debug::info, runner::WXMode

use super::runtime::{WXRuntimeError, WXRuntimeMessage};

/// A failable type.
pub type WXFailable<T> = Result<T, WXRuntimeError>;

impl From<std::io::Error> for WXRuntimeError {
fn from(err: std::io::Error) -> Self {
WXRuntimeError {
code: 500,
message: format!("IO error: {}", err),
}
}
}

/// The WebX web server.
pub struct WXServer {
mode: WXMode,
Expand Down Expand Up @@ -64,7 +76,7 @@ impl WXServer {
}

/// Starts the WebX web server and listens for incoming requests in its own thread.
pub async fn run(&mut self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
pub async fn run(&mut self) -> WXFailable<()> {
let listener = tokio::net::TcpListener::bind(&self.addrs()[..]).await?;
let svc = WXSvc::new(self.mode, self.runtime_tx.clone());
self.log_startup();
Expand All @@ -85,13 +97,17 @@ impl WXServer {
/// Serves a single connection.
/// This is the main entry point for each connection to the server
/// and simply passes the connection to the request handler `WXSvc` service.
async fn serve(io: TokioIo<tokio::net::TcpStream>, svc: WXSvc) {
async fn serve(io: TokioIo<tokio::net::TcpStream>, svc: WXSvc) -> WXFailable<()> {
let addr = svc
.address
.expect("No address found while serving connection.");
if let Err(err) = http1::Builder::new().serve_connection(io, svc).await {
eprintln!("failed to serve connection {}: {:?}", addr, err);
return Err(WXRuntimeError {
code: 500,
message: format!("failed to serve connection {}: {:?}", addr, err),
});
}
Ok(())
}
}

Expand Down

0 comments on commit 86d3f76

Please sign in to comment.