From fcce7d07c8431ad4f0050704961bbcefe465c8e1 Mon Sep 17 00:00:00 2001 From: Timo Ley Date: Tue, 4 Jan 2022 20:06:57 +0100 Subject: [PATCH] Replaced expect with error handling --- Cargo.toml | 1 + src/error.rs | 14 ++++++++++++++ src/main.rs | 16 +++++++++------- 3 files changed, 24 insertions(+), 7 deletions(-) create mode 100644 src/error.rs diff --git a/Cargo.toml b/Cargo.toml index 288d6fd..97db21b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ edition = "2018" [dependencies] tokio = { version = "1.0", features = ["full"] } axum = { version = "0.2.8", features = ["headers", "multipart"] } +hyper = "0.14.16" tower = { version = "0.4", features = ["util", "timeout"] } tower-http = { version = "0.1", features = ["add-extension", "trace", "fs", "set-header"] } serde = { version = "1.0", features = ["derive"] } diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..e14bbfc --- /dev/null +++ b/src/error.rs @@ -0,0 +1,14 @@ +use thiserror::Error; + + +#[derive(Error, Debug)] +pub enum JMError { + #[error("File read error: {0}")] + Read(#[from] std::io::Error), + #[error("Deserialize error: {0}")] + Deserialize(#[from] toml::de::Error), + #[error("Database connection error: {0}")] + Database(#[from] sqlx::Error), + #[error("Axum error: {0}")] + Axum(#[from] hyper::Error), +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index b410d08..d035681 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ use axum::{ Router, }; use config::Config; +use error::JMError; use sqlx::MySqlPool; use std::path::PathBuf; use structopt::StructOpt; @@ -13,6 +14,7 @@ mod cdn; mod config; mod ipfs; mod v1; +mod error; #[derive(StructOpt)] struct Opt { @@ -26,14 +28,13 @@ struct Opt { } #[tokio::main] -async fn main() { +async fn main() -> Result<(), JMError> { let opt = Opt::from_args(); - let config = std::fs::read(&opt.config).expect("Config file reading error"); - let config = toml::from_slice::(&config).expect("Config file parsing error"); + let config = std::fs::read(&opt.config)?; + let config = toml::from_slice::(&config)?; let db_pool = MySqlPool::new(&config.database) - .await - .expect("Database connection error"); + .await?; let app = Router::new() .nest("/api/v1", v1::routes()) @@ -47,6 +48,7 @@ async fn main() { axum::Server::bind(&config.addr) .serve(app.into_make_service()) - .await - .expect("Something went wrong :("); + .await?; + + Ok(()) }