This repository has been archived by the owner on Mar 12, 2020. It is now read-only.
/
ErrorHandler.ts
59 lines (56 loc) · 1.88 KB
/
ErrorHandler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { Platform } from 'react-native'
import StackTrace from 'stacktrace-js'
import Firebase from 'react-native-firebase'
const originalHandler = ErrorUtils.getGlobalHandler()
export function errorHandler(error: any, isFatal?: boolean) {
if (__DEV__) {
return
}
const e = new Error()
if (typeof error === 'string') {
e.name = error
e.message = error
} else if (typeof error === 'number') {
e.name = error.toString()
e.message = error.toString()
} else if (error instanceof Error) {
e.name = error.name
e.message = error.message
e.stack = error.stack
} else {
e.name = 'unknown'
e.message = 'unknown'
}
StackTrace.fromError(e, { offline: true }).then(frames => {
// Crashlytics.recordCustomExceptionName(e.message, e.message, frames)
const updatedFrames = frames.map(row => ({
lineNumber: row.lineNumber,
columnNumber: row.columnNumber,
functionName: row.functionName,
fileName: `${row.fileName}:${row.lineNumber || 0}:${row.columnNumber ||
0}`
}))
Firebase.crashlytics().recordError(0, e.message)
// Crashlytics.recordCustomExceptionName(e.message, e.message, updatedFrames)
})
if (originalHandler) {
if (Platform.OS === 'ios') {
originalHandler(error, isFatal)
} else {
// On Android, throwing the original exception immediately results in the
// recordCustomExceptionName() not finishing before the app crashes and therefore not logged
// Add a delay to give it time to log the custom JS exception before crashing the app.
// The user facing effect of this delay is that separate JS errors will appear as separate
// issues in the Crashlytics dashboard.
setTimeout(() => {
originalHandler(error, isFatal)
}, 500)
}
}
}
export function initErrorHandler() {
if (__DEV__) {
return
}
ErrorUtils.setGlobalHandler(errorHandler)
}