Skip to content

Instantly share code, notes, and snippets.

@berkedel
Forked from tianjianchn/error-report.js
Created May 31, 2018 09:28
Show Gist options
  • Save berkedel/33292118d7766d873b5d032e16c55d22 to your computer and use it in GitHub Desktop.
Save berkedel/33292118d7766d873b5d032e16c55d22 to your computer and use it in GitHub Desktop.
Error report util with fabric/crashlytics in react-native
/*global __DEV__*/
import StackTrace from 'stacktrace-js';
const Fabric = require('react-native-fabric');
const { Crashlytics } = Fabric;
//call this to start capturing any no-handled errors
exports.init = function(captrueOnDebugMode){
if (__DEV__ && !captrueOnDebugMode) {
return;
}
const originalHandler = global.ErrorUtils.getGlobalHandler();
function errorHandler(e) {
exports.issue(e)
if (originalHandler) {
originalHandler(e);
}
}
global.ErrorUtils.setGlobalHandler(errorHandler);
}
//user: {id: ,name: ,email: }
exports.setUser = function(user){
const {id, name, email} = {id: 'anony', name: 'anony', email: 'anony', ...user};
Crashlytics.setUserIdentifier(id+'');
Crashlytics.setUserName(name+'');
Crashlytics.setUserEmail(email+'');
}
exports.setAttrs = function(obj){
for(let kk in obj){
exports.setAttr(kk, obj[kk]);
}
}
exports.setAttr = function(key, value){
if(!key) return;
if(typeof key !== 'string') key = key + '';
let type = typeof value;
if(type==='boolean') Crashlytics.setBool(key, value);
else if(type==='number') Crashlytics.setNumber(key, value);
else if(type==='string') Crashlytics.setString(key, value);
else Crashlytics.setString(key, JSON.stringify(value));
}
//things that will be in issue's session logs
exports.log = function(value){
if(!value) return;
if(value instanceof Error){
value = value.stack || value.message;
}
if(typeof value !== 'string') value += '';
return Crashlytics.log(value);
}
//create a new issue. fileName will be the the error message as the `index.bundle.js` is meaningless
exports.issue = function(e){
return StackTrace.fromError(e, {offline: true}).then((stack)=>{
return stack.map(row=>{
let {source, lineNumber} = row;
if(!lineNumber){
lineNumber = parseInt(source.split(':').slice(-2, -1)) || 0
}
return {fileName: e.message, lineNumber, functionName: source}
})
})
.then((stack)=>{
Crashlytics.recordCustomExceptionName(e.message, e.message, stack)
})
}
exports.crash = function(){
return Crashlytics.crash();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment