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

Logger: Fix double error messages/stack #23919

Merged
merged 6 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const printErrorDetails = (error: any): void => {
} else if ((error as any).stats && (error as any).stats.compilation.errors) {
(error as any).stats.compilation.errors.forEach((e: any) => logger.plain(e));
} else {
logger.error(error as any);
logger.error(error);
}
} else if (error.compilation?.errors) {
error.compilation.errors.forEach((e: any) => logger.plain(e));
Expand Down
2 changes: 1 addition & 1 deletion code/lib/core-server/src/withTelemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export async function withTelemetry<T>(

try {
return await run();
} catch (error) {
} catch (error: any) {
if (canceled) {
return undefined;
}
Expand Down
27 changes: 25 additions & 2 deletions code/lib/node-logger/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
import { info, warn, error } from 'npmlog';
import { info, warn } from 'npmlog';
import { logger } from '.';

globalThis.console = { log: jest.fn() } as any;

jest.mock('npmlog', () => ({
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
levels: {
silly: -Infinity,
verbose: 1000,
info: 2000,
timing: 2500,
http: 3000,
notice: 3500,
warn: 4000,
error: 5000,
silent: Infinity,
},
level: 'info',
}));

//

describe('node-logger', () => {
it('should have an info method', () => {
const message = 'information';
Expand All @@ -21,6 +37,13 @@ describe('node-logger', () => {
it('should have an error method', () => {
const message = 'error message';
logger.error(message);
expect(error).toHaveBeenCalledWith('', message);
expect(globalThis.console.log).toHaveBeenCalledWith(expect.stringMatching('message'));
});
it('should format errors', () => {
const message = new Error('A complete disaster');
logger.error(message);
expect(globalThis.console.log).toHaveBeenCalledWith(
expect.stringMatching('A complete disaster')
);
});
});
19 changes: 17 additions & 2 deletions code/lib/node-logger/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,28 @@ export const logger = {
plain: (message: string): void => console.log(message),
line: (count = 1): void => console.log(`${Array(count - 1).fill('\n')}`),
warn: (message: string): void => npmLog.warn('', message),
// npmLog supports anything we log, it will just stringify it
error: (message: unknown): void => npmLog.error('', message as string),
trace: ({ message, time }: { message: string; time: [number, number] }): void =>
npmLog.info('', `${message} (${colors.purple(prettyTime(time))})`),
setLevel: (level = 'info'): void => {
npmLog.level = level;
},
error: (message: Error | string): void => {
if (npmLog.levels[npmLog.level] < npmLog.levels.error) {
let msg: string;

if (message instanceof Error && message.stack) {
msg = message.stack.toString();
} else {
msg = message.toString();
}

console.log(
msg
.replaceAll(process.cwd(), '.')
.replace(message.toString(), chalk.red(message.toString()))
ndelangen marked this conversation as resolved.
Show resolved Hide resolved
);
}
},
};

export { npmLog as instance };
Expand Down