0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -05:00

refactor: stringify error before report to AppInsights (#3964)

This commit is contained in:
Gao Sun 2023-06-05 12:38:52 +08:00 committed by GitHub
parent 598392a5ec
commit c3ff23902d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -4,10 +4,18 @@ import type { TelemetryClient } from 'applicationinsights';
export const normalizeError = (error: unknown) => {
const normalized = error instanceof Error ? error : new Error(String(error));
// Add message if empty otherwise Application Insights will respond 400
// and the error will not be recorded.
/**
* - 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 ||= 'Error occurred.';
normalized.message = JSON.stringify(
error,
// ApplicationInsights shows call stack, no need to stringify
Object.getOwnPropertyNames(error).filter((value) => value !== 'stack')
);
return normalized;
};