From 69686460b2eee877ad46a4864efc4ae24e23f306 Mon Sep 17 00:00:00 2001 From: thatscringebro Date: Fri, 27 Mar 2026 16:01:03 -0400 Subject: [PATCH] =?UTF-8?q?d=C3=A9but=20gestion=20des=20erreurs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app.rs | 20 ++++++++++++-- src/data_layer.rs | 53 +++++++++++++++++++++---------------- src/entities/accountlist.rs | 9 ++++--- src/main.rs | 7 +++++ src/ui.rs | 18 +++++++++++++ 5 files changed, 79 insertions(+), 28 deletions(-) diff --git a/src/app.rs b/src/app.rs index 36091d7..2715b9a 100644 --- a/src/app.rs +++ b/src/app.rs @@ -16,6 +16,8 @@ pub struct App { pub tr_types: Vec, pub selected_tab: usize, pub tabs: Vec, + pub error: String, + pub error_showing: bool, pub connection: Connection, } @@ -50,12 +52,20 @@ impl App { "Chart 3".to_string(), ] .to_vec(), + error: String::new(), + error_showing: false, connection: con, }; } pub fn get_list_accounts(&mut self) -> Vec { - return self.acc_list.get_accounts(&self.connection); + return match self.acc_list.get_accounts(&self.connection) { + Ok(a) => a, + Err(e) => { + self.error = e.to_string(); + return Vec::new(); + } + }; } pub fn next_ac(&mut self) { @@ -82,7 +92,13 @@ impl App { pub fn save_new_account(&mut self) { let new = self.new_account.get_id() == 0; self.next_ac_input(); - let ac = data_layer::upsert_account(&self.connection, self.new_account.clone()); + let ac = match data_layer::upsert_account(&self.connection, self.new_account.clone()) { + Ok(a) => a, + Err(e) => { + self.error = e.to_string(); + return; + } + }; if new { self.acc_list.add_account(ac); } diff --git a/src/data_layer.rs b/src/data_layer.rs index c23d0a6..69c4314 100644 --- a/src/data_layer.rs +++ b/src/data_layer.rs @@ -41,7 +41,10 @@ pub fn setup(con: &Connection) { } } -pub fn upsert_account(con: &Connection, ac: Account) -> Account { +pub fn upsert_account( + con: &Connection, + ac: Account, +) -> Result> { let query; let ac_type = ac.get_ac_type() as i64; if ac.get_id() == 0 { @@ -50,30 +53,33 @@ pub fn upsert_account(con: &Connection, ac: Account) -> Account { query = "UPDATE Accounts SET name = :name, type = :type, asset_price = :asset_price WHERE id = :id RETURNING id;"; } - let mut statement = con.prepare(query).unwrap(); - statement.bind((":name", &ac.get_name() as &str)).unwrap(); - statement.bind((":type", ac_type)).unwrap(); - statement - .bind((":asset_price", ac.get_asset_price())) - .unwrap(); + let mut statement = con + .prepare(query) + .map_err(|e| format!("Could not prepare query: {}", e))?; + statement.bind((":name", &ac.get_name() as &str))?; + statement.bind((":type", ac_type))?; + statement.bind((":asset_price", ac.get_asset_price()))?; if ac.get_id() != 0 { - statement.bind((":id", ac.get_id())).unwrap(); + statement.bind((":id", ac.get_id()))?; } let id; if let Ok(State::Row) = statement.next() { - id = statement.read::("id").unwrap(); + id = statement.read::("id")?; } else { id = 0; } - return Account::new( + let ac_type_enum = + AccountType::try_from(ac_type).map_err(|_| "Could not parse account type")?; + + return Ok(Account::new( id, ac.get_name(), ac.get_asset_price(), - ac_type.try_into().unwrap(), - ); + ac_type_enum, + )); } // pub fn get_account(con: &Connection, id: i64) -> Account { @@ -126,27 +132,28 @@ JOIN Accounts a ON t.account_id = a.id" } } -pub fn get_accounts(con: &Connection) -> Vec { +pub fn get_accounts(con: &Connection) -> Result, Box> { let query = "SELECT * FROM Accounts"; - let mut statement = con.prepare(query).unwrap(); + let mut statement = con + .prepare(query) + .map_err(|e| format!("Could not prepare query: {}", e))?; let mut vec = Vec::::new(); vec.push(Account::new(0, "All".to_string(), 0.0, AccountType::Cash)); while let Ok(State::Row) = statement.next() { + let type_i = statement.read::("type")?; + let ac_type_enum = + AccountType::try_from(type_i).map_err(|_| "Could not parse account type")?; vec.push(Account::new( - statement.read::("id").unwrap(), - statement.read::("name").unwrap(), - statement.read::("asset_price").unwrap(), - statement - .read::("type") - .unwrap() - .try_into() - .unwrap(), + statement.read::("id")?, + statement.read::("name")?, + statement.read::("asset_price")?, + ac_type_enum, )); } - return vec; + return Ok(vec); } // pub fn upsert_transaction_type(con: &Connection, tt: TransactionType) -> TransactionType { diff --git a/src/entities/accountlist.rs b/src/entities/accountlist.rs index cc3ebb5..9f2d4ee 100644 --- a/src/entities/accountlist.rs +++ b/src/entities/accountlist.rs @@ -17,11 +17,14 @@ impl AccountList { }; } - pub fn get_accounts(&mut self, con: &Connection) -> Vec { + pub fn get_accounts( + &mut self, + con: &Connection, + ) -> Result, Box> { if self.accounts.iter().count() == 0 { - self.accounts = data_layer::get_accounts(con); + self.accounts = data_layer::get_accounts(con)?; } - return self.accounts.clone(); + return Ok(self.accounts.clone()); } pub fn get_selected_id(&self) -> i64 { diff --git a/src/main.rs b/src/main.rs index edf41e3..398f57b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -63,6 +63,13 @@ where continue; } + if let KeyCode::Enter = key.code + && app.error_showing + { + app.error = String::new(); + app.error_showing = false; + } + match app.current_screen { CurrentScreen::Main => match key.code { KeyCode::Char('q') => { diff --git a/src/ui.rs b/src/ui.rs index da1c8f0..42ea8b6 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -299,6 +299,24 @@ pub fn ui(frame: &mut Frame, app: &mut App) { frame.render_widget(Clear, area); frame.render_widget(exit_paragraph, area); } + + if !app.error.is_empty() { + app.error_showing = true; + let popup = Block::default() + .title("Error") + .borders(Borders::all()) + .style(Style::default()); + + let error_text = Text::styled(&app.error, Style::default().fg(Color::Red)); + + let error_paragraph = Paragraph::new(error_text) + .block(popup) + .wrap(Wrap { trim: false }); + + let area = centered_rect(50, 20, frame.area()); + frame.render_widget(Clear, area); + frame.render_widget(error_paragraph, area); + } } fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect {