Skip to content

Commit d41393f

Browse files
committed
feat(utils): truncate group/spinner inline errors to one-liner
1 parent 1583c73 commit d41393f

File tree

5 files changed

+30
-17
lines changed

5 files changed

+30
-17
lines changed

packages/models/src/lib/core-config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ export const unrefinedCoreConfigSchema = z.object({
2020
categories: categoriesSchema.optional(),
2121
});
2222

23-
export const coreConfigSchema = refineCoreConfig(unrefinedCoreConfigSchema);
23+
export const coreConfigSchema = refineCoreConfig(
24+
unrefinedCoreConfigSchema,
25+
).meta({ title: 'CoreConfig' });
2426

2527
/**
2628
* Add refinements to coreConfigSchema

packages/utils/mocks/logger-demo.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import ansis from 'ansis';
2+
import { coreConfigSchema, validate } from '../../models/src/index.js';
23
import { logger } from '../src/index.js';
34

45
async function sleep(delay: number) {
@@ -20,6 +21,10 @@ try {
2021
await logger.task('Importing code-pushup.config.ts', async () => {
2122
await sleep(500);
2223

24+
if (errorStage === 'config') {
25+
validate(coreConfigSchema, {}, { filePath: 'code-pushup.config.ts' });
26+
}
27+
2328
return 'Loaded configuration from code-pushup.config.ts';
2429
});
2530
logger.debug('2 plugins:');
@@ -76,7 +81,7 @@ try {
7681
'Sent GraphQL mutation to https://api.code-pushup.example.com/graphql (organization: "example", project: "website")',
7782
);
7883
await sleep(2000);
79-
if (errorStage === 'core') {
84+
if (errorStage === 'upload') {
8085
throw new Error('GraphQL error');
8186
}
8287
return ansis.bold('Uploaded report to portal');

packages/utils/project.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,14 @@
3535
"verbose": {
3636
"args": ["--verbose"]
3737
},
38-
"error-core": {
39-
"args": ["--error=core"]
38+
"error-config": {
39+
"args": ["--error=config"]
4040
},
4141
"error-plugin": {
4242
"args": ["--error=plugin"]
43+
},
44+
"error-upload": {
45+
"args": ["--error=upload"]
4346
}
4447
}
4548
}

packages/utils/src/lib/logger.int.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ ${ansis.red('Failed to load config')}
166166
`
167167
${ansis.bold.cyan('❯ Running plugin "ESLint"')}
168168
${ansis.cyan('│')} $ npx eslint . --format=json --output-file=.code-pushup/eslint/results.json
169-
${ansis.cyan('└')} ${ansis.red("Error: ENOENT: no such file or directory, open '.code-pushup/eslint/results.json'")}
169+
${ansis.cyan('└')} ${ansis.red("ENOENT: no such file or directory, open '.code-pushup/eslint/results.json'")}
170170
171171
`,
172172
);
@@ -333,7 +333,7 @@ ${ansis.magenta('└')} ${ansis.green(`Total line coverage is ${ansis.bold('82%'
333333
await expect(task).rejects.toThrow('GraphQL error: Invalid API key');
334334

335335
expect(output).toBe(
336-
`${ansis.red('✖')} Uploading report to portal → ${ansis.red('Error: GraphQL error: Invalid API key')}\n`,
336+
`${ansis.red('✖')} Uploading report to portal → ${ansis.red('GraphQL error: Invalid API key')}\n`,
337337
);
338338
});
339339

@@ -428,7 +428,7 @@ ${ansis.green('✔')} Uploaded report to portal ${ansis.gray('(42 ms)')}
428428

429429
expect(output).toBe(
430430
`
431-
${ansis.red('✖')} Uploading report to portal → ${ansis.red('Error: GraphQL error: Invalid API key')}
431+
${ansis.red('✖')} Uploading report to portal → ${ansis.red('GraphQL error: Invalid API key')}
432432
${ansis.gray('Sent request to Portal API')}
433433
${ansis.gray('Received response from Portal API')}
434434
`.trimStart(),
@@ -689,7 +689,7 @@ ${ansis.cyan('-')} ${ansis.blue('$')} npx eslint . --format=json`,
689689
`
690690
${ansis.bold.cyan('❯ Running plugin "ESLint"')}
691691
${ansis.cyan('│')} ${ansis.red('$')} npx eslint . --format=json
692-
${ansis.cyan('└')} ${ansis.red('Error: Process failed with exit code 1')}
692+
${ansis.cyan('└')} ${ansis.red('Process failed with exit code 1')}
693693
694694
`,
695695
);
@@ -786,7 +786,7 @@ ${ansis.red.bold('Cancelled by SIGINT')}
786786
│ ESLint couldn't find a configuration file.
787787
788788
│ $ npx eslint . --format=json
789-
Error: Process failed with exit code 2
789+
└ Process failed with exit code 2
790790
791791
`,
792792
);

packages/utils/src/lib/logger.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import os from 'node:os';
33
import ora, { type Ora } from 'ora';
44
import { dateToUnixTimestamp } from './dates.js';
55
import { isEnvVarEnabled } from './env.js';
6+
import { stringifyError } from './errors.js';
67
import { formatDuration, indentLines, transformLines } from './formatting.js';
78
import { settlePromise } from './promises.js';
89

@@ -19,12 +20,11 @@ const GROUP_COLOR_ENV_VAR_NAME = 'CP_LOGGER_GROUP_COLOR';
1920
export class Logger {
2021
#isVerbose = isEnvVarEnabled('CP_VERBOSE');
2122
#isCI = isEnvVarEnabled('CI');
22-
#ciPlatform: CiPlatform | undefined =
23-
process.env['GITHUB_ACTIONS'] === 'true'
24-
? 'GitHub Actions'
25-
: process.env['GITLAB_CI'] === 'true'
26-
? 'GitLab CI/CD'
27-
: undefined;
23+
#ciPlatform: CiPlatform | undefined = isEnvVarEnabled('GITHUB_ACTIONS')
24+
? 'GitHub Actions'
25+
: isEnvVarEnabled('GITLAB_CI')
26+
? 'GitLab CI/CD'
27+
: undefined;
2828
#groupColor: GroupColor | undefined =
2929
process.env[GROUP_COLOR_ENV_VAR_NAME] === 'cyan' ||
3030
process.env[GROUP_COLOR_ENV_VAR_NAME] === 'magenta'
@@ -275,7 +275,10 @@ export class Logger {
275275
console.log(
276276
[
277277
this.#colorize(this.#groupSymbols.end, this.#groupColor),
278-
this.#colorize(`${result.reason}`, 'red'),
278+
this.#colorize(
279+
`${stringifyError(result.reason, { oneline: true })}`,
280+
'red',
281+
),
279282
].join(' '),
280283
);
281284
}
@@ -391,7 +394,7 @@ export class Logger {
391394
messages.success(result.value),
392395
this.#formatDurationSuffix({ start, end }),
393396
].join(' ')
394-
: messages.failure(result.reason);
397+
: messages.failure(stringifyError(result.reason, { oneline: true }));
395398

396399
if (this.#activeSpinner) {
397400
if (this.#groupColor) {

0 commit comments

Comments
 (0)