better errors

This commit is contained in:
Alex Janka 2024-01-12 09:09:42 +11:00
parent 7dd4cc43b1
commit 8276728057
4 changed files with 61 additions and 61 deletions

View file

@ -186,7 +186,20 @@ impl TeslaInterface {
return; 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 // refresh our Tesla (the company's web servers, not the car) access token
if Instant::now().duration_since(self.last_refresh) >= KEY_REFRESH_INTERVAL { if Instant::now().duration_since(self.last_refresh) >= KEY_REFRESH_INTERVAL {
log::warn!("refreshing keys..."); log::warn!("refreshing keys...");
match self.api.refresh().await { if self.api.refresh().await.some_or_print().is_some() {
Ok(_) => {
let now = Instant::now(); let now = Instant::now();
match self.save_key() { if self.save_key().some_or_print().is_some() {
Ok(_) => {
log::warn!("refreshed keys...");
self.last_refresh = now; self.last_refresh = now;
} }
Err(e) => error!("error saving auth token: {e:?}"),
}
}
Err(e) => error!("error refreshing auth token: {e:?}"),
} }
} }
} }

View file

@ -1,5 +1,27 @@
use thiserror::Error; use thiserror::Error;
pub trait PrintErrors {
type Inner;
fn some_or_print(self) -> Option<Self::Inner>;
}
impl<T, E> PrintErrors for Result<T, E>
where
E: std::error::Error,
{
type Inner = T;
fn some_or_print(self) -> Option<Self::Inner> {
match self {
Ok(val) => Some(val),
Err(e) => {
log::error!("{e:?}");
None
}
}
}
}
#[derive(Error, Debug)] #[derive(Error, Debug)]
pub enum SaveError { pub enum SaveError {
#[error("stdio error")] #[error("stdio error")]
@ -8,15 +30,6 @@ pub enum SaveError {
RonSpanned(#[from] ron::Error), 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)] #[derive(Error, Debug)]
pub enum AuthLoadError { pub enum AuthLoadError {
#[error("stdio error")] #[error("stdio error")]
@ -31,18 +44,6 @@ pub enum AuthLoadError {
NoVehicles, 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)] #[derive(Error, Debug)]
pub enum RequestError { pub enum RequestError {
#[error("stdio error")] #[error("stdio error")]

View file

@ -5,6 +5,7 @@ extern crate rocket;
use api_interface::TeslaInterface; use api_interface::TeslaInterface;
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
use errors::PrintErrors;
use pl_interface::Pli; use pl_interface::Pli;
use std::path::PathBuf; use std::path::PathBuf;
@ -51,8 +52,8 @@ async fn main() {
ron::ser::to_string_pretty(&Config::default(), Default::default()).unwrap() ron::ser::to_string_pretty(&Config::default(), Default::default()).unwrap()
); );
} }
Commands::Watch => match TeslaInterface::load(auth_path).await { Commands::Watch => {
Ok(mut interface) => { if let Some(mut interface) = TeslaInterface::load(auth_path).await.some_or_print() {
let config: Config = let config: Config =
ron::from_str(&std::fs::read_to_string(&config_path).unwrap()).unwrap(); ron::from_str(&std::fs::read_to_string(&config_path).unwrap()).unwrap();
@ -74,7 +75,7 @@ async fn main() {
_ = interval.tick() => pli.refresh(), _ = interval.tick() => pli.refresh(),
message = pli_receiver.recv() => match message { message = pli_receiver.recv() => match message {
Ok(message) => pli.process_request(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, _ = interval.tick() => interface.refresh().await,
message = api_receiver.recv() => match message { message = api_receiver.recv() => match message {
Ok(message) => interface.process_request(message).await, 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; server_handle.await;
} }
Err(e) => error!("{}", e.error_string()), }
},
} }
} }

View file

@ -9,7 +9,7 @@ use serde::{Deserialize, Serialize};
use serialport::SerialPort; use serialport::SerialPort;
use termcolor::WriteColor; use termcolor::WriteColor;
use crate::errors::PliError; use crate::errors::{PliError, PrintErrors};
pub struct Pli { pub struct Pli {
pub state: Arc<RwLock<PlState>>, pub state: Arc<RwLock<PlState>>,
@ -152,8 +152,7 @@ impl Pli {
} }
pub fn refresh(&mut self) { pub fn refresh(&mut self) {
match self.read_state() { if let Some(new_state) = self.read_state().some_or_print() {
Ok(new_state) => {
self.voltage_gauge.set(new_state.battery_voltage); self.voltage_gauge.set(new_state.battery_voltage);
self.target_voltage_gauge.set(new_state.target_voltage); self.target_voltage_gauge.set(new_state.target_voltage);
self.duty_cycle.set(new_state.duty_cycle); self.duty_cycle.set(new_state.duty_cycle);
@ -165,14 +164,12 @@ impl Pli {
*self.state.write().expect("PLI state handler panicked!!") = new_state; *self.state.write().expect("PLI state handler panicked!!") = new_state;
} }
Err(e) => log::error!("State read error: {e:#?}"),
}
} }
pub fn process_request(&mut self, message: PliRequest) { pub fn process_request(&mut self, message: PliRequest) {
match message { match message {
PliRequest::ReadRam(address) => match self.read_ram(address) { PliRequest::ReadRam(address) => {
Ok(data) => { if let Some(data) = self.read_ram(address).some_or_print() {
let mut stdout = let mut stdout =
termcolor::StandardStream::stdout(termcolor::ColorChoice::Always); termcolor::StandardStream::stdout(termcolor::ColorChoice::Always);
let _ = stdout.set_color( 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<const LENGTH: usize>(&mut self) -> [u8; LENGTH] { fn receive<const LENGTH: usize>(&mut self) -> [u8; LENGTH] {
let mut buf = [0; LENGTH]; let mut buf = [0; LENGTH];
match self.port.read_exact(&mut buf) { let _ = self.port.read_exact(&mut buf).some_or_print();
Ok(_) => {}
Err(e) => log::error!("PLI serial read error: {e:#?}"),
}
buf buf
} }