Skip to content

Commit

Permalink
Improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
DanNixon committed Apr 28, 2024
1 parent 8a95f20 commit 59aa2e0
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 31 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

7 changes: 7 additions & 0 deletions adapter/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use metrics_exporter_prometheus::{BuildError, PrometheusBuilder};
pub(crate) static REQUESTS: &str = "emf_schedule_adapter_requests";
pub(crate) static ENDPOINT_LABEL: &str = "endpoint";

pub(crate) static UPSTREAM_API_FAILURES: &str = "emf_schedule_adapter_upstream_api_failures";

pub(super) fn init(address: SocketAddr) -> Result<(), BuildError> {
info!("Starting observability server on {address}");

Expand All @@ -16,5 +18,10 @@ pub(super) fn init(address: SocketAddr) -> Result<(), BuildError> {

describe_counter!(REQUESTS, "Number of requests made to each API endpoint");

describe_counter!(
UPSTREAM_API_FAILURES,
"Number of requests to the upstream schedule API that have failed"
);

result
}
25 changes: 16 additions & 9 deletions adapter/src/queries/now_and_next.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use axum::{
extract::State,
http::StatusCode,
response::{IntoResponse, Response},
Json,
};
Expand All @@ -8,7 +9,7 @@ use chrono::{DateTime, FixedOffset, Local};
use emfcamp_schedule_api::schedule::mutation;
use metrics::counter;
use serde::{Deserialize, Serialize};
use tracing::info;
use tracing::{error, info};

#[derive(Debug, Deserialize, Serialize)]
pub(crate) struct NowAndNextQueryParams {
Expand Down Expand Up @@ -50,14 +51,20 @@ pub(crate) async fn now_and_next(
counter!(crate::metrics::REQUESTS, crate::metrics::ENDPOINT_LABEL => "now_and_next")
.increment(1);

let mut schedule = state.client.get_schedule().await;
match state.client.get_schedule().await {
Ok(mut schedule) => {
let now = query.now.unwrap_or_else(|| Local::now().into());

let now = query.now.unwrap_or_else(|| Local::now().into());
let mutators = query.into();
schedule.mutate(&mutators);

let mutators = query.into();
schedule.mutate(&mutators);

let epg = schedule.now_and_next(now);

Json(epg).into_response()
let epg = schedule.now_and_next(now);
Json(epg).into_response()
}
Err(err) => {
error!("{err}");
counter!(crate::metrics::UPSTREAM_API_FAILURES).increment(1);
(StatusCode::INTERNAL_SERVER_ERROR).into_response()
}
}
}
23 changes: 15 additions & 8 deletions adapter/src/queries/schedule.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use axum::{
extract::State,
http::StatusCode,
response::{IntoResponse, Response},
Json,
};
Expand All @@ -8,7 +9,7 @@ use chrono::{DateTime, FixedOffset};
use emfcamp_schedule_api::schedule::mutation;
use metrics::counter;
use serde::{Deserialize, Serialize};
use tracing::info;
use tracing::{error, info};

#[derive(Debug, Deserialize, Serialize)]
pub(crate) struct ScheduleQueryParams {
Expand Down Expand Up @@ -59,12 +60,18 @@ pub(crate) async fn schedule(
info!("Query: schedule: {:?}", query);
counter!(crate::metrics::REQUESTS, crate::metrics::ENDPOINT_LABEL => "schedule").increment(1);

let mut schedule = state.client.get_schedule().await;
match state.client.get_schedule().await {
Ok(mut schedule) => {
let mutators = query.into();
schedule.mutate(&mutators);

let mutators = query.into();
schedule.mutate(&mutators);

let events = &mut schedule.events;

Json(events).into_response()
let events = &mut schedule.events;
Json(events).into_response()
}
Err(err) => {
error!("{err}");
counter!(crate::metrics::UPSTREAM_API_FAILURES).increment(1);
(StatusCode::INTERNAL_SERVER_ERROR).into_response()
}
}
}
19 changes: 13 additions & 6 deletions adapter/src/queries/venues.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
use axum::{
extract::State,
http::StatusCode,
response::{IntoResponse, Response},
Json,
};
use metrics::counter;
use tracing::info;
use tracing::{error, info};

#[axum::debug_handler]
pub(crate) async fn venues(State(state): State<crate::State>) -> Response {
info!("Query: venues");
counter!(crate::metrics::REQUESTS, crate::metrics::ENDPOINT_LABEL => "venues").increment(1);

let schedule = state.client.get_schedule().await;

let venues = schedule.venues();

Json(venues).into_response()
match state.client.get_schedule().await {
Ok(schedule) => {
let venues = schedule.venues();
Json(venues).into_response()
}
Err(err) => {
error!("{err}");
counter!(crate::metrics::UPSTREAM_API_FAILURES).increment(1);
(StatusCode::INTERNAL_SERVER_ERROR).into_response()
}
}
}
1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ version.workspace = true
edition.workspace = true

[dependencies]
anyhow.workspace = true
ascii_table.workspace = true
chrono.workspace = true
clap.workspace = true
Expand Down
7 changes: 5 additions & 2 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod commands;
mod formatting;

use anyhow::Result;
use clap::{CommandFactory, Parser, Subcommand};
use clap_complete::Shell;
use url::Url;
Expand Down Expand Up @@ -46,12 +47,12 @@ enum Command {
}

#[tokio::main]
async fn main() {
async fn main() -> Result<()> {
let args = Cli::parse();

let client = emfcamp_schedule_api::Client::new(args.api_url);

let schedule = client.get_schedule().await;
let schedule = client.get_schedule().await?;

match args.command {
Command::Full(args) => commands::full::run(args, schedule),
Expand All @@ -61,6 +62,8 @@ async fn main() {
Command::Venues => commands::venues::run(schedule),
Command::ShellCompletions { shell } => print_shell_completions(shell),
}

Ok(())
}

fn print_shell_completions(shell: Shell) {
Expand Down
11 changes: 5 additions & 6 deletions client/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::schedule::{event::Event, Schedule};
use reqwest::Error;
use url::Url;

#[derive(Debug, Clone)]
Expand All @@ -11,14 +12,12 @@ impl Client {
Self { url }
}

pub async fn get_schedule(&self) -> Schedule {
pub async fn get_schedule(&self) -> Result<Schedule, Error> {
let events = reqwest::get(self.url.clone())
.await
.unwrap()
.await?
.json::<Vec<Event>>()
.await
.unwrap();
.await?;

Schedule { events }
Ok(Schedule { events })
}
}

0 comments on commit 59aa2e0

Please sign in to comment.