some error handling

This commit is contained in:
Adam 2024-05-05 05:32:16 -04:00
parent 30d4388063
commit a1fb258db4
4 changed files with 21 additions and 9 deletions

7
Cargo.lock generated
View file

@ -26,6 +26,12 @@ dependencies = [
"memchr",
]
[[package]]
name = "anyhow"
version = "1.0.82"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f538837af36e6f6a9be0faa67f9a314f8119e4e4b5867c6ab40ed60360142519"
[[package]]
name = "async-trait"
version = "0.1.80"
@ -153,6 +159,7 @@ checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9"
name = "cards"
version = "0.1.0"
dependencies = [
"anyhow",
"axum",
"futures",
"rand",

View file

@ -13,3 +13,4 @@ tokio = { version = "1", features = ["full"] }
tower = { version = "0.4", features = ["util"] }
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
anyhow = "1.0.82"

View file

@ -2,7 +2,7 @@ use crate::message_handler::*; //change this
use rand::prelude::IteratorRandom;
use rand::thread_rng;
use serde::{Deserialize, Serialize};
use serde_json::Result;
use anyhow::Result;
/// A CAH white card
#[derive(Debug, Serialize, Deserialize)]

View file

@ -1,10 +1,9 @@
use anyhow::{Context, Result};
use axum::{response::Html, routing::get, Router};
use std::{
// collections::HashSet,
error::Error,
fs,
net::SocketAddr,
result::Result,
sync::{Arc, Mutex},
};
use tokio::sync::broadcast;
@ -16,16 +15,18 @@ use crate::api::*;
use crate::message_handler::*;
/// Parse json for card data
fn load_json(path: &str) -> Result<Vec<CAHCardSet>, Box<dyn Error>> {
let data: String = fs::read_to_string(path)?;
let jayson: Vec<CAHCardSet> = serde_json::from_str(&data)?;
fn load_json(path: &str) -> Result<Vec<CAHCardSet>> {
let data: String =
fs::read_to_string(path).with_context(|| format!("Invalid JSON path: \"{}\"", path))?;
let jayson: Vec<CAHCardSet> =
serde_json::from_str(&data).with_context(|| format!("\"{path}\" is invalid json"))?;
Ok(jayson)
}
// this is still around just for reference
#[allow(dead_code)]
fn test() -> Result<(), Box<dyn Error>> {
fn test() -> Result<()> {
// choose decks
let cards_input_path: &str = "data/cah-cards-full.json";
@ -96,7 +97,7 @@ async fn spawnclients() -> Html<&'static str> {
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
async fn main() -> Result<()> {
// stuff for logging
tracing_subscriber::registry()
.with(
@ -127,7 +128,10 @@ async fn main() -> Result<(), Box<dyn Error>> {
.with_state(app_state);
// send it
let listener = tokio::net::TcpListener::bind("0.0.0.0:3030").await?;
let address = "0.0.0.0:3030";
let listener = tokio::net::TcpListener::bind(address)
.await
.with_context(|| format!("{} is not a valid bind address.", address))?;
tracing::debug!("listening on {}", listener.local_addr()?);
axum::serve(
listener,