Skip to content

Commit 412f33e

Browse files
authored
Log internal errors with logger (#275)
* feat: log internal errors with logger * add changelog
1 parent 87e1aef commit 412f33e

File tree

5 files changed

+30
-17
lines changed

5 files changed

+30
-17
lines changed

.changeset/busy-snails-play.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@css-modules-kit/codegen': patch
3+
---
4+
5+
feat: log internal errors with logger

packages/codegen/bin/cmk.mjs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#!/usr/bin/env node
22
/* eslint-disable n/no-process-exit */
33

4-
import { SystemError } from '@css-modules-kit/core';
54
import { createLogger, parseCLIArgs, printHelpText, printVersion, runCMK, shouldBePretty } from '../dist/index.js';
65

76
const cwd = process.cwd();
@@ -21,9 +20,6 @@ try {
2120

2221
await runCMK(args, logger);
2322
} catch (e) {
24-
if (e instanceof SystemError) {
25-
logger.logSystemError(e);
26-
process.exit(1);
27-
}
28-
throw e;
23+
logger.logError(e);
24+
process.exit(1);
2925
}

packages/codegen/src/logger/logger.test.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,25 @@ describe('createLogger', () => {
3434
"
3535
`);
3636
});
37-
test('logSystemError', () => {
37+
test('logError', () => {
3838
const logger = createLogger(cwd, false);
39-
logger.logSystemError(new SystemError('CODE', 'message'));
39+
logger.logError(new SystemError('CODE', 'message'));
4040
expect(stripVTControlCharacters(stderrWriteSpy.mock.lastCall![0] as string)).toMatchInlineSnapshot(`
4141
"error: message
4242
4343
"
4444
`);
45-
logger.logSystemError(
46-
new ReadCSSModuleFileError('/app/a.module.css', new Error('EACCES: permission denied, open ...')),
47-
);
45+
logger.logError(new ReadCSSModuleFileError('/app/a.module.css', new Error('EACCES: permission denied, open ...')));
4846
expect(stripVTControlCharacters(stderrWriteSpy.mock.lastCall![0] as string)).toMatchInlineSnapshot(`
4947
"error: Failed to read CSS Module file /app/a.module.css.: EACCES: permission denied, open ...
5048
5149
"
5250
`);
51+
logger.logError(new Error('Some unknown error'));
52+
// Check that error message and stack trace are included
53+
expect(stripVTControlCharacters(stderrWriteSpy.mock.lastCall![0] as string)).toMatch(
54+
/^Error: Some unknown error\n {4}at /mu,
55+
);
5356
});
5457
test('logMessage', () => {
5558
const logger = createLogger(cwd, false);

packages/codegen/src/logger/logger.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import { inspect } from 'node:util';
12
import type { DiagnosticSourceFile } from '@css-modules-kit/core';
2-
import { convertDiagnostic, convertSystemError, type Diagnostic, type SystemError } from '@css-modules-kit/core';
3+
import { convertDiagnostic, convertSystemError, type Diagnostic, SystemError } from '@css-modules-kit/core';
34
import ts from 'typescript';
45
import { formatDiagnostics } from './formatter.js';
56

67
export interface Logger {
78
logDiagnostics(diagnostics: Diagnostic[]): void;
8-
logSystemError(error: SystemError): void;
9+
logError(error: unknown): void;
910
logMessage(message: string): void;
1011
}
1112

@@ -29,9 +30,17 @@ export function createLogger(cwd: string, pretty: boolean): Logger {
2930
);
3031
process.stderr.write(result);
3132
},
32-
logSystemError(error: SystemError): void {
33-
const result = formatDiagnostics([convertSystemError(error)], host, pretty);
34-
process.stderr.write(result);
33+
logError(error: unknown): void {
34+
// NOTE: SystemErrors are errors expected by the css-modules-kit specification and may occur within normal usage.
35+
// These errors are formatted clearly and concisely. No stack trace is output.
36+
//
37+
// All other errors are unexpected errors. To assist in debugging when these errors occur, a stack trace is output.
38+
if (error instanceof SystemError) {
39+
const result = formatDiagnostics([convertSystemError(error)], host, pretty);
40+
process.stderr.write(result);
41+
} else {
42+
process.stderr.write(`${inspect(error, { colors: pretty })}\n`);
43+
}
3544
},
3645
logMessage(message: string): void {
3746
process.stdout.write(`${message}\n`);

packages/codegen/src/test/logger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { Logger } from '../logger/logger.js';
44
export function createLoggerSpy() {
55
return {
66
logDiagnostics: vi.fn(),
7-
logSystemError: vi.fn(),
7+
logError: vi.fn(),
88
logMessage: vi.fn(),
99
} satisfies Logger;
1010
}

0 commit comments

Comments
 (0)