Replaced expect with error handling

This commit is contained in:
Timo Ley 2022-01-04 20:06:57 +01:00
parent 0977a5ffcb
commit fcce7d07c8
3 changed files with 24 additions and 7 deletions

View file

@ -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"] }

14
src/error.rs Normal file
View file

@ -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),
}

View file

@ -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>(&config).expect("Config file parsing error");
let config = std::fs::read(&opt.config)?;
let config = toml::from_slice::<Config>(&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(())
}