provide better error messages (#300)

This commit is contained in:
Conrad Ludgate 2022-04-13 18:08:49 +01:00 committed by GitHub
parent bc45bab273
commit 3c5fbc5734
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 30 additions and 16 deletions

9
Cargo.lock generated
View file

@ -70,6 +70,7 @@ dependencies = [
"crossbeam-channel", "crossbeam-channel",
"directories", "directories",
"eyre", "eyre",
"fs-err",
"humantime 2.1.0", "humantime 2.1.0",
"indicatif", "indicatif",
"itertools", "itertools",
@ -97,6 +98,7 @@ dependencies = [
"config", "config",
"directories", "directories",
"eyre", "eyre",
"fs-err",
"itertools", "itertools",
"log", "log",
"minspan", "minspan",
@ -144,6 +146,7 @@ dependencies = [
"chrono", "chrono",
"config", "config",
"eyre", "eyre",
"fs-err",
"http", "http",
"log", "log",
"rand 0.8.5", "rand 0.8.5",
@ -676,6 +679,12 @@ dependencies = [
"percent-encoding", "percent-encoding",
] ]
[[package]]
name = "fs-err"
version = "2.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5bd79fa345a495d3ae89fb7165fec01c0e72f41821d642dda363a1e97975652e"
[[package]] [[package]]
name = "fuchsia-cprng" name = "fuchsia-cprng"
version = "0.1.1" version = "0.1.1"

View file

@ -57,6 +57,7 @@ tabwriter = "1.2.1"
crossbeam-channel = "0.5.1" crossbeam-channel = "0.5.1"
clap = { version = "3.1.8", features = ["derive"] } clap = { version = "3.1.8", features = ["derive"] }
clap_complete = "3.1.1" clap_complete = "3.1.1"
fs-err = "2.7"
[profile.release] [profile.release]
lto = "fat" lto = "fat"

View file

@ -48,3 +48,4 @@ sqlx = { version = "0.5", features = [
] } ] }
minspan = "0.1.1" minspan = "0.1.1"
regex = "1.5.4" regex = "1.5.4"
fs-err = "2.7"

View file

@ -9,6 +9,7 @@ use eyre::Result;
use itertools::Itertools; use itertools::Itertools;
use regex::Regex; use regex::Regex;
use fs_err as fs;
use sqlx::sqlite::{ use sqlx::sqlite::{
SqliteConnectOptions, SqliteJournalMode, SqlitePool, SqlitePoolOptions, SqliteRow, SqliteConnectOptions, SqliteJournalMode, SqlitePool, SqlitePoolOptions, SqliteRow,
}; };
@ -62,7 +63,7 @@ impl Sqlite {
let create = !path.exists(); let create = !path.exists();
if create { if create {
if let Some(dir) = path.parent() { if let Some(dir) = path.parent() {
std::fs::create_dir_all(dir)?; fs::create_dir_all(dir)?;
} }
} }

View file

@ -8,11 +8,11 @@
// clients must share the secret in order to be able to sync, as it is needed // clients must share the secret in order to be able to sync, as it is needed
// to decrypt // to decrypt
use std::fs::File; use fs_err as fs;
use std::io::prelude::*; use std::io::prelude::*;
use std::path::PathBuf; use std::path::PathBuf;
use eyre::{eyre, Result}; use eyre::{eyre, Context, Result};
use sodiumoxide::crypto::secretbox; use sodiumoxide::crypto::secretbox;
use crate::history::History; use crate::history::History;
@ -30,7 +30,7 @@ pub fn new_key(settings: &Settings) -> Result<secretbox::Key> {
let key = secretbox::gen_key(); let key = secretbox::gen_key();
let encoded = encode_key(key.clone())?; let encoded = encode_key(key.clone())?;
let mut file = File::create(path)?; let mut file = fs::File::create(path)?;
file.write_all(encoded.as_bytes())?; file.write_all(encoded.as_bytes())?;
Ok(key) Ok(key)
@ -41,7 +41,7 @@ pub fn load_key(settings: &Settings) -> Result<secretbox::Key> {
let path = settings.key_path.as_str(); let path = settings.key_path.as_str();
let key = if PathBuf::from(path).exists() { let key = if PathBuf::from(path).exists() {
let key = std::fs::read_to_string(path)?; let key = fs_err::read_to_string(path)?;
decode_key(key)? decode_key(key)?
} else { } else {
new_key(settings)? new_key(settings)?
@ -54,13 +54,13 @@ pub fn load_encoded_key(settings: &Settings) -> Result<String> {
let path = settings.key_path.as_str(); let path = settings.key_path.as_str();
if PathBuf::from(path).exists() { if PathBuf::from(path).exists() {
let key = std::fs::read_to_string(path)?; let key = fs::read_to_string(path)?;
Ok(key) Ok(key)
} else { } else {
let key = secretbox::gen_key(); let key = secretbox::gen_key();
let encoded = encode_key(key)?; let encoded = encode_key(key)?;
let mut file = File::create(path)?; let mut file = fs::File::create(path)?;
file.write_all(encoded.as_bytes())?; file.write_all(encoded.as_bytes())?;
Ok(encoded) Ok(encoded)
@ -68,15 +68,16 @@ pub fn load_encoded_key(settings: &Settings) -> Result<String> {
} }
pub fn encode_key(key: secretbox::Key) -> Result<String> { pub fn encode_key(key: secretbox::Key) -> Result<String> {
let buf = rmp_serde::to_vec(&key)?; let buf = rmp_serde::to_vec(&key).wrap_err("could not encode key to message pack")?;
let buf = base64::encode(buf); let buf = base64::encode(buf);
Ok(buf) Ok(buf)
} }
pub fn decode_key(key: String) -> Result<secretbox::Key> { pub fn decode_key(key: String) -> Result<secretbox::Key> {
let buf = base64::decode(key)?; let buf = base64::decode(key).wrap_err("encryption key is not a valid base64 encoding")?;
let buf: secretbox::Key = rmp_serde::from_read_ref(&buf)?; let buf: secretbox::Key = rmp_serde::from_read_ref(&buf)
.wrap_err("encryption key is not a valid message pack encoding")?;
Ok(buf) Ok(buf)
} }

View file

@ -1,4 +1,4 @@
use std::fs::{create_dir_all, File}; use fs_err::{create_dir_all, File};
use std::io::prelude::*; use std::io::prelude::*;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
@ -76,7 +76,7 @@ impl Settings {
let sync_time_path = data_dir.join("last_sync_time"); let sync_time_path = data_dir.join("last_sync_time");
std::fs::write(sync_time_path, Utc::now().to_rfc3339())?; fs_err::write(sync_time_path, Utc::now().to_rfc3339())?;
Ok(()) Ok(())
} }
@ -91,7 +91,7 @@ impl Settings {
return Ok(Utc.ymd(1970, 1, 1).and_hms(0, 0, 0)); return Ok(Utc.ymd(1970, 1, 1).and_hms(0, 0, 0));
} }
let time = std::fs::read_to_string(sync_time_path)?; let time = fs_err::read_to_string(sync_time_path)?;
let time = chrono::DateTime::parse_from_rfc3339(time.as_str())?; let time = chrono::DateTime::parse_from_rfc3339(time.as_str())?;
Ok(time.with_timezone(&Utc)) Ok(time.with_timezone(&Utc))
@ -188,7 +188,7 @@ impl Settings {
// Finally, set the auth token // Finally, set the auth token
if Path::new(session_path.to_string().as_str()).exists() { if Path::new(session_path.to_string().as_str()).exists() {
let token = std::fs::read_to_string(session_path.to_string())?; let token = fs_err::read_to_string(session_path.to_string())?;
settings.session_token = token.trim().to_string(); settings.session_token = token.trim().to_string();
} else { } else {
settings.session_token = String::from("not logged in"); settings.session_token = String::from("not logged in");

View file

@ -29,3 +29,4 @@ sqlx = { version = "0.5", features = [ "runtime-tokio-rustls", "uuid", "chrono",
async-trait = "0.1.49" async-trait = "0.1.49"
axum = "0.5" axum = "0.5"
http = "0.2" http = "0.2"
fs-err = "2.7"

View file

@ -1,4 +1,4 @@
use std::fs::{create_dir_all, File}; use fs_err::{create_dir_all, File};
use std::io::prelude::*; use std::io::prelude::*;
use std::path::PathBuf; use std::path::PathBuf;

View file

@ -1,4 +1,4 @@
use std::fs::remove_file; use fs_err::remove_file;
pub fn run() { pub fn run() {
let session_path = atuin_common::utils::data_dir().join("session"); let session_path = atuin_common::utils::data_dir().join("session");