Skip to content

Commit c78c726

Browse files
committed
fix: use safe error to string conversions
1 parent 5e7456f commit c78c726

File tree

15 files changed

+49
-42
lines changed

15 files changed

+49
-42
lines changed

.github/actions/code-pushup/src/runner.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
type SourceFileIssue,
1111
runInCI,
1212
} from '@code-pushup/ci';
13-
import { CODE_PUSHUP_UNICODE_LOGO } from '@code-pushup/utils';
13+
import { CODE_PUSHUP_UNICODE_LOGO, stringifyError } from '@code-pushup/utils';
1414

1515
type GitHubRefs = {
1616
head: GitBranch;
@@ -150,7 +150,7 @@ async function run(): Promise<void> {
150150

151151
core.info(`${LOG_PREFIX} Finished running successfully`);
152152
} catch (error) {
153-
const message = error instanceof Error ? error.message : String(error);
153+
const message = stringifyError(error);
154154
core.error(`${LOG_PREFIX} Failed: ${message}`);
155155
core.setFailed(message);
156156
}

packages/core/src/lib/implementation/execute-plugin.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
logMultipleResults,
1717
pluralizeToken,
1818
scoreAuditsWithTarget,
19+
stringifyError,
1920
} from '@code-pushup/utils';
2021
import {
2122
executePluginRunner,
@@ -41,8 +42,8 @@ import {
4142
* // error handling
4243
* try {
4344
* await executePlugin(pluginCfg);
44-
* } catch (e) {
45-
* console.error(e.message);
45+
* } catch (error) {
46+
* console.error(error);
4647
* }
4748
*/
4849
export async function executePlugin(
@@ -125,11 +126,9 @@ const wrapProgress = async (
125126
} catch (error) {
126127
progressBar?.incrementInSteps(steps);
127128
throw new Error(
128-
error instanceof Error
129-
? `- Plugin ${bold(pluginCfg.title)} (${bold(
130-
pluginCfg.slug,
131-
)}) produced the following error:\n - ${error.message}`
132-
: String(error),
129+
`- Plugin ${bold(pluginCfg.title)} (${bold(
130+
pluginCfg.slug,
131+
)}) produced the following error:\n - ${stringifyError(error)}`,
133132
);
134133
}
135134
};
@@ -150,8 +149,8 @@ const wrapProgress = async (
150149
* // error handling
151150
* try {
152151
* await executePlugins(plugins);
153-
* } catch (e) {
154-
* console.error(e.message); // Plugin output is invalid
152+
* } catch (error) {
153+
* console.error(error); // Plugin output is invalid
155154
* }
156155
*
157156
*/

packages/core/src/lib/implementation/persist.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
directoryExists,
88
generateMdReport,
99
logMultipleFileResults,
10+
stringifyError,
1011
ui,
1112
} from '@code-pushup/utils';
1213

@@ -51,7 +52,7 @@ export async function persistReport(
5152
try {
5253
await mkdir(outputDir, { recursive: true });
5354
} catch (error) {
54-
ui().logger.warning((error as Error).toString());
55+
ui().logger.warning(stringifyError(error));
5556
throw new PersistDirError(outputDir);
5657
}
5758
}

packages/core/src/lib/load-portal-client.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
import { ui } from '@code-pushup/utils';
1+
import { stringifyError, ui } from '@code-pushup/utils';
22

33
export async function loadPortalClient(): Promise<
44
typeof import('@code-pushup/portal-client') | null
55
> {
66
try {
77
return await import('@code-pushup/portal-client');
8-
} catch {
8+
} catch (error) {
9+
ui().logger.warning(
10+
`Failed to import @code-pushup/portal-client - ${stringifyError(error)}`,
11+
);
912
ui().logger.error(
1013
'Optional peer dependency @code-pushup/portal-client is not available. Make sure it is installed to enable upload functionality.',
1114
);

packages/plugin-lighthouse/src/lib/runner/details/opportunity.type.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { LighthouseAuditDetailsParsingError } from './utils.js';
1111
export function parseOpportunityToAuditDetailsTable(
1212
details: Details.Opportunity,
1313
): Table | undefined {
14-
const { headings: rawHeadings, items } = details;
14+
const { headings, items } = details;
1515

1616
if (items.length === 0) {
1717
return undefined;
@@ -20,14 +20,14 @@ export function parseOpportunityToAuditDetailsTable(
2020
try {
2121
return tableSchema().parse({
2222
title: 'Opportunity',
23-
columns: parseTableColumns(rawHeadings),
24-
rows: items.map(row => parseOpportunityItemToTableRow(row, rawHeadings)),
23+
columns: parseTableColumns(headings),
24+
rows: items.map(row => parseOpportunityItemToTableRow(row, headings)),
2525
});
2626
} catch (error) {
2727
throw new LighthouseAuditDetailsParsingError(
2828
'opportunity',
29-
{ items, headings: rawHeadings },
30-
(error as Error).message.toString(),
29+
{ items, headings },
30+
error,
3131
);
3232
}
3333
}

packages/plugin-lighthouse/src/lib/runner/details/opportunity.type.unit.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ describe('parseOpportunityDetails', () => {
256256
items: [null],
257257
headings,
258258
},
259-
'Cannot convert undefined or null to object',
259+
'TypeError: Cannot convert undefined or null to object',
260260
),
261261
);
262262
});

packages/plugin-lighthouse/src/lib/runner/details/table.type.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,22 @@ import { LighthouseAuditDetailsParsingError } from './utils.js';
1111
export function parseTableToAuditDetailsTable(
1212
details: Details.Table,
1313
): Table | undefined {
14-
const { headings: rawHeadings, items } = details;
14+
const { headings, items } = details;
1515

1616
if (items.length === 0) {
1717
return undefined;
1818
}
1919

2020
try {
2121
return tableSchema().parse({
22-
columns: parseTableColumns(rawHeadings),
23-
rows: items.map(row => parseTableRow(row, rawHeadings)),
22+
columns: parseTableColumns(headings),
23+
rows: items.map(row => parseTableRow(row, headings)),
2424
});
2525
} catch (error) {
2626
throw new LighthouseAuditDetailsParsingError(
2727
'table',
28-
{ items, headings: rawHeadings },
29-
(error as Error).message.toString(),
28+
{ items, headings },
29+
error,
3030
);
3131
}
3232
}

packages/plugin-lighthouse/src/lib/runner/details/table.type.unit.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ describe('parseTableToAuditDetails', () => {
301301
items: [null],
302302
headings,
303303
},
304-
'Cannot convert undefined or null to object',
304+
'TypeError: Cannot convert undefined or null to object',
305305
),
306306
);
307307
});
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import { bold } from 'ansis';
22
import type Details from 'lighthouse/types/lhr/audit-details';
3+
import { stringifyError } from '@code-pushup/utils';
34

45
export class LighthouseAuditDetailsParsingError extends Error {
56
constructor(
67
type: Details['type'],
78
rawTable: Record<string, unknown>,
8-
error: string,
9+
error: unknown,
910
) {
1011
super(
1112
`Parsing lighthouse report details ${bold(
1213
type,
13-
)} failed: \nRaw data:\n ${JSON.stringify(rawTable, null, 2)}\n${error}`,
14+
)} failed: \nRaw data:\n ${JSON.stringify(rawTable, null, 2)}\n${stringifyError(error)}`,
1415
);
1516
}
1617
}

packages/plugin-lighthouse/src/lib/runner/runner.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Config, RunnerResult } from 'lighthouse';
22
import { runLighthouse } from 'lighthouse/cli/run.js';
33
import path from 'node:path';
44
import type { AuditOutputs, RunnerFunction } from '@code-pushup/models';
5-
import { ensureDirectoryExists, ui } from '@code-pushup/utils';
5+
import { ensureDirectoryExists, stringifyError, ui } from '@code-pushup/utils';
66
import { orderSlug, shouldExpandForUrls } from '../processing.js';
77
import type { LighthouseOptions } from '../types.js';
88
import { DEFAULT_CLI_FLAGS } from './constants.js';
@@ -46,7 +46,7 @@ export function createRunnerFunction(
4646

4747
return [...acc, ...processedOutputs];
4848
} catch (error) {
49-
ui().logger.warning((error as Error).message);
49+
ui().logger.warning(stringifyError(error));
5050
return acc;
5151
}
5252
}, Promise.resolve<AuditOutputs>([]));

0 commit comments

Comments
 (0)