Skip to content

Commit

Permalink
utils: Add isUserCancelledError function and use it in parseError (
Browse files Browse the repository at this point in the history
…#1443)

* utils: Add and use `isUserCancelledError` function

* Fixup docs

* Bump version to 1.1.1

* Update utils/src/errors.ts

Co-authored-by: Brandon Waterloo [MSFT] <36966225+bwateratmsft@users.noreply.github.com>

* Ensure error is not null or undefined

* Change implementation to be more TypeScripty

---------

Co-authored-by: Brandon Waterloo [MSFT] <36966225+bwateratmsft@users.noreply.github.com>
  • Loading branch information
alexweininger and bwateratmsft authored Apr 7, 2023
1 parent 5119734 commit 29504af
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 4 deletions.
7 changes: 7 additions & 0 deletions utils/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,13 @@ export declare class UserCancelledError extends Error {
constructor(stepName?: string);
}

/**
* Checks if the given error is a UserCancelledError.
*
* Note: only works with errors created by versions >=1.1.1 of this package.
*/
export declare function isUserCancelledError(error: unknown): error is UserCancelledError;

export declare class NoResourceFoundError extends Error {
constructor(context?: ITreeItemPickerContext);
}
Expand Down
4 changes: 2 additions & 2 deletions utils/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion utils/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@microsoft/vscode-azext-utils",
"author": "Microsoft Corporation",
"version": "1.1.0",
"version": "1.1.1",
"description": "Common UI tools for developing Azure extensions for VS Code",
"tags": [
"azure",
Expand Down
8 changes: 8 additions & 0 deletions utils/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,21 @@ import { ITreeItemPickerContext } from "..";
import { localize } from "./localize";

export class UserCancelledError extends Error {
_isUserCancelledError = true;
public stepName: string | undefined;
constructor(stepName?: string) {
super(localize('userCancelledError', 'Operation cancelled.'));
this.stepName = stepName;
}
}

export function isUserCancelledError(error: unknown): error is UserCancelledError {
return !!error &&
typeof error === 'object' &&
'_isUserCancelledError' in error &&
error._isUserCancelledError === true;
}

export class GoBackError extends Error {
constructor() {
super(localize('backError', 'Go back.'));
Expand Down
6 changes: 5 additions & 1 deletion utils/src/parseError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import * as htmlToText from 'html-to-text';
import { IParsedError } from '../index';
import { isUserCancelledError } from './errors';
import { localize } from './localize';
import { parseJson } from './utils/parseJson';

Expand Down Expand Up @@ -86,7 +87,10 @@ export function parseError(error: any): IParsedError {
stepName,
// NOTE: Intentionally not using 'error instanceof UserCancelledError' because that doesn't work if multiple versions of the UI package are used in one extension
// See https://github.com/Microsoft/vscode-azuretools/issues/51 for more info
isUserCancelledError: errorType === 'UserCancelledError'
isUserCancelledError:
// check using both methods in case error was created before we implemented isUserCancelledError
isUserCancelledError(error) ||
errorType === 'UserCancelledError'
};
}

Expand Down

0 comments on commit 29504af

Please sign in to comment.