@datadog/mobile-react-native
Version:
A client-side React Native module to interact with Datadog
87 lines (78 loc) • 2.72 kB
text/typescript
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2016-Present Datadog, Inc.
*/
import { debugId } from '../metro/debugIdResolver';
export const EMPTY_MESSAGE = 'Unknown Error';
export const EMPTY_STACK_TRACE = '';
export const DEFAULT_ERROR_NAME = 'Error';
export const getErrorMessage = (error: any | undefined): string => {
let message = EMPTY_MESSAGE;
if (error === undefined || error === null) {
message = EMPTY_MESSAGE;
} else if (typeof error === 'object' && 'message' in error) {
message = String(error.message);
} else {
message = String(error);
}
return message;
};
/**
* Will extract the stack from the error, taking the first key found among:
* `stacktrace`, `stack`, `componentStack` (component tree for component errors,
* which contains only native components names in production).
*
* In last resort and if `sourceURL`, `line` and `column` are present, it will
* generate a stack from this information.
*/
export const getErrorStackTrace = (error: any | undefined): string => {
let stack = EMPTY_STACK_TRACE;
try {
if (error === undefined || error === null) {
stack = EMPTY_STACK_TRACE;
} else if (typeof error === 'string') {
stack = EMPTY_STACK_TRACE;
} else if (typeof error === 'object') {
if ('stacktrace' in error) {
stack = String(error.stacktrace);
} else if ('stack' in error) {
stack = String(error.stack);
} else if ('componentStack' in error) {
stack = String(error.componentStack);
} else if (
'sourceURL' in error &&
'line' in error &&
'column' in error
) {
stack = `at ${error.sourceURL}:${error.line}:${error.column}`;
}
}
} catch (e) {
// Do nothing
}
return stack;
};
export const getErrorName = (error: unknown): string => {
try {
if (typeof error !== 'object' || error === null) {
return DEFAULT_ERROR_NAME;
}
if (typeof (error as any).name === 'string') {
return (error as any).name;
}
} catch (e) {
// Do nothing
}
return DEFAULT_ERROR_NAME;
};
export const getErrorContext = (originalContext: any): Record<string, any> => {
const _debugId = debugId;
if (!_debugId) {
return originalContext;
}
return {
...originalContext,
'_dd.debug_id': _debugId
};
};