From e78eac0511bebe72e471346bb45d8b7436a52799 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Mon, 17 Nov 2025 19:15:03 +0800 Subject: Add error handling for sheet use command --- locales/help_docs/en.yml | 9 +++++++++ locales/help_docs/zh-CN.yml | 9 +++++++++ src/bin/jv.rs | 29 +++++++++++++++++++++++++---- 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/locales/help_docs/en.yml b/locales/help_docs/en.yml index 4997257..db143a1 100644 --- a/locales/help_docs/en.yml +++ b/locales/help_docs/en.yml @@ -533,6 +533,15 @@ jv: analyze: | Failed to analyze local workspace status! + use: + sheet_not_exists: | + Sheet `%{name}` does not exist! + **Tip**: Please use `jv update` to update workspace status + + directory_not_empty: | + When no sheet is in use, the workspace should not contain any files! + **Tip**: Please ensure the workspace is clean before using `jv use ` to select and use a sheet + success: account: as: Successfully switched this workspace's account to `%{account}` diff --git a/locales/help_docs/zh-CN.yml b/locales/help_docs/zh-CN.yml index c35b6aa..a1dd4fe 100644 --- a/locales/help_docs/zh-CN.yml +++ b/locales/help_docs/zh-CN.yml @@ -540,6 +540,15 @@ jv: analyze: | 分析本地工作区的状态失败! + use: + sheet_not_exists: | + 表 `%{name}` 不存在! + **提示**:请使用 `jv update` 更新工作区状态 + + directory_not_empty: | + 在没有使用表的时候,工作区不应该存在任何文件! + **提示**:请保证工作区是干净的,再使用 `jv use <表名>` 选择并使用一张表 + success: account: as: 成功将此工作区的账户切换至 `%{account}` diff --git a/src/bin/jv.rs b/src/bin/jv.rs index 7507680..51f73dd 100644 --- a/src/bin/jv.rs +++ b/src/bin/jv.rs @@ -1474,16 +1474,37 @@ async fn jv_sheet_use(args: SheetUseArgs) { return; }; - match local_cfg.use_sheet(args.sheet_name).await { + match local_cfg.use_sheet(args.sheet_name.clone()).await { Ok(_) => { let Ok(_) = LocalConfig::write(&local_cfg).await else { eprintln!("{}", t!("jv.fail.write_cfg").trim().red()); return; }; } - Err(e) => { - handle_err(e.into()); - } + Err(e) => match e.kind() { + std::io::ErrorKind::AlreadyExists => {} // Already In Use + std::io::ErrorKind::NotFound => { + eprintln!( + "{}", + md(t!("jv.fail.use.sheet_not_exists", name = args.sheet_name)).red() + ); + return; + } + std::io::ErrorKind::DirectoryNotEmpty => { + eprintln!( + "{}", + md(t!( + "jv.fail.use.directory_not_empty", + name = args.sheet_name + )) + .red() + ); + return; + } + _ => { + handle_err(e.into()); + } + }, } } -- cgit