Skip to content

Commit 406263f

Browse files
committed
customize tag errors with action
1 parent ab8bff2 commit 406263f

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

src/env/node/git/localGitProvider.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,7 +1270,7 @@ export class LocalGitProvider implements GitProvider, Disposable {
12701270
await this.git.tag(repoPath, name, ref, ...(message?.length !== 0 ? ['-m', message] : []));
12711271
} catch (ex) {
12721272
if (ex instanceof TagError) {
1273-
throw ex.WithTag(name);
1273+
throw ex.WithTag(name).WithAction('create');
12741274
}
12751275

12761276
throw ex;
@@ -1283,7 +1283,7 @@ export class LocalGitProvider implements GitProvider, Disposable {
12831283
await this.git.tag(repoPath, '-d', name);
12841284
} catch (ex) {
12851285
if (ex instanceof TagError) {
1286-
throw ex.WithTag(name);
1286+
throw ex.WithTag(name).WithAction('delete');
12871287
}
12881288

12891289
throw ex;

src/git/errors.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -506,12 +506,17 @@ export class TagError extends Error {
506506

507507
readonly original?: Error;
508508
readonly reason: TagErrorReason | undefined;
509-
readonly tag?: string;
509+
action?: string;
510+
tag?: string;
511+
512+
private static buildTagErrorMessage(reason?: TagErrorReason, tag?: string, action?: string): string {
513+
let baseMessage: string;
514+
if (action != null) {
515+
baseMessage = `Unable to ${action} tag ${tag ? `'${tag}'` : ''}`;
516+
} else {
517+
baseMessage = `Unable to perform action${tag ? ` with tag '${tag}'` : 'on tag'}`;
518+
}
510519

511-
private static buildTagErrorMessage(reason?: TagErrorReason, tag?: string, remote?: string): string {
512-
const baseMessage = `Unable to perform action${
513-
tag ? ` with tag '${tag}'${remote ? ` on ${remote}` : ''}` : 'on tag'
514-
}`;
515520
switch (reason) {
516521
case TagErrorReason.TagAlreadyExists:
517522
return `${baseMessage} because it already exists`;
@@ -528,9 +533,9 @@ export class TagError extends Error {
528533
}
529534
}
530535

531-
constructor(reason?: TagErrorReason, original?: Error, tag?: string, remote?: string);
536+
constructor(reason?: TagErrorReason, original?: Error, tag?: string, action?: string);
532537
constructor(message?: string, original?: Error);
533-
constructor(messageOrReason: string | TagErrorReason | undefined, original?: Error, tag?: string, remote?: string) {
538+
constructor(messageOrReason: string | TagErrorReason | undefined, original?: Error, tag?: string, action?: string) {
534539
let reason: TagErrorReason | undefined;
535540
if (typeof messageOrReason !== 'string') {
536541
reason = messageOrReason as TagErrorReason;
@@ -540,17 +545,25 @@ export class TagError extends Error {
540545
const message =
541546
typeof messageOrReason === 'string'
542547
? messageOrReason
543-
: TagError.buildTagErrorMessage(messageOrReason as TagErrorReason, tag, remote);
548+
: TagError.buildTagErrorMessage(messageOrReason as TagErrorReason, tag, action);
544549
super(message);
545550

546551
this.original = original;
547552
this.reason = reason;
548553
this.tag = tag;
554+
this.action = action;
549555
Error.captureStackTrace?.(this, TagError);
550556
}
551557

552558
WithTag(tag: string) {
553-
this.message = TagError.buildTagErrorMessage(this.reason, tag);
559+
this.tag = tag;
560+
this.message = TagError.buildTagErrorMessage(this.reason, tag, this.action);
561+
return this;
562+
}
563+
564+
WithAction(action: string) {
565+
this.action = action;
566+
this.message = TagError.buildTagErrorMessage(this.reason, this.tag, action);
554567
return this;
555568
}
556569
}

0 commit comments

Comments
 (0)