Add better error messages

This commit is contained in:
Sofia 2020-08-24 02:17:50 +03:00
parent b765cb3279
commit 89460011eb
4 changed files with 47 additions and 14 deletions

View File

@ -7,8 +7,6 @@ use serde::Deserialize;
use std::sync::mpsc;
use std::sync::mpsc::Receiver;
use std::thread;
use std::thread::sleep;
use std::thread::Thread;
use std::time::{Duration, Instant};
pub trait Timestamped {

View File

@ -17,7 +17,7 @@ pub struct Config {
impl Default for Config {
fn default() -> Config {
Config {
api_key: "E3GrOiQAnY61BP623XXzt9Fo87A1IQrS1FFzD57P".to_owned(),
api_key: "<your API key here from https://developers.yepzon.com/>".to_owned(),
tags_url: "https://platform.yepzon.com/tags".to_owned(),
states_url: "https://platform.yepzon.com/tags/{tag}/states".to_owned(),
@ -26,7 +26,7 @@ impl Default for Config {
timestamp_format: "%Y-%m-%dT%H:%M:%S%.fZ".to_owned(),
between_format: "%d.%m.%Y %H:%M:%S".to_owned(),
throttle: 10,
throttle: 9,
}
}
}

View File

@ -1,4 +1,5 @@
use super::api::ErrorModel;
use std::fmt::{Display, Formatter};
use std::io;
#[derive(Debug)]
@ -9,7 +10,7 @@ pub enum GenericError {
ChronoParseError(chrono::ParseError),
IOError(io::Error),
FromUTF8Error(std::string::FromUtf8Error),
OutOfLocations,
MessagedError(String, Box<GenericError>),
}
impl From<toml::de::Error> for GenericError {
@ -47,3 +48,31 @@ impl From<std::string::FromUtf8Error> for GenericError {
GenericError::FromUTF8Error(error)
}
}
impl Display for GenericError {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
let err = match self {
GenericError::ChronoParseError(_) => "Chrono parse error".to_owned(),
GenericError::FromUTF8Error(_) => "UTF8 error".to_owned(),
GenericError::IOError(e) => format!("IO error: {}", e),
GenericError::MinreqError(_) => "Minreq error".to_owned(),
GenericError::TomlError(_) => "Toml error".to_owned(),
GenericError::YepzonServerError(_) => "Yepzon server error".to_owned(),
GenericError::MessagedError(msg, err) => format!("{}\n {}", msg, err),
};
write!(f, "{}", err)
}
}
pub trait MessagedError<A> {
fn with_msg<T: Into<String>>(self, text: T) -> Result<A, GenericError>;
}
impl<A, B: Into<GenericError>> MessagedError<A> for Result<A, B> {
fn with_msg<T: Into<String>>(self, text: T) -> Result<A, GenericError> {
match self {
Ok(ok) => Ok(ok),
Err(e) => Err(GenericError::MessagedError(text.into(), Box::new(e.into()))),
}
}
}

View File

@ -7,7 +7,7 @@ use api::API;
use chrono::NaiveDateTime;
use cmd::*;
use config::Config;
use errors::GenericError;
use errors::{GenericError, MessagedError};
use std::fs::File;
use std::io::prelude::*;
use std::sync::mpsc::TryRecvError;
@ -15,19 +15,21 @@ use std::sync::mpsc::TryRecvError;
fn main() {
let env: EnvOpt = argh::from_env();
if let Err(e) = from_env(env) {
eprintln!("Error: {:?}", e);
eprintln!("Critical Error: {}", e);
std::process::exit(1);
}
}
fn from_env(env: EnvOpt) -> Result<(), GenericError> {
match env.subcommand {
Subcommand::Run(opt) => {
let mut file = File::open("config.toml")?;
Subcommand::Run(_) => {
let mut file = File::open("config.toml").with_msg("Could not find config.toml")?;
let mut string = String::new();
file.read_to_string(&mut string)?;
file.read_to_string(&mut string)
.with_msg("config.toml is not valid UTF-8")?;
let config: Config = toml::from_str(&string)?;
let config: Config =
toml::from_str(&string).with_msg("config.toml is not a valid config file")?;
run(
&config,
@ -36,11 +38,12 @@ fn from_env(env: EnvOpt) -> Result<(), GenericError> {
)?;
Ok(())
}
Subcommand::Init(opt) => {
Subcommand::Init(_) => {
let config = Config::default();
let mut file = File::create("config.toml").unwrap();
file.write_all(&toml::to_vec(&config).unwrap())?;
file.write_all(&toml::to_vec(&config).unwrap())
.with_msg("Could not write config.toml, make sure you have correct permissions.")?;
Ok(())
}
}
@ -75,7 +78,10 @@ fn run(config: &Config, from: Option<String>, to: Option<String>) -> Result<(),
);
match loc {
Ok(mut loc) => locations.append(&mut loc),
Err(e) => println!("Error: {}", e.message.unwrap_or(String::new())),
Err(e) => println!(
"Error fetching location data: {}",
e.message.unwrap_or(String::new())
),
}
}
Err(e) => eprintln!("{:?}", e),