From 8276728057ccda939003b35bebcd4b3f243d1bd7 Mon Sep 17 00:00:00 2001 From: Alex Janka Date: Fri, 12 Jan 2024 09:09:42 +1100 Subject: [PATCH] better errors --- src/api_interface.rs | 30 ++++++++++++++++++------------ src/errors.rs | 43 ++++++++++++++++++++++--------------------- src/main.rs | 12 ++++++------ src/pl_interface.rs | 37 +++++++++++++++---------------------- 4 files changed, 61 insertions(+), 61 deletions(-) diff --git a/src/api_interface.rs b/src/api_interface.rs index bea2e27..ea09aa5 100644 --- a/src/api_interface.rs +++ b/src/api_interface.rs @@ -186,7 +186,20 @@ impl TeslaInterface { return; } } - error!("Error getting state: {e:#?}") + + match e { + RequestError::StdIo(e) => log::error!("Stdio error: {e:?}"), + RequestError::RonSpanned(e) => log::error!("RON parsing error: {e:?}"), + RequestError::Teslatte(teslatte::error::TeslatteError::DecodeJsonError { + source: _, + request: _, + body, + }) => { + log::error!("Error getting state: {body}") + } + RequestError::Teslatte(e) => log::error!("Teslatte error: {e:?}"), + RequestError::Save(e) => log::error!("Save error: {e:?}"), + } } } } @@ -195,18 +208,11 @@ impl TeslaInterface { // refresh our Tesla (the company's web servers, not the car) access token if Instant::now().duration_since(self.last_refresh) >= KEY_REFRESH_INTERVAL { log::warn!("refreshing keys..."); - match self.api.refresh().await { - Ok(_) => { - let now = Instant::now(); - match self.save_key() { - Ok(_) => { - log::warn!("refreshed keys..."); - self.last_refresh = now; - } - Err(e) => error!("error saving auth token: {e:?}"), - } + if self.api.refresh().await.some_or_print().is_some() { + let now = Instant::now(); + if self.save_key().some_or_print().is_some() { + self.last_refresh = now; } - Err(e) => error!("error refreshing auth token: {e:?}"), } } } diff --git a/src/errors.rs b/src/errors.rs index 0683823..dbd3dc4 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -1,5 +1,27 @@ use thiserror::Error; +pub trait PrintErrors { + type Inner; + fn some_or_print(self) -> Option; +} + +impl PrintErrors for Result +where + E: std::error::Error, +{ + type Inner = T; + + fn some_or_print(self) -> Option { + match self { + Ok(val) => Some(val), + Err(e) => { + log::error!("{e:?}"); + None + } + } + } +} + #[derive(Error, Debug)] pub enum SaveError { #[error("stdio error")] @@ -8,15 +30,6 @@ pub enum SaveError { RonSpanned(#[from] ron::Error), } -impl SaveError { - pub fn error_string(&self) -> String { - match self { - SaveError::StdIo(e) => format!("Error reading access token from disk: {e:?}"), - SaveError::RonSpanned(e) => format!("Error deserialising access token: {e:?}"), - } - } -} - #[derive(Error, Debug)] pub enum AuthLoadError { #[error("stdio error")] @@ -31,18 +44,6 @@ pub enum AuthLoadError { NoVehicles, } -impl AuthLoadError { - pub fn error_string(&self) -> String { - match self { - AuthLoadError::Teslatte(e) => format!("Error refreshing access token: {e:?}"), - AuthLoadError::StdIo(e) => format!("Error reading access token from disk: {e:?}"), - AuthLoadError::RonSpanned(e) => format!("Error deserialising access token: {e:?}"), - AuthLoadError::Save(e) => e.error_string(), - AuthLoadError::NoVehicles => String::from("No vehicles attached to account"), - } - } -} - #[derive(Error, Debug)] pub enum RequestError { #[error("stdio error")] diff --git a/src/main.rs b/src/main.rs index bb7d7ca..69837dc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ extern crate rocket; use api_interface::TeslaInterface; use clap::{Parser, Subcommand}; +use errors::PrintErrors; use pl_interface::Pli; use std::path::PathBuf; @@ -51,8 +52,8 @@ async fn main() { ron::ser::to_string_pretty(&Config::default(), Default::default()).unwrap() ); } - Commands::Watch => match TeslaInterface::load(auth_path).await { - Ok(mut interface) => { + Commands::Watch => { + if let Some(mut interface) = TeslaInterface::load(auth_path).await.some_or_print() { let config: Config = ron::from_str(&std::fs::read_to_string(&config_path).unwrap()).unwrap(); @@ -74,7 +75,7 @@ async fn main() { _ = interval.tick() => pli.refresh(), message = pli_receiver.recv() => match message { Ok(message) => pli.process_request(message), - Err(e) => error!("Error on receive channel: {e:#?}") + Err(e) => error!("Error on receive channel: {e:?}") } } } @@ -106,7 +107,7 @@ async fn main() { _ = interval.tick() => interface.refresh().await, message = api_receiver.recv() => match message { Ok(message) => interface.process_request(message).await, - Err(e) => error!("Error on receive channel: {e:#?}") + Err(e) => error!("Error on receive channel: {e:?}") } } } @@ -114,7 +115,6 @@ async fn main() { server_handle.await; } - Err(e) => error!("{}", e.error_string()), - }, + } } } diff --git a/src/pl_interface.rs b/src/pl_interface.rs index 9c5433f..f7ed690 100644 --- a/src/pl_interface.rs +++ b/src/pl_interface.rs @@ -9,7 +9,7 @@ use serde::{Deserialize, Serialize}; use serialport::SerialPort; use termcolor::WriteColor; -use crate::errors::PliError; +use crate::errors::{PliError, PrintErrors}; pub struct Pli { pub state: Arc>, @@ -152,27 +152,24 @@ impl Pli { } pub fn refresh(&mut self) { - match self.read_state() { - Ok(new_state) => { - self.voltage_gauge.set(new_state.battery_voltage); - self.target_voltage_gauge.set(new_state.target_voltage); - self.duty_cycle.set(new_state.duty_cycle); - self.internal_charge_current - .set(new_state.internal_charge_current); - self.internal_load_current - .set(new_state.internal_load_current); - self.regulator_gauges.set(&new_state.regulator_state); + if let Some(new_state) = self.read_state().some_or_print() { + self.voltage_gauge.set(new_state.battery_voltage); + self.target_voltage_gauge.set(new_state.target_voltage); + self.duty_cycle.set(new_state.duty_cycle); + self.internal_charge_current + .set(new_state.internal_charge_current); + self.internal_load_current + .set(new_state.internal_load_current); + self.regulator_gauges.set(&new_state.regulator_state); - *self.state.write().expect("PLI state handler panicked!!") = new_state; - } - Err(e) => log::error!("State read error: {e:#?}"), + *self.state.write().expect("PLI state handler panicked!!") = new_state; } } pub fn process_request(&mut self, message: PliRequest) { match message { - PliRequest::ReadRam(address) => match self.read_ram(address) { - Ok(data) => { + PliRequest::ReadRam(address) => { + if let Some(data) = self.read_ram(address).some_or_print() { let mut stdout = termcolor::StandardStream::stdout(termcolor::ColorChoice::Always); let _ = stdout.set_color( @@ -184,8 +181,7 @@ impl Pli { ); }; } - Err(e) => log::error!("RAM read error: {e:?}"), - }, + } } } @@ -208,10 +204,7 @@ impl Pli { fn receive(&mut self) -> [u8; LENGTH] { let mut buf = [0; LENGTH]; - match self.port.read_exact(&mut buf) { - Ok(_) => {} - Err(e) => log::error!("PLI serial read error: {e:#?}"), - } + let _ = self.port.read_exact(&mut buf).some_or_print(); buf }