Skip to content

Commit

Permalink
fix: clone error for AppInsights (#3985)
Browse files Browse the repository at this point in the history
  • Loading branch information
gao-sun authored Jun 6, 2023
1 parent be43ecd commit 2e2ba5d
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions packages/app-insights/src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,30 @@ import { trySafe } from '@silverhand/essentials';
import type { TelemetryClient } from 'applicationinsights';

export const normalizeError = (error: unknown) => {
const normalized = error instanceof Error ? error : new Error(String(error));
const errorObject = error instanceof Error ? error : new Error(String(error));

/**
* - Ensure the message if not empty otherwise Application Insights will respond 400
* and the error will not be recorded.
* - We stringify error object here since other error properties won't show on the
* ApplicationInsights details page.
*/
// eslint-disable-next-line @silverhand/fp/no-mutation
normalized.message = JSON.stringify(
error,
const message = JSON.stringify(
errorObject,
// ApplicationInsights shows call stack, no need to stringify
Object.getOwnPropertyNames(error).filter((value) => value !== 'stack')
Object.getOwnPropertyNames(errorObject).filter((value) => value !== 'stack')
);

// Ensure we don't mutate the original error
const normalized = new Error(message);

// Manually clone key fields of the error for AppInsights display
/* eslint-disable @silverhand/fp/no-mutation */
normalized.name = errorObject.name;
normalized.stack = errorObject.stack;
normalized.cause = errorObject.cause;
/* eslint-enable @silverhand/fp/no-mutation */

return normalized;
};

Expand Down

0 comments on commit 2e2ba5d

Please sign in to comment.