Skip to content

Commit e083379

Browse files
committed
add message builder to BranchError
1 parent 02efcea commit e083379

File tree

1 file changed

+26
-25
lines changed

1 file changed

+26
-25
lines changed

src/git/errors.ts

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -237,39 +237,40 @@ export class BranchError extends Error {
237237
constructor(reason?: BranchErrorReason, original?: Error, branch?: string);
238238
constructor(message?: string, original?: Error);
239239
constructor(messageOrReason: string | BranchErrorReason | undefined, original?: Error, branch?: string) {
240-
let message;
241-
const baseMessage = `Unable to perform action on branch${branch ? ` '${branch}'` : ''}`;
242240
let reason: BranchErrorReason | undefined;
243-
if (messageOrReason == null) {
244-
message = baseMessage;
245-
} else if (typeof messageOrReason === 'string') {
246-
message = messageOrReason;
247-
reason = undefined;
248-
} else {
249-
reason = messageOrReason;
250-
switch (reason) {
251-
case BranchErrorReason.BranchAlreadyExists:
252-
message = `${baseMessage} because it already exists`;
253-
break;
254-
case BranchErrorReason.BranchNotFullyMerged:
255-
message = `${baseMessage} because it is not fully merged`;
256-
break;
257-
case BranchErrorReason.NoRemoteReference:
258-
message = `${baseMessage} because the remote reference does not exist`;
259-
break;
260-
case BranchErrorReason.InvalidBranchName:
261-
message = `${baseMessage} because the branch name is invalid`;
262-
break;
263-
default:
264-
message = baseMessage;
265-
}
241+
if (typeof messageOrReason !== 'string') {
242+
reason = messageOrReason as BranchErrorReason;
266243
}
244+
245+
const message =
246+
typeof messageOrReason === 'string' ? messageOrReason : BranchError.buildBranchErrorMessage(reason, branch);
267247
super(message);
268248

269249
this.original = original;
270250
this.reason = reason;
271251
Error.captureStackTrace?.(this, BranchError);
272252
}
253+
254+
private static buildBranchErrorMessage(reason?: BranchErrorReason, branch?: string): string {
255+
const baseMessage = `Unable to perform action on branch${branch ? ` '${branch}'` : ''}`;
256+
switch (reason) {
257+
case BranchErrorReason.BranchAlreadyExists:
258+
return `${baseMessage} because it already exists`;
259+
case BranchErrorReason.BranchNotFullyMerged:
260+
return `${baseMessage} because it is not fully merged`;
261+
case BranchErrorReason.NoRemoteReference:
262+
return `${baseMessage} because the remote reference does not exist`;
263+
case BranchErrorReason.InvalidBranchName:
264+
return `${baseMessage} because the branch name is invalid`;
265+
default:
266+
return baseMessage;
267+
}
268+
}
269+
270+
WithBranch(branchName: string): this {
271+
this.message = BranchError.buildBranchErrorMessage(this.reason, branchName);
272+
return this;
273+
}
273274
}
274275

275276
export const enum PullErrorReason {

0 commit comments

Comments
 (0)