1 /* eslint @typescript-eslint/no-use-before-define: 0 */
4 * Returns `true` if the passed parameter is an `Error`.
6 export const isError = (e: unknown): e is Error =>
9 typeof e === 'object' &&
11 typeof e.name === 'string' &&
13 typeof e.message === 'string');
15 type SafeValue = string | number | boolean | undefined | null | SafeObject | SafeValue[];
16 type SafeObject = { [key: string]: SafeValue };
17 export type SafeErrorObject = {
22 // Used by Drive errors
23 isEnrichedError?: boolean;
25 sentryMessage?: string;
29 * Transforms an arbitrary value into something that is safe to transfer through `postMessage`.
31 export function getSafeValue(value: any): SafeValue {
33 typeof value === 'string' ||
34 typeof value === 'number' ||
35 typeof value === 'boolean' ||
36 value === undefined ||
43 return getSafeErrorObject(value);
46 if (Array.isArray(value)) {
47 return getSafeArray(value);
50 if (typeof value === 'object') {
51 return getSafeObject(value);
58 * Returns an array of objects that is safe to transfer through `postMessage`.
60 * Some elements may be transformed into `undefined` if they are not safe.
62 export function getSafeArray(value: any[]): SafeValue[] {
63 return value.map(getSafeValue);
67 * Returns an object that is safe to transfer through `postMessage`.
69 export function getSafeObject(obj: any): SafeObject | undefined {
70 if (typeof obj !== 'object') {
74 let result: SafeObject = {};
76 Object.entries(obj).forEach(([key, value]) => {
77 const safeValue = getSafeValue(value);
79 if (safeValue !== undefined) {
80 result[key] = safeValue;
88 * Returns a safe Error-like object that can be transferred through `postMessage`.
89 * Mainly needed because Safari <= 16 errors when cloning Error objects.
91 export function getSafeErrorObject(error: Error): SafeErrorObject {
94 message: error.message,
97 // Used by Drive errors to provide additional data to Sentry
98 isEnrichedError: (error as any).isEnrichedError,
99 context: getSafeObject((error as any).context),
100 sentryMessage: (error as any).sentryMessage,