From ecc604df2ee15b2b7c5ef1abbb928a3a9c803a2a Mon Sep 17 00:00:00 2001 From: msrd0 Date: Wed, 20 Oct 2021 16:20:02 +0200 Subject: [PATCH] Change error returned from start functions to custom enum (#571) This helps future-proofing the API. Thanks @FSMaxB for suggesting this in #568 --- gotham/Cargo.toml | 2 +- gotham/src/lib.rs | 10 ++++++++++ gotham/src/plain/mod.rs | 13 ++++++++----- gotham/src/tls/mod.rs | 13 ++++++++----- 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/gotham/Cargo.toml b/gotham/Cargo.toml index 981446a9..29f3924c 100644 --- a/gotham/Cargo.toml +++ b/gotham/Cargo.toml @@ -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] diff --git a/gotham/src/lib.rs b/gotham/src/lib.rs index e48599d3..2db2ad73 100644 --- a/gotham/src/lib.rs +++ b/gotham/src/lib.rs @@ -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}; @@ -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) diff --git a/gotham/src/plain/mod.rs b/gotham/src/plain/mod.rs index 57d8cc64..abae35d1 100644 --- a/gotham/src/plain/mod.rs +++ b/gotham/src/plain/mod.rs @@ -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(addr: A, new_handler: NH) -> io::Result<()> +pub fn start(addr: A, new_handler: NH) -> Result<(), StartError> where NH: NewHandler + 'static, A: ToSocketAddrs + 'static + Send, @@ -19,7 +18,11 @@ where } /// Starts a Gotham application with a designated number of threads. -pub fn start_with_num_threads(addr: A, new_handler: NH, threads: usize) -> io::Result<()> +pub fn start_with_num_threads( + addr: A, + new_handler: NH, + threads: usize, +) -> Result<(), StartError> where NH: NewHandler + 'static, A: ToSocketAddrs + 'static + Send, @@ -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(addr: A, new_handler: NH) -> io::Result<()> +pub async fn init_server(addr: A, new_handler: NH) -> Result<(), StartError> where NH: NewHandler + 'static, A: ToSocketAddrs + 'static + Send, diff --git a/gotham/src/tls/mod.rs b/gotham/src/tls/mod.rs index 2696574b..d6db42ce 100644 --- a/gotham/src/tls/mod.rs +++ b/gotham/src/tls/mod.rs @@ -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(addr: A, new_handler: NH, tls_config: rustls::ServerConfig) -> io::Result<()> +pub fn start( + addr: A, + new_handler: NH, + tls_config: rustls::ServerConfig, +) -> Result<(), StartError> where NH: NewHandler + 'static, A: ToSocketAddrs + 'static + Send, @@ -27,7 +30,7 @@ pub fn start_with_num_threads( new_handler: NH, tls_config: rustls::ServerConfig, threads: usize, -) -> io::Result<()> +) -> Result<(), StartError> where NH: NewHandler + 'static, A: ToSocketAddrs + 'static + Send, @@ -45,7 +48,7 @@ pub async fn init_server( addr: A, new_handler: NH, tls_config: rustls::ServerConfig, -) -> io::Result<()> +) -> Result<(), StartError> where NH: NewHandler + 'static, A: ToSocketAddrs + 'static + Send,