Skip to content

Instantly share code, notes, and snippets.

@femotizo
Forked from tianjianchn/error-report.js
Created July 19, 2018 15:24
Show Gist options
  • Save femotizo/51a26a5630b5ee9ecbb21a9937e47e24 to your computer and use it in GitHub Desktop.
Save femotizo/51a26a5630b5ee9ecbb21a9937e47e24 to your computer and use it in GitHub Desktop.

Revisions

  1. @kilihorse kilihorse revised this gist Jul 16, 2016. No changes.
  2. @kilihorse kilihorse revised this gist Jul 16, 2016. 1 changed file with 11 additions and 5 deletions.
    16 changes: 11 additions & 5 deletions error-report.js
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@ exports.init = function(captrueOnDebugMode){

    const originalHandler = global.ErrorUtils.getGlobalHandler();
    function errorHandler(e) {
    exports.issue(e);
    exports.issue(e)
    if (originalHandler) {
    originalHandler(e);
    }
    @@ -58,15 +58,21 @@ exports.log = function(value){

    //create a new issue. fileName will be the the error message as the `index.bundle.js` is meaningless
    exports.issue = function(e){
    StackTrace.fromError(e).then((stack)=>{
    stack = stack.map(row=>{
    const {source, lineNumber} = row;
    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();
    }

  3. @kilihorse kilihorse revised this gist Jul 6, 2016. No changes.
  4. @kilihorse kilihorse created this gist Jul 6, 2016.
    72 changes: 72 additions & 0 deletions error-report.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    /*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){
    StackTrace.fromError(e).then((stack)=>{
    stack = stack.map(row=>{
    const {source, lineNumber} = row;
    return {fileName: e.message, lineNumber, functionName: source}
    })
    Crashlytics.recordCustomExceptionName(e.message, e.message, stack)
    });
    }

    exports.crash = function(){
    return Crashlytics.crash();
    }