Skip to content

Commit

Permalink
Return results with errors instead of panicking
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksbgbg committed Feb 5, 2024
1 parent ab3c6ca commit a45d9eb
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
1 change: 1 addition & 0 deletions backend-rs/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
[dependencies]
axum = "0.7.4"
serde = { version = "1.0.196", features = ["derive"] }
thiserror = "1.0.56"
tokio = { version = "1.36.0", features = ["rt-multi-thread"] }
toml-env = "1.1.1"
tower-http = { version = "0.5.1", features = ["trace"] }
Expand Down
16 changes: 12 additions & 4 deletions backend-rs/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use serde::{Deserialize, Serialize};
use thiserror::Error;
use toml_env::Args;

#[derive(Serialize, Deserialize)]
Expand All @@ -12,11 +13,18 @@ pub struct App {
pub port: u16,
}

pub fn load() -> Config {
#[derive(Error, Debug)]
pub enum ConfigError {
#[error("could not load config")]
Load(#[from] toml_env::Error),
#[error("config was not found")]
NotFound,
}

pub fn load() -> Result<Config, ConfigError> {
toml_env::initialize(Args {
config_variable_name: "config",
..Default::default()
})
.expect("could not initialize toml_env")
.expect("could not load config")
})?
.ok_or(ConfigError::NotFound)
}
29 changes: 24 additions & 5 deletions backend-rs/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
mod config;

use crate::config::ConfigError;
use axum::{routing, Router};
use std::io;
use std::net::SocketAddr;
use thiserror::Error;
use tokio::net::TcpListener;
use tower_http::trace::{DefaultMakeSpan, DefaultOnResponse, TraceLayer};
use tracing::{info, Level};

#[derive(Error, Debug)]
enum AppError {
#[error("could not load config")]
LoadConfig(#[from] ConfigError),
#[error("could not bind to network interface")]
BindTcpListener(io::Error),
#[error("could not get TCP listener address")]
GetListenerAddress(io::Error),
#[error("could not start Axum server")]
ServeApp(io::Error),
}

#[tokio::main]
async fn main() {
let config = config::load();
async fn main() -> Result<(), AppError> {
let config = config::load()?;

tracing_subscriber::fmt()
.with_target(false)
Expand All @@ -25,16 +40,20 @@ async fn main() {

let listener = TcpListener::bind(SocketAddr::from((config.app.host, config.app.port)))
.await
.expect("could not bind");
.map_err(AppError::BindTcpListener)?;

info!(
"backend available at: {}",
listener
.local_addr()
.expect("could not get listener address")
.map_err(AppError::GetListenerAddress)?
);

axum::serve(listener, app).await.expect("could not serve");
axum::serve(listener, app)
.await
.map_err(AppError::ServeApp)?;

Ok(())
}

async fn hello_world() -> &'static str {
Expand Down

0 comments on commit a45d9eb

Please sign in to comment.