-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add AzureActionHandler to UI package (#53)
The AzureActionHandler provides consistent error handling and telemetry across extensions.
- Loading branch information
Showing
18 changed files
with
335 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -290,4 +290,5 @@ __pycache__/ | |
node_modules | ||
lib | ||
package-lock.json | ||
*.tgz | ||
*.tgz | ||
out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ script: | |
- npm install | ||
- npm run build | ||
- npm run lint | ||
- npm run test | ||
|
||
notifications: | ||
email: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,7 @@ node_modules/ | |
src/ | ||
tsconfig.json | ||
tslint.json | ||
*.tgz | ||
*.tgz | ||
test/ | ||
out/test/ | ||
**/*.js.map |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { commands, Event, ExtensionContext, OutputChannel, window } from 'vscode'; | ||
import TelemetryReporter from 'vscode-extension-telemetry'; | ||
import { TelemetryMeasurements, TelemetryProperties } from '../index'; | ||
import { IParsedError } from '../index'; | ||
import { localize } from './localize'; | ||
import { parseError } from './parseError'; | ||
|
||
// tslint:disable:no-any no-unsafe-any | ||
|
||
export class AzureActionHandler { | ||
private _extensionContext: ExtensionContext; | ||
private _outputChannel: OutputChannel; | ||
private _telemetryReporter: TelemetryReporter | undefined; | ||
public constructor(extensionContext: ExtensionContext, outputChannel: OutputChannel, telemetryReporter?: TelemetryReporter) { | ||
this._extensionContext = extensionContext; | ||
this._outputChannel = outputChannel; | ||
this._telemetryReporter = telemetryReporter; | ||
} | ||
|
||
public registerCommand(commandId: string, callback: (...args: any[]) => any): void { | ||
this.registerCommandWithCustomTelemetry(commandId, (_properties: TelemetryProperties, _measurements: TelemetryMeasurements, ...args: any[]) => callback(...args)); | ||
} | ||
|
||
public registerCommandWithCustomTelemetry(commandId: string, callback: (properties: TelemetryProperties, measurements: TelemetryMeasurements, ...args: any[]) => any): void { | ||
this._extensionContext.subscriptions.push(commands.registerCommand(commandId, this.wrapCallback(commandId, (trackTelemetry: () => void, properties: TelemetryProperties, measurements: TelemetryMeasurements, ...args: any[]) => { | ||
trackTelemetry(); // Always track telemetry for commands | ||
return callback(properties, measurements, ...args); | ||
}))); | ||
} | ||
|
||
public registerEvent<T>(eventId: string, event: Event<T>, callback: (trackTelemetry: () => void, ...args: any[]) => any): void { | ||
this.registerEventWithCustomTelemetry<T>(eventId, event, (trackTelemetry: () => void, _properties: TelemetryProperties, _measurements: TelemetryMeasurements, ...args: any[]) => callback(trackTelemetry, ...args)); | ||
} | ||
|
||
public registerEventWithCustomTelemetry<T>(eventId: string, event: Event<T>, callback: (trackTelemetry: () => void, properties: TelemetryProperties, measurements: TelemetryMeasurements, ...args: any[]) => any): void { | ||
this._extensionContext.subscriptions.push(event(this.wrapCallback(eventId, callback))); | ||
} | ||
|
||
private wrapCallback(callbackId: string, callback: (trackTelemetry: () => void, properties: TelemetryProperties, measurements: TelemetryMeasurements, ...args: any[]) => any): (...args: any[]) => Promise<any> { | ||
return async (...args: any[]): Promise<any> => { | ||
const start: number = Date.now(); | ||
const properties: TelemetryProperties = {}; | ||
const measurements: TelemetryMeasurements = {}; | ||
properties.result = 'Succeeded'; | ||
let sendTelemetry: boolean = false; | ||
|
||
try { | ||
await Promise.resolve(callback(() => { sendTelemetry = true; }, properties, measurements, ...args)); | ||
} catch (error) { | ||
const errorData: IParsedError = parseError(error); | ||
if (errorData.isUserCancelledError) { | ||
properties.result = 'Canceled'; | ||
} else { | ||
properties.result = 'Failed'; | ||
properties.error = errorData.errorType; | ||
properties.errorMessage = errorData.message; | ||
// Always append the error to the output channel, but only 'show' the output channel for multiline errors | ||
this._outputChannel.appendLine(localize('outputError', 'Error: {0}', errorData.message)); | ||
if (errorData.message.includes('\n')) { | ||
this._outputChannel.show(); | ||
window.showErrorMessage(localize('multilineError', 'An error has occured. Check output window for more details.')); | ||
} else { | ||
window.showErrorMessage(errorData.message); | ||
} | ||
} | ||
} finally { | ||
if (this._telemetryReporter && sendTelemetry) { | ||
const end: number = Date.now(); | ||
measurements.duration = (end - start) / 1000; | ||
this._telemetryReporter.sendTelemetryEvent(callbackId, properties, measurements); | ||
} | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { IParsedError } from '../index'; | ||
import { localize } from './localize'; | ||
|
||
// tslint:disable-next-line:no-any | ||
export function parseError(error: any): IParsedError { | ||
let errorType: string; | ||
let message: string; | ||
if (error instanceof Error) { | ||
try { | ||
// Azure errors have a JSON object in the message | ||
// tslint:disable-next-line:no-unsafe-any | ||
errorType = JSON.parse(error.message).Code; | ||
// tslint:disable-next-line:no-unsafe-any | ||
message = JSON.parse(error.message).Message; | ||
} catch (err) { | ||
errorType = error.constructor.name; | ||
message = error.message; | ||
} | ||
} else if (typeof (error) === 'object' && error !== null) { | ||
errorType = (<object>error).constructor.name; | ||
message = JSON.stringify(error); | ||
// tslint:disable-next-line:no-unsafe-any | ||
} else if (error !== undefined && error !== null && error.toString && error.toString().trim() !== '') { | ||
errorType = typeof (error); | ||
// tslint:disable-next-line:no-unsafe-any | ||
message = error.toString(); | ||
} else { | ||
errorType = typeof (error); | ||
message = localize('unknownError', 'Unknown Error'); | ||
} | ||
|
||
return { | ||
errorType: errorType, | ||
message: message, | ||
// 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' | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.