Make fancy permission denied error

This commit is contained in:
Augusto Gunsch 2022-01-09 08:43:18 -03:00
parent ce485b733e
commit 191146da4b
No known key found for this signature in database
GPG Key ID: F7EEFE29825C72DC
1 changed files with 17 additions and 2 deletions

View File

@ -1,8 +1,10 @@
use std::fs; use std::fs;
use std::io::ErrorKind; use std::io::ErrorKind;
use std::process::exit;
use reqwest; use reqwest;
use rusqlite::{Connection, Transaction}; use rusqlite::{Connection, Transaction, ErrorCode};
use rusqlite::Error::SqliteFailure;
use rusqlite::params; use rusqlite::params;
use serde_json::Value; use serde_json::Value;
use serde_json::json; use serde_json::json;
@ -36,7 +38,20 @@ impl WordDb {
let mut conn = self.connect(); let mut conn = self.connect();
let transaction = conn.transaction().unwrap(); let transaction = conn.transaction().unwrap();
transaction.execute(&format!("DROP TABLE IF EXISTS {0}_words", &lang.code), []).unwrap(); if let Err(e) = transaction.execute(&format!("DROP TABLE IF EXISTS {0}_words", &lang.code), []) {
match e {
SqliteFailure(f, _) => match f.code {
ErrorCode::ReadOnly => {
eprintln!("Could not write to database: Permission denied");
eprintln!("Please run as root");
exit(1);
},
_ => panic!("{}", e)
},
_ => panic!("{}", e)
}
}
transaction.execute(&format!("DROP TABLE IF EXISTS {0}_types", &lang.code), []).unwrap(); transaction.execute(&format!("DROP TABLE IF EXISTS {0}_types", &lang.code), []).unwrap();
transaction.execute(&format!(" transaction.execute(&format!("