Skip to content

Commit

Permalink
Improve error handling for database path retrieval across Tauri compo…
Browse files Browse the repository at this point in the history
…nents

- Refactored `get_database_path` function to return a `Result<String, io::Error>` for better error management.
- Updated calls to `get_database_path` in `get_chat_history`, `get_chat_history_by_session`, `get_chatgpt_response`, `get_session_id_list`, and `lib.rs` to handle potential errors gracefully.
- Removed direct panics from database path retrieval and directory creation by incorporating proper error handling.
- Ensured that the application can gracefully handle scenarios when the database path cannot be determined, improving overall robustness.

This update enhances the reliability of database-related operations within the application.
  • Loading branch information
takanotume24 committed Dec 19, 2024
1 parent 4461b17 commit 41e7e21
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src-tauri/src/get_chat_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use tauri::async_runtime::block_on;
pub fn get_chat_history() -> Result<Vec<RawDatabaseChatEntry>, String> {
// Use block_on to run the async code synchronously
block_on(async {
let database_path = get_database_path();
let database_path = get_database_path().map_err(|e| e.to_string())?;
let mut conn = establish_connection(&database_path);

// Pass the mutable reference to Diesel operations
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/get_chat_history_by_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn get_chat_history_by_session(
// Use block_on to run the async code synchronously
block_on(async {
// Use the helper function to get a mutable connection
let database_path = get_database_path();
let database_path = get_database_path().map_err(|e| e.to_string())?;
let mut conn = establish_connection(&database_path);

// Filter the chat history by the specific session_id
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/get_chatgpt_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub async fn get_chatgpt_response(
model: String,
api_key: String,
) -> Result<ChatResponse, String> {
let database_path = get_database_path();
let database_path = get_database_path().map_err(|e| e.to_string())?;
let mut conn = establish_connection(&database_path);

let session_history = chat_histories
Expand Down
14 changes: 10 additions & 4 deletions src-tauri/src/get_database_path.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
use dirs;
use std::fs;
use std::io;

pub fn get_database_path() -> String {
let mut db_path = dirs::home_dir().expect("Failed to get home directory");
pub fn get_database_path() -> Result<String, io::Error> {
// ホームディレクトリの取得
let mut db_path = dirs::home_dir()
.ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, "Failed to get home directory"))?;
db_path.push(".cuuri/chat.db");

// 親ディレクトリの作成
if let Some(parent) = db_path.parent() {
fs::create_dir_all(parent).expect("Failed to create directories for database file");
fs::create_dir_all(parent)?;
}

// データベースURLの生成
let database_url = format!("sqlite://{}", db_path.to_string_lossy());

return database_url;
Ok(database_url) // 成功時はOkを返す
}
2 changes: 1 addition & 1 deletion src-tauri/src/get_session_id_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use tauri::async_runtime::block_on;
#[tauri::command]
pub fn get_session_id_list() -> Result<Vec<SessionId>, String> {
block_on(async {
let database_path = get_database_path();
let database_path = get_database_path().map_err(|e| e.to_string())?;
let mut conn = establish_connection(&database_path);

let results = chat_histories
Expand Down
4 changes: 2 additions & 2 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ use std::env;
pub fn run() {
init_config_file();

let database_url = get_database_path();
let mut connection: diesel::SqliteConnection = establish_connection(&database_url);
let database_path: String = get_database_path().unwrap();
let mut connection: diesel::SqliteConnection = establish_connection(&database_path);
run_migrations(&mut connection);

tauri::Builder::default()
Expand Down

0 comments on commit 41e7e21

Please sign in to comment.