Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: handle startup errors better [IDE-5] #524

Merged
merged 7 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,8 @@
"viewsWelcome": [
{
"view": "snyk.views.welcome",
"contents": "Snyk has encountered a problem. Please restart the extension: \n[Restart](command:snyk.start 'Restart Snyk')\nIf the error persists, please check your [settings](command:snyk.settings) and [contact us](https://snyk.io/contact-us/?utm_source=vsc)!",
"when": "snyk:error == 'blocking'"
"contents": "Snyk has encountered a problem. Please restart the extension: \n[Restart](command:snyk.start 'Restart Snyk')\nIf the error persists, please check your [settings](command:snyk.settings) and [contact us](https://snyk.io/contact-us/?utm_source=vsc).\n\n You can check the logs to see the exact error in [Snyk Security](command:snyk.showOutputChannel) and [Snyk Language Server](command:snyk.showLsOutputChannel) output channels.\n[Display Error](command:snyk.showErrorFromContext)\n",
"when": "snyk:error"
},
{
"view": "snyk.views.welcome",
Expand Down
1 change: 1 addition & 0 deletions src/snyk/common/constants/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const SNYK_OPEN_ISSUE_COMMAND = 'snyk.showissue';
export const SNYK_IGNORE_ISSUE_COMMAND = 'snyk.ignoreissue';
export const SNYK_SHOW_OUTPUT_COMMAND = 'snyk.showOutputChannel';
export const SNYK_SHOW_LS_OUTPUT_COMMAND = 'snyk.showLsOutputChannel';
export const SNYK_SHOW_ERROR_FROM_CONTEXT_COMMAND = 'snyk.showErrorFromContext';
export const SNYK_GET_LESSON_COMMAND = 'snyk.getLearnLesson';
export const SNYK_GET_SETTINGS_SAST_ENABLED = 'snyk.getSettingsSastEnabled';
export const SNYK_SET_BASE_BRANCH_COMMAND = 'snyk.setBaseBranch';
Expand Down
4 changes: 0 additions & 4 deletions src/snyk/common/constants/views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ export const SNYK_CONTEXT = {
DELTA_FINDINGS_ENABLED: 'deltaFindingsEnabled',
};

export const SNYK_ERROR_CODES = {
BLOCKING: 'blocking',
};

export const SNYK_ANALYSIS_STATUS = {
FILTERS: 'Supported extentions',
COLLECTING: 'Collecting files',
Expand Down
4 changes: 2 additions & 2 deletions src/snyk/common/error/errorHandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ILoadingBadge } from '../../base/views/loadingBadge';
import { SNYK_CONTEXT, SNYK_ERROR_CODES } from '../constants/views';
import { SNYK_CONTEXT } from '../constants/views';
import { ILog } from '../logger/interfaces';
import { IContextService } from '../services/contextService';
import { ErrorReporter, Tags } from './errorReporter';
Expand All @@ -12,12 +12,12 @@
* Should be used only if the affected error breaks the whole extension.
*/
static async handleGlobal(
error: Error | unknown,

Check warning on line 15 in src/snyk/common/error/errorHandler.ts

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest)

'unknown' overrides all other types in this union type

Check warning on line 15 in src/snyk/common/error/errorHandler.ts

View workflow job for this annotation

GitHub Actions / Build and Test (macos-latest)

'unknown' overrides all other types in this union type

Check warning on line 15 in src/snyk/common/error/errorHandler.ts

View workflow job for this annotation

GitHub Actions / Build and Test (windows-latest)

'unknown' overrides all other types in this union type
logger: ILog,
contextService: IContextService,
loadingBadge: ILoadingBadge,
): Promise<void> {
await contextService.setContext(SNYK_CONTEXT.ERROR, SNYK_ERROR_CODES.BLOCKING);
await contextService.setContext(SNYK_CONTEXT.ERROR, error);
loadingBadge.setLoadingBadge(true);
ErrorHandler.handle(error, logger);
}
Expand All @@ -25,13 +25,13 @@
/**
* Should be used to log locally and report error event remotely.
*/
static handle(error: Error | unknown, logger: ILog, message?: string, tags?: Tags): void {

Check warning on line 28 in src/snyk/common/error/errorHandler.ts

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest)

'unknown' overrides all other types in this union type

Check warning on line 28 in src/snyk/common/error/errorHandler.ts

View workflow job for this annotation

GitHub Actions / Build and Test (macos-latest)

'unknown' overrides all other types in this union type

Check warning on line 28 in src/snyk/common/error/errorHandler.ts

View workflow job for this annotation

GitHub Actions / Build and Test (windows-latest)

'unknown' overrides all other types in this union type
const errorStr = ErrorHandler.stringifyError(error);
logger.error(message ? `${message}. ${errorStr}` : errorStr);
ErrorReporter.capture(error, tags);
}

static stringifyError(error: Error | unknown): string {

Check warning on line 34 in src/snyk/common/error/errorHandler.ts

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest)

'unknown' overrides all other types in this union type

Check warning on line 34 in src/snyk/common/error/errorHandler.ts

View workflow job for this annotation

GitHub Actions / Build and Test (macos-latest)

'unknown' overrides all other types in this union type

Check warning on line 34 in src/snyk/common/error/errorHandler.ts

View workflow job for this annotation

GitHub Actions / Build and Test (windows-latest)

'unknown' overrides all other types in this union type
return JSON.stringify(error, Object.getOwnPropertyNames(error));
}
}
2 changes: 1 addition & 1 deletion src/snyk/common/services/productService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { CodeActionsProvider } from '../editor/codeActionsProvider';
import { ILanguageServer } from '../languageServer/languageServer';
import { Issue, Scan, ScanProduct, ScanStatus } from '../languageServer/types';
import { ILog } from '../logger/interfaces';
import { IViewManagerService } from '../services/viewManagerService';
import { IViewManagerService } from './viewManagerService';
import { IProductWebviewProvider } from '../views/webviewProvider';
import { ExtensionContext } from '../vscode/extensionContext';
import { IVSCodeLanguages } from '../vscode/languages';
Expand Down
14 changes: 9 additions & 5 deletions src/snyk/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { AuthenticationService } from './base/services/authenticationService';
import { ScanModeService } from './base/services/scanModeService';
import { EmptyTreeDataProvider } from './base/views/emptyTreeDataProvider';
import { SupportProvider } from './base/views/supportProvider';
import { messages } from './cli/messages/messages';
import { CommandController } from './common/commands/commandController';
import { OpenIssueCommandArg } from './common/commands/types';
import { configuration } from './common/configuration/instance';
Expand All @@ -21,6 +20,7 @@ import {
SNYK_SET_BASE_BRANCH_COMMAND,
SNYK_SET_TOKEN_COMMAND,
SNYK_SETTINGS_COMMAND,
SNYK_SHOW_ERROR_FROM_CONTEXT_COMMAND,
SNYK_SHOW_LS_OUTPUT_COMMAND,
SNYK_SHOW_OUTPUT_COMMAND,
SNYK_START_COMMAND,
Expand Down Expand Up @@ -77,7 +77,7 @@ import OssIssueTreeProvider from './snykOss/providers/ossVulnerabilityTreeProvid
import { OssVulnerabilityCountService } from './snykOss/services/vulnerabilityCount/ossVulnerabilityCountService';
import { FeatureFlagService } from './common/services/featureFlagService';
import { DiagnosticsIssueProvider } from './common/services/diagnosticsService';
import { CodeIssueData, IacIssueData, OssIssueData, ScanProduct } from './common/languageServer/types';
import { CodeIssueData, IacIssueData, OssIssueData } from './common/languageServer/types';

class SnykExtension extends SnykLib implements IExtension {
public async activate(vscodeContext: vscode.ExtensionContext): Promise<void> {
Expand Down Expand Up @@ -270,8 +270,8 @@ class SnykExtension extends SnykLib implements IExtension {
this.folderConfigs,
);

let securityCodeView = SNYK_VIEW_ANALYSIS_CODE_SECURITY;
let codeQualityView = SNYK_VIEW_ANALYSIS_CODE_QUALITY;
const securityCodeView = SNYK_VIEW_ANALYSIS_CODE_SECURITY;
const codeQualityView = SNYK_VIEW_ANALYSIS_CODE_QUALITY;

const codeSecurityTree = vscode.window.createTreeView(securityCodeView, {
treeDataProvider: codeSecurityIssueProvider,
Expand Down Expand Up @@ -409,7 +409,7 @@ class SnykExtension extends SnykLib implements IExtension {

private initDependencyDownload(): DownloadService {
this.downloadService.downloadOrUpdate().catch(err => {
Logger.error(`${messages.lsDownloadFailed} ${ErrorHandler.stringifyError(err)}`);
void ErrorHandler.handleGlobal(err, Logger, this.contextService, this.loadingBadge);
});

return this.downloadService;
Expand Down Expand Up @@ -444,6 +444,10 @@ class SnykExtension extends SnykLib implements IExtension {
vscode.commands.registerCommand(SNYK_SET_BASE_BRANCH_COMMAND, (folderPath: string) =>
this.commandController.setBaseBranch(folderPath),
),
vscode.commands.registerCommand(SNYK_SHOW_ERROR_FROM_CONTEXT_COMMAND, () => {
const err = this.contextService.viewContext[SNYK_CONTEXT.ERROR] as Error;
void vscode.window.showErrorMessage(err.message);
}),
);
}
}
Expand Down
1 change: 0 additions & 1 deletion src/snyk/snykCode/views/webviewPanelSerializer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as vscode from 'vscode';
import { WebviewProvider } from '../../../snyk/common/views/webviewProvider';
import { Logger } from '../../common/logger/logger';

export class WebviewPanelSerializer<Provider extends WebviewProvider<State>, State>
implements vscode.WebviewPanelSerializer
Expand Down
2 changes: 1 addition & 1 deletion src/snyk/snykOss/ossService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Subscription } from 'rxjs';
import { IConfiguration } from '../common/configuration/configuration';
import { IWorkspaceTrust } from '../common/configuration/trustedFolders';
import { ILanguageServer } from '../common/languageServer/languageServer';
import { CodeIssueData, OssIssueData, Scan, ScanProduct } from '../common/languageServer/types';
import { OssIssueData, Scan, ScanProduct } from '../common/languageServer/types';
import { ILog } from '../common/logger/interfaces';
import { ProductService } from '../common/services/productService';
import { IViewManagerService } from '../common/services/viewManagerService';
Expand Down
1 change: 0 additions & 1 deletion src/test/unit/common/commands/commandController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import * as util from 'util';
import { IAuthenticationService } from '../../../../snyk/base/services/authenticationService';
import { ScanModeService } from '../../../../snyk/base/services/scanModeService';
import { CommandController } from '../../../../snyk/common/commands/commandController';
import { COMMAND_DEBOUNCE_INTERVAL } from '../../../../snyk/common/constants/general';
import { CodeIssueData, IacIssueData } from '../../../../snyk/common/languageServer/types';
import { IOpenerService } from '../../../../snyk/common/services/openerService';
import { IProductService } from '../../../../snyk/common/services/productService';
Expand Down
Loading