Merge pull request #1997 from github/robertbrignull/use_as_error

Make use of asError instead of reimplementing
This commit is contained in:
Robert
2023-01-23 17:15:59 +00:00
committed by GitHub
4 changed files with 12 additions and 11 deletions

View File

@@ -8,7 +8,7 @@ import {
} from "vscode";
import { showAndLogErrorMessage, showAndLogWarningMessage } from "./helpers";
import { extLogger } from "./common";
import { getErrorMessage, getErrorStack } from "./pure/helpers-pure";
import { asError, getErrorMessage, getErrorStack } from "./pure/helpers-pure";
import { telemetryListener } from "./telemetry";
export class UserCancellationException extends Error {
@@ -126,7 +126,7 @@ export function commandRunner(
return await task(...args);
} catch (e) {
const errorMessage = `${getErrorMessage(e) || e} (${commandId})`;
error = e instanceof Error ? e : new Error(errorMessage);
error = asError(e);
const errorStack = getErrorStack(e);
if (e instanceof UserCancellationException) {
// User has cancelled this action manually
@@ -179,7 +179,7 @@ export function commandRunnerWithProgress<R>(
return await withProgress(progressOptionsWithDefaults, task, ...args);
} catch (e) {
const errorMessage = `${getErrorMessage(e) || e} (${commandId})`;
error = e instanceof Error ? e : new Error(errorMessage);
error = asError(e);
const errorStack = getErrorStack(e);
if (e instanceof UserCancellationException) {
// User has cancelled this action manually

View File

@@ -19,7 +19,7 @@ import {
} from "./archive-filesystem-provider";
import { DisposableObject } from "./pure/disposable-object";
import { Logger, extLogger } from "./common";
import { getErrorMessage } from "./pure/helpers-pure";
import { asError, getErrorMessage } from "./pure/helpers-pure";
import { QueryRunner } from "./queryRunner";
import { pathsEqual } from "./pure/files";
@@ -370,7 +370,7 @@ export class DatabaseItemImpl implements DatabaseItem {
this._error = undefined;
} catch (e) {
this._contents = undefined;
this._error = e instanceof Error ? e : new Error(String(e));
this._error = asError(e);
throw e;
}
} finally {

View File

@@ -46,14 +46,14 @@ export const REPO_REGEX = /^[a-zA-Z0-9-_\.]+\/[a-zA-Z0-9-_\.]+$/;
*/
export const OWNER_REGEX = /^[a-zA-Z0-9-_\.]+$/;
export function getErrorMessage(e: any) {
export function getErrorMessage(e: unknown) {
return e instanceof Error ? e.message : String(e);
}
export function getErrorStack(e: any) {
export function getErrorStack(e: unknown) {
return e instanceof Error ? e.stack ?? "" : "";
}
export function asError(e: any): Error {
export function asError(e: unknown): Error {
return e instanceof Error ? e : new Error(String(e));
}

View File

@@ -33,7 +33,7 @@ import { DatabaseManager } from "./databases";
import { DecodedBqrsChunk } from "./pure/bqrs-cli-types";
import { extLogger, Logger } from "./common";
import { generateSummarySymbolsFile } from "./log-insights/summary-parser";
import { asError } from "./pure/helpers-pure";
import { getErrorMessage } from "./pure/helpers-pure";
/**
* run-queries.ts
@@ -270,9 +270,10 @@ export class QueryEvaluationInfo {
);
return this.evalLogSummaryPath;
} catch (e) {
const err = asError(e);
void showAndLogWarningMessage(
`Failed to generate human-readable structured evaluator log summary. Reason: ${err.message}`,
`Failed to generate human-readable structured evaluator log summary. Reason: ${getErrorMessage(
e,
)}`,
);
return undefined;
}