diff --git a/src/commands/git/stash.ts b/src/commands/git/stash.ts index c63c15e..3c6a67a 100644 --- a/src/commands/git/stash.ts +++ b/src/commands/git/stash.ts @@ -294,6 +294,7 @@ export class StashGitCommand extends QuickCommand { } QuickCommand.endSteps(state); + try { void (await state.repo.stashApply( // pop can only take a stash index, e.g. `stash@{1}` @@ -303,14 +304,12 @@ export class StashGitCommand extends QuickCommand { } catch (ex) { Logger.error(ex, context.title); - if (ex instanceof StashApplyError) { - if (ex.reason === StashApplyErrorReason.WorkingChanges) { - void window.showWarningMessage( - 'Unable to apply stash. Your working tree changes would be overwritten. Please commit or stash your changes before trying again', - ); - } else { - void Messages.showGenericErrorMessage(ex.message); - } + if (StashApplyError.is(ex, StashApplyErrorReason.WorkingChanges)) { + void window.showWarningMessage( + 'Unable to apply stash. Your working tree changes would be overwritten. Please commit or stash your changes before trying again', + ); + } else { + void Messages.showGenericErrorMessage(ex.message); } } } diff --git a/src/commands/git/worktree.ts b/src/commands/git/worktree.ts index 7877c6d..f82ec73 100644 --- a/src/commands/git/worktree.ts +++ b/src/commands/git/worktree.ts @@ -367,9 +367,8 @@ export class WorktreeGitCommand extends QuickCommand { }); } catch (ex) { if ( - !state.flags.includes('--force') && - ex instanceof WorktreeCreateError && - ex.reason === WorktreeCreateErrorReason.AlreadyCheckedOut + WorktreeCreateError.is(ex, WorktreeCreateErrorReason.AlreadyCheckedOut) && + !state.flags.includes('--force') ) { const createBranch: MessageItem = { title: 'Create New Branch' }; const force: MessageItem = { title: 'Create Anyway' }; @@ -398,11 +397,11 @@ export class WorktreeGitCommand extends QuickCommand { state.confirm = false; continue; } - } else if (ex instanceof WorktreeCreateError && ex.reason === WorktreeCreateErrorReason.AlreadyExists) { - void Messages.showGenericErrorMessage( + } else if (WorktreeCreateError.is(ex, WorktreeCreateErrorReason.AlreadyExists)) { + void window.showErrorMessage( `Unable to create a new worktree in '${GitWorktree.getFriendlyPath( uri, - )} because that folder already exists.`, + )} because that folder already exists and is not empty.`, ); } else { void Messages.showGenericErrorMessage( @@ -610,7 +609,7 @@ export class WorktreeGitCommand extends QuickCommand { await state.repo.deleteWorktree(uri, { force: force }); } catch (ex) { - if (ex instanceof WorktreeDeleteError) { + if (WorktreeDeleteError.is(ex)) { if (ex.reason === WorktreeDeleteErrorReason.MainWorkingTree) { void window.showErrorMessage('Unable to delete the main worktree'); } else if (!force) { diff --git a/src/git/errors.ts b/src/git/errors.ts index 85c955a..03f9398 100644 --- a/src/git/errors.ts +++ b/src/git/errors.ts @@ -3,6 +3,10 @@ export const enum StashApplyErrorReason { } export class StashApplyError extends Error { + static is(ex: any, reason?: StashApplyErrorReason): ex is StashApplyError { + return ex instanceof StashApplyError && (reason == null || ex.reason === reason); + } + readonly original?: Error; readonly reason: StashApplyErrorReason | undefined; @@ -35,6 +39,10 @@ export const enum WorktreeCreateErrorReason { } export class WorktreeCreateError extends Error { + static is(ex: any, reason?: WorktreeCreateErrorReason): ex is WorktreeCreateError { + return ex instanceof WorktreeCreateError && (reason == null || ex.reason === reason); + } + readonly original?: Error; readonly reason: WorktreeCreateErrorReason | undefined; @@ -73,6 +81,10 @@ export const enum WorktreeDeleteErrorReason { } export class WorktreeDeleteError extends Error { + static is(ex: any, reason?: WorktreeDeleteErrorReason): ex is WorktreeDeleteError { + return ex instanceof WorktreeDeleteError && (reason == null || ex.reason === reason); + } + readonly original?: Error; readonly reason: WorktreeDeleteErrorReason | undefined;