Skip to content

Commit

Permalink
Add telemetry tracking of errors
Browse files Browse the repository at this point in the history
  • Loading branch information
zargony committed Dec 21, 2024
1 parent 4c854db commit 54985e8
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
1 change: 0 additions & 1 deletion firmware/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ impl fmt::Display for Error {

impl Error {
/// Error kind
#[allow(dead_code)]
pub fn kind(&self) -> &ErrorKind {
&self.kind
}
Expand Down
4 changes: 2 additions & 2 deletions firmware/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ async fn main(spawner: Spawner) {
// Display error to user and try again
Err(err) => {
error!("Initialization error: {:?}", err);
let _ = ui.show_error(&err).await;
let _ = ui.show_error(err).await;
}
}
}
Expand All @@ -273,7 +273,7 @@ async fn main(spawner: Spawner) {
// Display error to user and start over again
Err(err) => {
error!("Error: {:?}", err);
let _ = ui.show_error(&err).await;
let _ = ui.show_error(err).await;
}
}
}
Expand Down
15 changes: 11 additions & 4 deletions firmware/src/telemetry.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::http::Http;
use crate::mixpanel::{self, Mixpanel};
use crate::{article, json, nfc, user};
use crate::{article, error, json, nfc, user};
use alloc::collections::VecDeque;
use embassy_time::{Duration, Instant};
use embedded_io_async::Write;
Expand Down Expand Up @@ -28,6 +28,8 @@ pub enum Event {
UserAuthenticated(user::UserId, nfc::Uid),
/// Article purchased (user id, article id, amount, total price)
ArticlePurchased(user::UserId, article::ArticleId, f32, f32),
/// Error occured
Error(error::Error),
}

impl Event {
Expand All @@ -39,18 +41,20 @@ impl Event {
Event::AuthenticationFailed(..) => "authentication_failed",
Event::UserAuthenticated(..) => "user_authenticated",
Event::ArticlePurchased(..) => "article_purchased",
Event::Error(..) => "error",
}
}

/// User id associated with this event, if any
pub fn user_id(&self) -> Option<&user::UserId> {
pub fn user_id(&self) -> Option<user::UserId> {
#[allow(clippy::match_same_arms)]
match self {
Event::SystemStart => None,
Event::DataRefreshed(..) => None,
Event::AuthenticationFailed(..) => None,
Event::UserAuthenticated(user_id, ..) => Some(user_id),
Event::ArticlePurchased(user_id, ..) => Some(user_id),
Event::UserAuthenticated(user_id, ..) => Some(*user_id),
Event::ArticlePurchased(user_id, ..) => Some(*user_id),
Event::Error(err) => err.user_id(),
}
}

Expand Down Expand Up @@ -85,6 +89,9 @@ impl Event {
.field("total_price", total_price)
.await?;
}
Event::Error(err) => {
object.field("error_message", err.kind()).await?;
}
}
Ok(())
}
Expand Down
6 changes: 4 additions & 2 deletions firmware/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,18 @@ impl<'a, RNG: RngCore, I2C: I2c, IRQ: Wait<Error = Infallible>> Ui<'a, RNG, I2C,
}

/// Show error screen and wait for keypress or timeout
pub async fn show_error(&mut self, error: &Error) -> Result<(), Error> {
pub async fn show_error(&mut self, error: Error) -> Result<(), Error> {
info!("UI: Displaying error: {}", error);

self.display.screen(&screen::Failure::new(error)).await?;
self.display.screen(&screen::Failure::new(&error)).await?;

// Sound the error buzzer if the error was caused by a user's interaction
if error.user_id().is_some() {
let _ = self.buzzer.error().await;
}

self.telemetry.track(Event::Error(error));

// Wait at least 1s without responding to keypad
let min_time = Duration::from_secs(1);
Timer::after(min_time).await;
Expand Down

0 comments on commit 54985e8

Please sign in to comment.