-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy patherrors.ts
581 lines (513 loc) · 17.6 KB
/
errors.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
export class GitSearchError extends Error {
constructor(public readonly original: Error) {
super(original.message);
Error.captureStackTrace?.(this, GitSearchError);
}
}
export const enum ApplyPatchCommitErrorReason {
StashFailed,
CreateWorktreeFailed,
ApplyFailed,
ApplyAbortedWouldOverwrite,
AppliedWithConflicts,
}
export class ApplyPatchCommitError extends Error {
static is(ex: unknown, reason?: ApplyPatchCommitErrorReason): ex is ApplyPatchCommitError {
return ex instanceof ApplyPatchCommitError && (reason == null || ex.reason === reason);
}
readonly original?: Error;
readonly reason: ApplyPatchCommitErrorReason | undefined;
constructor(reason: ApplyPatchCommitErrorReason, message?: string, original?: Error) {
message ||= 'Unable to apply patch';
super(message);
this.original = original;
this.reason = reason;
Error.captureStackTrace?.(this, ApplyPatchCommitError);
}
}
export class BlameIgnoreRevsFileError extends Error {
static is(ex: unknown): ex is BlameIgnoreRevsFileError {
return ex instanceof BlameIgnoreRevsFileError;
}
constructor(
public readonly fileName: string,
public readonly original?: Error,
) {
super(`Invalid blame.ignoreRevsFile: '${fileName}'`);
Error.captureStackTrace?.(this, BlameIgnoreRevsFileError);
}
}
export class BlameIgnoreRevsFileBadRevisionError extends Error {
static is(ex: unknown): ex is BlameIgnoreRevsFileBadRevisionError {
return ex instanceof BlameIgnoreRevsFileBadRevisionError;
}
constructor(
public readonly revision: string,
public readonly original?: Error,
) {
super(`Invalid revision in blame.ignoreRevsFile: '${revision}'`);
Error.captureStackTrace?.(this, BlameIgnoreRevsFileBadRevisionError);
}
}
export const enum StashApplyErrorReason {
WorkingChanges,
}
export class StashApplyError extends Error {
static is(ex: unknown, reason?: StashApplyErrorReason): ex is StashApplyError {
return ex instanceof StashApplyError && (reason == null || ex.reason === reason);
}
readonly original?: Error;
readonly reason: StashApplyErrorReason | undefined;
constructor(reason?: StashApplyErrorReason, original?: Error);
constructor(message?: string, original?: Error);
constructor(messageOrReason: string | StashApplyErrorReason | undefined, original?: Error) {
let message;
let reason: StashApplyErrorReason | undefined;
if (messageOrReason == null) {
message = 'Unable to apply stash';
} else if (typeof messageOrReason === 'string') {
message = messageOrReason;
reason = undefined;
} else {
reason = messageOrReason;
message =
'Unable to apply stash. Your working tree changes would be overwritten. Please commit or stash your changes before trying again';
}
super(message);
this.original = original;
this.reason = reason;
Error.captureStackTrace?.(this, StashApplyError);
}
}
export const enum StashPushErrorReason {
ConflictingStagedAndUnstagedLines,
NothingToSave,
}
export class StashPushError extends Error {
static is(ex: unknown, reason?: StashPushErrorReason): ex is StashPushError {
return ex instanceof StashPushError && (reason == null || ex.reason === reason);
}
readonly original?: Error;
readonly reason: StashPushErrorReason | undefined;
constructor(reason?: StashPushErrorReason, original?: Error);
constructor(message?: string, original?: Error);
constructor(messageOrReason: string | StashPushErrorReason | undefined, original?: Error) {
let message;
let reason: StashPushErrorReason | undefined;
if (messageOrReason == null) {
message = 'Unable to stash';
} else if (typeof messageOrReason === 'string') {
message = messageOrReason;
reason = undefined;
} else {
reason = messageOrReason;
switch (reason) {
case StashPushErrorReason.ConflictingStagedAndUnstagedLines:
message =
'Changes were stashed, but the working tree cannot be updated because at least one file has staged and unstaged changes on the same line(s)';
break;
case StashPushErrorReason.NothingToSave:
message = 'No files to stash';
break;
default:
message = 'Unable to stash';
}
}
super(message);
this.original = original;
this.reason = reason;
Error.captureStackTrace?.(this, StashApplyError);
}
}
export const enum PushErrorReason {
RemoteAhead,
TipBehind,
PushRejected,
PushRejectedWithLease,
PushRejectedWithLeaseIfIncludes,
PermissionDenied,
RemoteConnection,
NoUpstream,
Other,
}
export class PushError extends Error {
static is(ex: unknown, reason?: PushErrorReason): ex is PushError {
return ex instanceof PushError && (reason == null || ex.reason === reason);
}
readonly original?: Error;
readonly reason: PushErrorReason | undefined;
constructor(reason?: PushErrorReason, original?: Error, branch?: string, remote?: string);
constructor(message?: string, original?: Error);
constructor(
messageOrReason: string | PushErrorReason | undefined,
original?: Error,
branch?: string,
remote?: string,
) {
let message;
const baseMessage = `Unable to push${branch ? ` branch '${branch}'` : ''}${remote ? ` to ${remote}` : ''}`;
let reason: PushErrorReason | undefined;
if (messageOrReason == null) {
message = baseMessage;
} else if (typeof messageOrReason === 'string') {
message = messageOrReason;
reason = undefined;
} else {
reason = messageOrReason;
switch (reason) {
case PushErrorReason.RemoteAhead:
message = `${baseMessage} because the remote contains work that you do not have locally. Try fetching first.`;
break;
case PushErrorReason.TipBehind:
message = `${baseMessage} as it is behind its remote counterpart. Try pulling first.`;
break;
case PushErrorReason.PushRejected:
message = `${baseMessage} because some refs failed to push or the push was rejected. Try pulling first.`;
break;
case PushErrorReason.PushRejectedWithLease:
case PushErrorReason.PushRejectedWithLeaseIfIncludes:
message = `Unable to force push${branch ? ` branch '${branch}'` : ''}${
remote ? ` to ${remote}` : ''
} because some refs failed to push or the push was rejected. The tip of the remote-tracking branch has been updated since the last checkout. Try pulling first.`;
break;
case PushErrorReason.PermissionDenied:
message = `${baseMessage} because you don't have permission to push to this remote repository.`;
break;
case PushErrorReason.RemoteConnection:
message = `${baseMessage} because the remote repository could not be reached.`;
break;
case PushErrorReason.NoUpstream:
message = `${baseMessage} because it has no upstream branch.`;
break;
default:
message = baseMessage;
}
}
super(message);
this.original = original;
this.reason = reason;
Error.captureStackTrace?.(this, PushError);
}
}
export const enum PullErrorReason {
Conflict,
GitIdentity,
RemoteConnection,
UnstagedChanges,
UnmergedFiles,
UncommittedChanges,
OverwrittenChanges,
RefLocked,
RebaseMultipleBranches,
TagConflict,
Other,
}
export class PullError extends Error {
static is(ex: unknown, reason?: PullErrorReason): ex is PullError {
return ex instanceof PullError && (reason == null || ex.reason === reason);
}
readonly original?: Error;
readonly reason: PullErrorReason | undefined;
constructor(reason?: PullErrorReason, original?: Error, branch?: string, remote?: string);
constructor(message?: string, original?: Error);
constructor(messageOrReason: string | PullErrorReason | undefined, original?: Error) {
let message;
let reason: PullErrorReason | undefined;
const baseMessage = `Unable to pull`;
if (messageOrReason == null) {
message = baseMessage;
} else if (typeof messageOrReason === 'string') {
message = messageOrReason;
reason = undefined;
} else {
reason = messageOrReason;
switch (reason) {
case PullErrorReason.Conflict:
message = `${baseMessage} due to conflicts.`;
break;
case PullErrorReason.GitIdentity:
message = `${baseMessage} because you have not yet set up your Git identity.`;
break;
case PullErrorReason.RemoteConnection:
message = `${baseMessage} because the remote repository could not be reached.`;
break;
case PullErrorReason.UnstagedChanges:
message = `${baseMessage} because you have unstaged changes.`;
break;
case PullErrorReason.UnmergedFiles:
message = `${baseMessage} because you have unmerged files.`;
break;
case PullErrorReason.UncommittedChanges:
message = `${baseMessage} because you have uncommitted changes.`;
break;
case PullErrorReason.OverwrittenChanges:
message = `${baseMessage} because local changes to some files would be overwritten.`;
break;
case PullErrorReason.RefLocked:
message = `${baseMessage} because a local ref could not be updated.`;
break;
case PullErrorReason.RebaseMultipleBranches:
message = `${baseMessage} because you are trying to rebase onto multiple branches.`;
break;
case PullErrorReason.TagConflict:
message = `${baseMessage} because a local tag would be overwritten.`;
break;
default:
message = baseMessage;
}
}
super(message);
this.original = original;
this.reason = reason;
Error.captureStackTrace?.(this, PullError);
}
}
export const enum FetchErrorReason {
NoFastForward,
NoRemote,
RemoteConnection,
Other,
}
export class FetchError extends Error {
static is(ex: unknown, reason?: FetchErrorReason): ex is FetchError {
return ex instanceof FetchError && (reason == null || ex.reason === reason);
}
readonly original?: Error;
readonly reason: FetchErrorReason | undefined;
constructor(reason?: FetchErrorReason, original?: Error, branch?: string, remote?: string);
constructor(message?: string, original?: Error);
constructor(
messageOrReason: string | FetchErrorReason | undefined,
original?: Error,
branch?: string,
remote?: string,
) {
let message;
const baseMessage = `Unable to fetch${branch ? ` branch '${branch}'` : ''}${remote ? ` from ${remote}` : ''}`;
let reason: FetchErrorReason | undefined;
if (messageOrReason == null) {
message = baseMessage;
} else if (typeof messageOrReason === 'string') {
message = messageOrReason;
reason = undefined;
} else {
reason = messageOrReason;
switch (reason) {
case FetchErrorReason.NoFastForward:
message = `${baseMessage} as it cannot be fast-forwarded`;
break;
case FetchErrorReason.NoRemote:
message = `${baseMessage} without a remote repository specified.`;
break;
case FetchErrorReason.RemoteConnection:
message = `${baseMessage}. Could not connect to the remote repository.`;
break;
default:
message = baseMessage;
}
}
super(message);
this.original = original;
this.reason = reason;
Error.captureStackTrace?.(this, FetchError);
}
}
export const enum CherryPickErrorReason {
Conflicts,
AbortedWouldOverwrite,
Other,
}
export class CherryPickError extends Error {
static is(ex: unknown, reason?: CherryPickErrorReason): ex is CherryPickError {
return ex instanceof CherryPickError && (reason == null || ex.reason === reason);
}
readonly original?: Error;
readonly reason: CherryPickErrorReason | undefined;
ref?: string;
private static buildCherryPickErrorMessage(reason: CherryPickErrorReason | undefined, ref?: string) {
let baseMessage = `Unable to cherry-pick${ref ? ` commit '${ref}'` : ''}`;
switch (reason) {
case CherryPickErrorReason.AbortedWouldOverwrite:
baseMessage += ' as some local changes would be overwritten';
break;
case CherryPickErrorReason.Conflicts:
baseMessage += ' due to conflicts';
break;
}
return baseMessage;
}
constructor(reason?: CherryPickErrorReason, original?: Error, sha?: string);
constructor(message?: string, original?: Error);
constructor(messageOrReason: string | CherryPickErrorReason | undefined, original?: Error, ref?: string) {
let reason: CherryPickErrorReason | undefined;
if (typeof messageOrReason !== 'string') {
reason = messageOrReason as CherryPickErrorReason;
} else {
super(messageOrReason);
}
const message =
typeof messageOrReason === 'string'
? messageOrReason
: CherryPickError.buildCherryPickErrorMessage(messageOrReason as CherryPickErrorReason, ref);
super(message);
this.original = original;
this.reason = reason;
this.ref = ref;
Error.captureStackTrace?.(this, CherryPickError);
}
WithRef(ref: string) {
this.ref = ref;
this.message = CherryPickError.buildCherryPickErrorMessage(this.reason, ref);
return this;
}
}
export class WorkspaceUntrustedError extends Error {
constructor() {
super('Unable to perform Git operations because the current workspace is untrusted');
Error.captureStackTrace?.(this, WorkspaceUntrustedError);
}
}
export const enum WorktreeCreateErrorReason {
AlreadyCheckedOut,
AlreadyExists,
}
export class WorktreeCreateError extends Error {
static is(ex: unknown, reason?: WorktreeCreateErrorReason): ex is WorktreeCreateError {
return ex instanceof WorktreeCreateError && (reason == null || ex.reason === reason);
}
readonly original?: Error;
readonly reason: WorktreeCreateErrorReason | undefined;
constructor(reason?: WorktreeCreateErrorReason, original?: Error);
constructor(message?: string, original?: Error);
constructor(messageOrReason: string | WorktreeCreateErrorReason | undefined, original?: Error) {
let message;
let reason: WorktreeCreateErrorReason | undefined;
if (messageOrReason == null) {
message = 'Unable to create worktree';
} else if (typeof messageOrReason === 'string') {
message = messageOrReason;
reason = undefined;
} else {
reason = messageOrReason;
switch (reason) {
case WorktreeCreateErrorReason.AlreadyCheckedOut:
message = 'Unable to create worktree because it is already checked out';
break;
case WorktreeCreateErrorReason.AlreadyExists:
message = 'Unable to create worktree because it already exists';
break;
}
}
super(message);
this.original = original;
this.reason = reason;
Error.captureStackTrace?.(this, WorktreeCreateError);
}
}
export const enum WorktreeDeleteErrorReason {
HasChanges,
DefaultWorkingTree,
DirectoryNotEmpty,
}
export class WorktreeDeleteError extends Error {
static is(ex: unknown, reason?: WorktreeDeleteErrorReason): ex is WorktreeDeleteError {
return ex instanceof WorktreeDeleteError && (reason == null || ex.reason === reason);
}
readonly original?: Error;
readonly reason: WorktreeDeleteErrorReason | undefined;
constructor(reason?: WorktreeDeleteErrorReason, original?: Error);
constructor(message?: string, original?: Error);
constructor(messageOrReason: string | WorktreeDeleteErrorReason | undefined, original?: Error) {
let message;
let reason: WorktreeDeleteErrorReason | undefined;
if (messageOrReason == null) {
message = 'Unable to delete worktree';
} else if (typeof messageOrReason === 'string') {
message = messageOrReason;
reason = undefined;
} else {
reason = messageOrReason;
switch (reason) {
case WorktreeDeleteErrorReason.HasChanges:
message = 'Unable to delete worktree because there are uncommitted changes';
break;
case WorktreeDeleteErrorReason.DefaultWorkingTree:
message = 'Cannot delete worktree because it is the default working tree';
break;
}
}
super(message);
this.original = original;
this.reason = reason;
Error.captureStackTrace?.(this, WorktreeDeleteError);
}
}
export const enum TagErrorReason {
TagAlreadyExists,
TagNotFound,
InvalidTagName,
PermissionDenied,
RemoteRejected,
Other,
}
export class TagError extends Error {
static is(ex: unknown, reason?: TagErrorReason): ex is TagError {
return ex instanceof TagError && (reason == null || ex.reason === reason);
}
readonly original?: Error;
readonly reason: TagErrorReason | undefined;
action?: string;
tag?: string;
private static buildTagErrorMessage(reason?: TagErrorReason, tag?: string, action?: string): string {
let baseMessage: string;
if (action != null) {
baseMessage = `Unable to ${action} tag ${tag ? `'${tag}'` : ''}`;
} else {
baseMessage = `Unable to perform action${tag ? ` with tag '${tag}'` : 'on tag'}`;
}
switch (reason) {
case TagErrorReason.TagAlreadyExists:
return `${baseMessage} because it already exists`;
case TagErrorReason.TagNotFound:
return `${baseMessage} because it does not exist`;
case TagErrorReason.InvalidTagName:
return `${baseMessage} because the tag name is invalid`;
case TagErrorReason.PermissionDenied:
return `${baseMessage} because you don't have permission to push to this remote repository.`;
case TagErrorReason.RemoteRejected:
return `${baseMessage} because the remote repository rejected the push.`;
default:
return baseMessage;
}
}
constructor(reason?: TagErrorReason, original?: Error, tag?: string, action?: string);
constructor(message?: string, original?: Error);
constructor(messageOrReason: string | TagErrorReason | undefined, original?: Error, tag?: string, action?: string) {
let reason: TagErrorReason | undefined;
if (typeof messageOrReason !== 'string') {
reason = messageOrReason as TagErrorReason;
} else {
super(messageOrReason);
}
const message =
typeof messageOrReason === 'string'
? messageOrReason
: TagError.buildTagErrorMessage(messageOrReason as TagErrorReason, tag, action);
super(message);
this.original = original;
this.reason = reason;
this.tag = tag;
this.action = action;
Error.captureStackTrace?.(this, TagError);
}
WithTag(tag: string) {
this.tag = tag;
this.message = TagError.buildTagErrorMessage(this.reason, tag, this.action);
return this;
}
WithAction(action: string) {
this.action = action;
this.message = TagError.buildTagErrorMessage(this.reason, this.tag, action);
return this;
}
}