Skip to content

Commit 46d29f5

Browse files
authored
Return without an error message when user cancels a push/pull (jupyterlab#1170)
* handle user cancellation error by returning an empty logger * add changes based on the review
1 parent 8de5292 commit 46d29f5

File tree

2 files changed

+65
-40
lines changed

2 files changed

+65
-40
lines changed

src/cancelledError.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export class CancelledError extends Error {
2+
constructor(...params: any) {
3+
super(...params);
4+
this.name = 'CancelledError';
5+
}
6+
}

src/commandsAndMenu.tsx

Lines changed: 59 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { CONTEXT_COMMANDS } from './components/FileList';
2727
import { MergeBranchDialog } from './components/MergeBranchDialog';
2828
import { AUTH_ERROR_MESSAGES, requestAPI } from './git';
2929
import { logger } from './logger';
30+
import { CancelledError } from './cancelledError';
3031
import { getDiffProvider, GitExtension } from './model';
3132
import {
3233
addIcon,
@@ -342,16 +343,24 @@ export function addCommands(
342343
level: Level.SUCCESS,
343344
details
344345
});
345-
} catch (error) {
346-
console.error(
347-
trans.__('Encountered an error when pushing changes. Error: '),
348-
error
349-
);
350-
logger.log({
351-
message: trans.__('Failed to push'),
352-
level: Level.ERROR,
353-
error: error as Error
354-
});
346+
} catch (error: any) {
347+
if (error.name !== 'CancelledError') {
348+
console.error(
349+
trans.__('Encountered an error when pushing changes. Error: '),
350+
error
351+
);
352+
logger.log({
353+
message: trans.__('Failed to push'),
354+
level: Level.ERROR,
355+
error: error as Error
356+
});
357+
} else {
358+
return logger.log({
359+
//Empty logger to supress the message
360+
message: '',
361+
level: Level.INFO
362+
});
363+
}
355364
}
356365
}
357366
});
@@ -388,41 +397,49 @@ export function addCommands(
388397
level: Level.SUCCESS,
389398
details
390399
});
391-
} catch (error) {
392-
console.error(
393-
'Encountered an error when pulling changes. Error: ',
394-
error
395-
);
400+
} catch (error: any) {
401+
if (error.name !== 'CancelledError') {
402+
console.error(
403+
'Encountered an error when pulling changes. Error: ',
404+
error
405+
);
396406

397-
const errorMsg =
398-
typeof error === 'string' ? error : (error as Error).message;
407+
const errorMsg =
408+
typeof error === 'string' ? error : (error as Error).message;
399409

400-
// Discard changes then retry pull
401-
if (
402-
errorMsg
403-
.toLowerCase()
404-
.includes(
405-
'your local changes to the following files would be overwritten by merge'
406-
)
407-
) {
408-
await commands.execute(CommandIDs.gitPull, {
409-
force: true,
410-
fallback: true
411-
});
412-
} else {
413-
if ((error as any).cancelled) {
414-
// Empty message to hide alert
415-
logger.log({
416-
message: '',
417-
level: Level.INFO
410+
// Discard changes then retry pull
411+
if (
412+
errorMsg
413+
.toLowerCase()
414+
.includes(
415+
'your local changes to the following files would be overwritten by merge'
416+
)
417+
) {
418+
await commands.execute(CommandIDs.gitPull, {
419+
force: true,
420+
fallback: true
418421
});
419422
} else {
420-
logger.log({
421-
message: trans.__('Failed to pull'),
422-
level: Level.ERROR,
423-
error
424-
});
423+
if ((error as any).cancelled) {
424+
// Empty message to hide alert
425+
logger.log({
426+
message: '',
427+
level: Level.INFO
428+
});
429+
} else {
430+
logger.log({
431+
message: trans.__('Failed to pull'),
432+
level: Level.ERROR,
433+
error
434+
});
435+
}
425436
}
437+
} else {
438+
return logger.log({
439+
//Empty logger to supress the message
440+
message: '',
441+
level: Level.INFO
442+
});
426443
}
427444
}
428445
}
@@ -1577,6 +1594,8 @@ export async function showGitOperationDialog<T>(
15771594
credentials.value,
15781595
true
15791596
);
1597+
} else {
1598+
throw new CancelledError();
15801599
}
15811600
}
15821601
// Throw the error if it cannot be handled or

0 commit comments

Comments
 (0)