From 191146da4b579430e3f855a4b0d28dfc91fbc25e Mon Sep 17 00:00:00 2001 From: Augusto Gunsch Date: Sun, 9 Jan 2022 08:43:18 -0300 Subject: [PATCH] Make fancy permission denied error --- src/database.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/database.rs b/src/database.rs index 5afce7f..dfcae30 100644 --- a/src/database.rs +++ b/src/database.rs @@ -1,8 +1,10 @@ use std::fs; use std::io::ErrorKind; +use std::process::exit; use reqwest; -use rusqlite::{Connection, Transaction}; +use rusqlite::{Connection, Transaction, ErrorCode}; +use rusqlite::Error::SqliteFailure; use rusqlite::params; use serde_json::Value; use serde_json::json; @@ -36,7 +38,20 @@ impl WordDb { let mut conn = self.connect(); 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!("