Skip to content

Commit cf00818

Browse files
SDA-4993 - Fix logging issue with error object (#2488)
1 parent 5380689 commit cf00818

2 files changed

Lines changed: 63 additions & 10 deletions

File tree

src/app/auto-update-handler.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import * as path from 'path';
88
import { version } from '../../package.json';
99
import { isMac, isWindowsOS } from '../common/env';
1010
import { logger } from '../common/logger';
11-
import { isUrl } from '../common/utils';
11+
import { formatError, isUrl } from '../common/utils';
1212
import { whitelistHandler } from '../common/whitelist-handler';
1313
import { fetchLatestVersion } from './auto-update-utils';
1414
import { sendAutoUpdateAnalytics } from './bi/auto-update-analytics';
@@ -107,8 +107,8 @@ export class AutoUpdate {
107107
this.autoUpdater.on('error', (error) => {
108108
this.autoUpdateTrigger = undefined;
109109
logger.error(
110-
'auto-update-handler: Error occurred while updating. ',
111-
error,
110+
'auto-update-handler: Error occurred while updating.',
111+
formatError(error),
112112
);
113113
});
114114
await this.performForcedAutoUpdate();
@@ -217,13 +217,21 @@ export class AutoUpdate {
217217
): Promise<void> => {
218218
this.autoUpdateTrigger = trigger;
219219
logger.info('auto-update-handler: Checking for updates', trigger);
220-
if (this.autoUpdater) {
221-
const opts: GenericServerOptions = await this.getGenericServerOptions();
222-
this.autoUpdater.setFeedURL(opts);
223-
const updateCheckResult = await this.autoUpdater.checkForUpdates();
224-
logger.info('auto-update-handler: ', updateCheckResult);
220+
try {
221+
if (this.autoUpdater) {
222+
const opts: GenericServerOptions = await this.getGenericServerOptions();
223+
this.autoUpdater.setFeedURL(opts);
224+
const updateCheckResult = await this.autoUpdater.checkForUpdates();
225+
logger.info('auto-update-handler: ', updateCheckResult);
226+
}
227+
} catch (error) {
228+
logger.error(
229+
'auto-update-handler: Error occurred while checking for updates',
230+
formatError(error),
231+
);
232+
} finally {
233+
logger.info('auto-update-handler: After checking auto update');
225234
}
226-
logger.info('auto-update-handler: After checking auto update');
227235
};
228236

229237
/**
@@ -416,7 +424,10 @@ export class AutoUpdate {
416424
this.channelConfigLocation = ChannelConfigLocation.REGISTRY;
417425
}
418426
} catch (error) {
419-
logger.error('auto-update-handler: error retrieving registry', error);
427+
logger.error(
428+
'auto-update-handler: error retrieving registry',
429+
formatError(error),
430+
);
420431
}
421432
}
422433
};

src/common/utils.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,3 +371,45 @@ export class DelayedFunctionQueue {
371371
}
372372
}
373373
}
374+
375+
/**
376+
* Converts an error into a readable and serializable format.
377+
*
378+
* - Returns `"Unknown error"` if the input is null or undefined.
379+
* - For `Error` instances, includes name, message, stack, and optional `code`/`cause`.
380+
* - For objects, returns their own properties if available, otherwise stringifies them.
381+
* - For primitives, returns their string representation.
382+
*
383+
* @param error - The error to format.
384+
* @returns A string or an object with error details.
385+
*/
386+
export const formatError = (error: any): any => {
387+
if (!error) {
388+
return 'Unknown error';
389+
}
390+
if (error instanceof Error) {
391+
const { name, message, stack } = error;
392+
const code = (error as any).code;
393+
const cause = (error as any).cause;
394+
return {
395+
name,
396+
message,
397+
stack,
398+
...(code ? { code } : {}),
399+
...(cause ? { cause: String(cause) } : {}),
400+
};
401+
}
402+
if (typeof error === 'object') {
403+
try {
404+
const ownProps = Object.getOwnPropertyNames(error as object);
405+
const details: any = {};
406+
for (const key of ownProps) {
407+
details[key] = (error as any)[key];
408+
}
409+
return Object.keys(details).length ? details : String(error);
410+
} catch {
411+
return String(error);
412+
}
413+
}
414+
return String(error);
415+
};

0 commit comments

Comments
 (0)