main refactor: add error enum

This commit is contained in:
topquark12 2021-06-07 09:46:31 +08:00
parent 677cdbdc12
commit 2731df7813
2 changed files with 13 additions and 8 deletions

View File

@ -50,7 +50,9 @@ pub enum Handler {
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub enum Error { pub enum Error {
ReportError,
PostFilterRateError,
FlashError
} }
fn send_line(socket: &mut TcpSocket, data: &[u8]) -> bool { fn send_line(socket: &mut TcpSocket, data: &[u8]) -> bool {
@ -99,7 +101,7 @@ impl Handler {
Err(e) => { Err(e) => {
error!("unable to serialize report: {:?}", e); error!("unable to serialize report: {:?}", e);
let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e); let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e);
return Err(Error::ReportError);
} }
} }
Ok(Handler::Handled) Ok(Handler::Handled)
@ -113,6 +115,7 @@ impl Handler {
Err(e) => { Err(e) => {
error!("unable to serialize pid summary: {:?}", e); error!("unable to serialize pid summary: {:?}", e);
let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e); let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e);
return Err(Error::ReportError);
} }
} }
Ok(Handler::Handled) Ok(Handler::Handled)
@ -126,6 +129,7 @@ impl Handler {
Err(e) => { Err(e) => {
error!("unable to serialize pwm summary: {:?}", e); error!("unable to serialize pwm summary: {:?}", e);
let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e); let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e);
return Err(Error::ReportError);
} }
} }
Ok(Handler::Handled) Ok(Handler::Handled)
@ -139,6 +143,7 @@ impl Handler {
Err(e) => { Err(e) => {
error!("unable to serialize steinhart-hart summaries: {:?}", e); error!("unable to serialize steinhart-hart summaries: {:?}", e);
let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e); let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e);
return Err(Error::ReportError);
} }
} }
Ok(Handler::Handled) Ok(Handler::Handled)
@ -152,6 +157,7 @@ impl Handler {
Err(e) => { Err(e) => {
error!("unable to serialize postfilter summary: {:?}", e); error!("unable to serialize postfilter summary: {:?}", e);
let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e); let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e);
return Err(Error::ReportError);
} }
} }
Ok(Handler::Handled) Ok(Handler::Handled)
@ -262,6 +268,7 @@ impl Handler {
None => { None => {
error!("unable to choose postfilter for rate {:.3}", rate); error!("unable to choose postfilter for rate {:.3}", rate);
send_line(socket, b"{{\"error\": \"unable to choose postfilter rate\"}}"); send_line(socket, b"{{\"error\": \"unable to choose postfilter rate\"}}");
return Err(Error::PostFilterRateError);
} }
} }
Ok(Handler::Handled) Ok(Handler::Handled)
@ -282,6 +289,7 @@ impl Handler {
Err(e) => { Err(e) => {
error!("unable to load config from flash: {:?}", e); error!("unable to load config from flash: {:?}", e);
let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e); let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e);
return Err(Error::FlashError);
} }
} }
} }
@ -301,6 +309,7 @@ impl Handler {
Err(e) => { Err(e) => {
error!("unable to save channel {} config to flash: {:?}", c, e); error!("unable to save channel {} config to flash: {:?}", c, e);
let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e); let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e);
return Err(Error::FlashError);
} }
} }
} }

View File

@ -4,7 +4,6 @@
#![cfg_attr(test, allow(unused))] #![cfg_attr(test, allow(unused))]
// TODO: #![deny(warnings, unused)] // TODO: #![deny(warnings, unused)]
// use Handler::NewIPV4;
#[cfg(not(any(feature = "semihosting", test)))] #[cfg(not(any(feature = "semihosting", test)))]
use panic_abort as _; use panic_abort as _;
#[cfg(all(feature = "semihosting", not(test)))] #[cfg(all(feature = "semihosting", not(test)))]
@ -12,7 +11,6 @@ use panic_semihosting as _;
use log::{error, info, warn}; use log::{error, info, warn};
// use core::fmt::Write;
use cortex_m::asm::wfi; use cortex_m::asm::wfi;
use cortex_m_rt::entry; use cortex_m_rt::entry;
use stm32f4xx_hal::{ use stm32f4xx_hal::{
@ -209,10 +207,8 @@ fn main() -> ! {
Ok(SessionInput::Nothing) => {} Ok(SessionInput::Nothing) => {}
Ok(SessionInput::Command(command)) => { Ok(SessionInput::Command(command)) => {
match Handler::handle_command(command, &mut socket, &mut channels, session, &mut leds, &mut store, &mut ipv4_config) { match Handler::handle_command(command, &mut socket, &mut channels, session, &mut leds, &mut store, &mut ipv4_config) {
Ok(Handler::NewIPV4(ip)) => { Ok(Handler::NewIPV4(ip)) => new_ipv4_config = Some(ip),
new_ipv4_config = Some(ip); Ok(Handler::Handled) => {},
}
Ok(Handler::Handled) => {}
Ok(Handler::CloseSocket) => socket.close(), Ok(Handler::CloseSocket) => socket.close(),
Ok(Handler::Reset) => should_reset = true, Ok(Handler::Reset) => should_reset = true,
Err(_) => {}, Err(_) => {},