Skip to content

Commit b12c52c

Browse files
committed
fix: sanitizing exception strings
1 parent 2bcb2ed commit b12c52c

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/appInsights.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,19 @@ export function getCpus(): string {
5353

5454
const homeDir = os.homedir();
5555

56+
const sanitizeError = (err: Error): Error => {
57+
if (err.name) {
58+
err.name = err.name.replace(homeDir, '~');
59+
}
60+
if (err.message) {
61+
err.message = err.message.replace(homeDir, '~');
62+
}
63+
if (err.stack) {
64+
// there might be lots of this one
65+
err.stack = err.stack.replace(new RegExp(`\\b${homeDir}\\b`, 'gi'), '~');
66+
}
67+
return err;
68+
};
5669
function getSystemMemory(): string {
5770
return `${(os.totalmem() / (1024 * 1024 * 1024)).toFixed(2)} GB`;
5871
}
@@ -136,9 +149,10 @@ export class AppInsights extends AsyncCreatable<TelemetryOptions> {
136149
* @param attributes {Attributes} - map of measurements to publish alongside the exception.
137150
*/
138151
public sendTelemetryException(exception: Error, attributes: Attributes = {}): void {
139-
this.logger.debug(`Sending telemetry exception: ${exception.message}`);
152+
const cleanException = sanitizeError(exception);
153+
this.logger.debug(`Sending telemetry exception: ${cleanException.message}`);
140154
const { properties, measurements } = buildPropertiesAndMeasurements(attributes);
141-
this.appInsightsClient.trackException({ exception, properties, measurements });
155+
this.appInsightsClient.trackException({ exception: cleanException, properties, measurements });
142156
}
143157

144158
/**

0 commit comments

Comments
 (0)