Skip to content

Commit

Permalink
Change error returned from start functions to custom enum (#571)
Browse files Browse the repository at this point in the history
This helps future-proofing the API. Thanks @FSMaxB for
suggesting this in #568
  • Loading branch information
msrd0 committed Oct 20, 2021
1 parent d4f3780 commit ecc604d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
2 changes: 1 addition & 1 deletion gotham/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ rand = "0.8"
rand_chacha = "0.3"
regex = "1.0"
serde = { version = "1.0", features = ["derive"] }
thiserror = "1.0"
tokio = { version = "1.11.0", features = ["net", "rt-multi-thread", "time", "fs", "io-util"] }
tokio-rustls = { version = "0.23", optional = true }
uuid = { version = "0.8", features = ["v4"] }

[dev-dependencies]
futures-executor = "0.3.14"
thiserror = "1.0"
tokio = { version = "1.11.0", features = ["macros", "test-util"] }

[package.metadata.docs.rs]
Expand Down
10 changes: 10 additions & 0 deletions gotham/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ use std::future::Future;
use std::io;
use std::net::ToSocketAddrs;
use std::sync::Arc;
use thiserror::Error;
use tokio::io::{AsyncRead, AsyncWrite};
use tokio::net::{TcpListener, TcpStream};
use tokio::runtime::{self, Runtime};
Expand All @@ -74,6 +75,15 @@ pub use plain::*;
#[cfg(feature = "rustls")]
pub use tls::start as start_with_tls;

/// The error that can occur when starting the gotham server.
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum StartError {
/// I/O error.
#[error("I/O Error: {0}")]
IoError(#[from] io::Error),
}

fn new_runtime(threads: usize) -> Runtime {
runtime::Builder::new_multi_thread()
.worker_threads(threads)
Expand Down
13 changes: 8 additions & 5 deletions gotham/src/plain/mod.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
use futures_util::future;
use log::info;
use std::io;
use std::net::ToSocketAddrs;

use super::handler::NewHandler;
use super::{bind_server, new_runtime, tcp_listener};
use super::{bind_server, new_runtime, tcp_listener, StartError};

#[cfg(feature = "testing")]
pub mod test;

/// Starts a Gotham application on plain, unsecured HTTP.
pub fn start<NH, A>(addr: A, new_handler: NH) -> io::Result<()>
pub fn start<NH, A>(addr: A, new_handler: NH) -> Result<(), StartError>
where
NH: NewHandler + 'static,
A: ToSocketAddrs + 'static + Send,
Expand All @@ -19,7 +18,11 @@ where
}

/// Starts a Gotham application with a designated number of threads.
pub fn start_with_num_threads<NH, A>(addr: A, new_handler: NH, threads: usize) -> io::Result<()>
pub fn start_with_num_threads<NH, A>(
addr: A,
new_handler: NH,
threads: usize,
) -> Result<(), StartError>
where
NH: NewHandler + 'static,
A: ToSocketAddrs + 'static + Send,
Expand All @@ -33,7 +36,7 @@ where
/// This is used internally, but exposed in case the developer intends on doing any
/// manual wiring that isn't supported by the Gotham API. It's unlikely that this will
/// be required in most use cases; it's mainly exposed for shutdown handling.
pub async fn init_server<NH, A>(addr: A, new_handler: NH) -> io::Result<()>
pub async fn init_server<NH, A>(addr: A, new_handler: NH) -> Result<(), StartError>
where
NH: NewHandler + 'static,
A: ToSocketAddrs + 'static + Send,
Expand Down
13 changes: 8 additions & 5 deletions gotham/src/tls/mod.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
use futures_util::future::{MapErr, TryFutureExt};
use log::{error, info};
use std::io;
use std::net::ToSocketAddrs;
use std::sync::Arc;
use tokio::net::TcpStream;
use tokio_rustls::{rustls, Accept, TlsAcceptor};

use super::handler::NewHandler;
use super::{bind_server, new_runtime, tcp_listener};
use super::{bind_server, new_runtime, tcp_listener, StartError};

#[cfg(feature = "testing")]
pub mod test;

/// Starts a Gotham application with the default number of threads.
pub fn start<NH, A>(addr: A, new_handler: NH, tls_config: rustls::ServerConfig) -> io::Result<()>
pub fn start<NH, A>(
addr: A,
new_handler: NH,
tls_config: rustls::ServerConfig,
) -> Result<(), StartError>
where
NH: NewHandler + 'static,
A: ToSocketAddrs + 'static + Send,
Expand All @@ -27,7 +30,7 @@ pub fn start_with_num_threads<NH, A>(
new_handler: NH,
tls_config: rustls::ServerConfig,
threads: usize,
) -> io::Result<()>
) -> Result<(), StartError>
where
NH: NewHandler + 'static,
A: ToSocketAddrs + 'static + Send,
Expand All @@ -45,7 +48,7 @@ pub async fn init_server<NH, A>(
addr: A,
new_handler: NH,
tls_config: rustls::ServerConfig,
) -> io::Result<()>
) -> Result<(), StartError>
where
NH: NewHandler + 'static,
A: ToSocketAddrs + 'static + Send,
Expand Down

0 comments on commit ecc604d

Please sign in to comment.