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

fix: reduce the size of log messages for update errors #254

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 14 additions & 3 deletions src/lib/logger.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-return */
import process from 'node:process';
import { inspect } from 'node:util';
import * as winston from 'winston';

const objectFormatter = (object: Record<string, unknown>) => {
const objectWithoutSymbols = Object.fromEntries(Object.entries(object));
const objectFormatter = (object: Record<string, any>) => {
const entries = Object.entries(object).map(([ key, value ]) => {
if (key === 'timings' && value && typeof value === 'object') {
return [ key, { phases: value.phases }];
} else if (key === 'options' && value && value.url && typeof value.url === 'object') {
return [ key, { url: { href: value.url.href } }];
}

return [ key, value ];
});

const objectWithoutSymbols = Object.fromEntries(entries);
return inspect(objectWithoutSymbols);
};

Expand All @@ -13,7 +24,7 @@ const logger = winston.createLogger({
winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
winston.format.prettyPrint(),
winston.format.printf((info: winston.Logform.TransformableInfo) => {
const { timestamp, level, scope, message, stack, ...otherFields } = info; // eslint-disable-line @typescript-eslint/no-unsafe-assignment
const { timestamp, level, scope, message, stack, ...otherFields } = info;
let result = `[${timestamp as string}] [${level.toUpperCase()}] [${scope as string}] ${message as string}`;

if (Object.keys(otherFields).length > 0) {
Expand Down
12 changes: 10 additions & 2 deletions src/lib/updater.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import config from 'config';
import process from 'node:process';
import _ from 'lodash';
import got, { TimeoutError } from 'got';
import got, { HTTPError, RequestError, TimeoutError } from 'got';
import { VERSION } from '../constants.js';
import { scopedLogger } from './logger.js';

Expand Down Expand Up @@ -30,7 +30,15 @@ const checkForUpdates = () => {
process.kill(process.pid, 'SIGTERM');
}).catch((error: unknown) => {
if (error instanceof TimeoutError) {
logger.warn('The server timed out, while checking for a new version.');
logger.warn('The request timed out while checking for a new probe version.');
logger.warn(error);
return;
} else if (error instanceof HTTPError) {
logger.warn('The request failed with an HTTP error while checking for a new probe version.');
logger.warn(error);
return;
} else if (error instanceof RequestError) {
logger.warn('The request failed while checking for a new probe version.');
logger.warn(error);
return;
}
Expand Down
8 changes: 7 additions & 1 deletion test/unit/lib/updater.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ import * as td from 'testdouble';
import * as sinon from 'sinon';
import * as constants from '../../../src/constants.js';

class MockHTTPError extends Error {}
class MockRequestError extends Error {}
class MockTimeoutError extends Error {}

describe('updater module', () => {
let sandbox: sinon.SinonSandbox;
const gotStub = sinon.stub();

before(async () => {
await td.replaceEsm('got', { TimeoutError: MockTimeoutError }, gotStub);
await td.replaceEsm('got', {
HTTPError: MockHTTPError,
RequestError: MockRequestError,
TimeoutError: MockTimeoutError,
}, gotStub);
});

beforeEach(() => {
Expand Down
Loading