forked from mikelambert/react-native-fabric-crashlytics
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
63 lines (60 loc) · 2.19 KB
/
index.js
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
60
61
62
63
'use strict'
import StackTrace from 'stacktrace-js'
import {initSourceMaps, getStackTrace} from 'react-native-source-maps'
import { Crashlytics } from 'react-native-fabric'
var assign = require('lodash.assign')
function init (sourcemapName = null) {
if (__DEV__) {
// Don't send exceptions from __DEV__, it's way too noisy!
// Live reloading and hot reloading in particular lead to tons of noise...
return
}
var originalHandler = global.ErrorUtils.getGlobalHandler()
if(!originalHandler) originalHandler = ErrorUtils.getGlobalHandler()
if (sourcemapName) {
initSourceMaps({sourceMapBundle: sourcemapName})
}
async function errorHandler (e, isFatal) {
let jsParsed = false
// map the JS stacktrace using the sourcemap file given on init
let trace = null
if (sourcemapName) {
try {
trace = await getStackTrace(e)
} catch (error) {
// ignore and use the normal stack on the error
}
}
if (!trace) {
// record the exception with the info that we have
trace = await StackTrace.fromError(e, {offline: true})
}
if (trace) {
trace = trace.map((row) => (assign({}, row, {
fileName: `${row.fileName}:${row.lineNumber || 0}:${row.columnNumber || 0}`,
lineNumber: row.lineNumber || 0,
columnNumber: row.columnNumber || 0
})))
e.stack = trace
jsParsed = true
Crashlytics.recordCustomExceptionName(e.message, e.message, e.stack)
}
// And then re-throw the exception with the original handler
if (originalHandler) {
// If we want the App to actually exit on a fatal crash, we need to call the originalHandler with true as the 2nd arg
// This will create a Fatal error with Non JS stacktrace but still, the JS trace will appear as Non fatal before
// We also delay it a bit to give time for the custom exception to be sent
setTimeout(() => {
if (isFatal && jsParsed) {
originalHandler(new Error('JS crash happened. Check Fabric dashboard for non fatal errors'), true)
} else {
originalHandler(e, isFatal)
}
}, 1000)
}
}
global.ErrorUtils.setGlobalHandler(errorHandler)
}
module.exports = {
init
}