From 661258489b3ba1cf9d47c835a383f1b876bccaea Mon Sep 17 00:00:00 2001 From: zer0-star Date: Sat, 16 Dec 2023 22:26:31 +0900 Subject: [PATCH] use anyhow in entrypoint --- Cargo.lock | 7 +++++++ entrypoint/Cargo.toml | 1 + entrypoint/src/main.rs | 13 +++++++------ 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 26e56bb..c246edd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -125,6 +125,12 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "anyhow" +version = "1.0.75" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6" + [[package]] name = "arrayvec" version = "0.7.4" @@ -834,6 +840,7 @@ dependencies = [ name = "entrypoint" version = "0.1.0" dependencies = [ + "anyhow", "bot-client", "handler", "once_cell", diff --git a/entrypoint/Cargo.toml b/entrypoint/Cargo.toml index 4d29cb5..e62db9c 100644 --- a/entrypoint/Cargo.toml +++ b/entrypoint/Cargo.toml @@ -14,3 +14,4 @@ traq-bot-http.workspace = true bot-client.path = "../bot-client" handler.path = "../handler" repository.path = "../repository" +anyhow = "1.0.75" diff --git a/entrypoint/src/main.rs b/entrypoint/src/main.rs index 7031c41..878b6ae 100644 --- a/entrypoint/src/main.rs +++ b/entrypoint/src/main.rs @@ -1,5 +1,6 @@ use std::{env, process::exit}; +use anyhow::{Context, Result}; use once_cell::sync::Lazy; use rocket::{fairing::AdHoc, http::Method, routes}; use traq_bot_http::RequestParser; @@ -20,10 +21,10 @@ static CORS_CONFIG: Lazy = Lazy::new(|| { }); #[tokio::main] -async fn main() -> Result<(), rocket::Error> { +async fn main() -> Result<()> { let verification_token = - env::var("VERIFICATION_TOKEN").expect("env var VERIFICATION_TOKEN is unset"); - let access_token = env::var("BOT_ACCESS_TOKEN").expect("env var BOT_ACCESS_TOKEN is unset"); + env::var("VERIFICATION_TOKEN").context("env var VERIFICATION_TOKEN is unset")?; + let access_token = env::var("BOT_ACCESS_TOKEN").context("env var BOT_ACCESS_TOKEN is unset")?; let check_auth = env::var("CHECK_AUTH") .ok() .and_then(|c| c.parse::().ok()) @@ -35,10 +36,10 @@ async fn main() -> Result<(), rocket::Error> { let config = load("") .or_else(|_| load("MYSQL_")) .or_else(|_| load("NS_MARIADB_")) - .expect("env var config for database not found"); + .context("env var config for database not found")?; CardRepositoryImpl::connect_with_config(config) .await - .expect("failed to connect database") + .context("failed to connect database")? }; let migration_strategy = env::var("MIGRATION") .ok() @@ -47,7 +48,7 @@ async fn main() -> Result<(), rocket::Error> { card_repository .migrate(migration_strategy) .await - .expect("failed white migration"); + .context("failed white migration")?; rocket::build() .mount("/api", routes![handler::ping]) .mount("/api/cards", handler::cards::routes())