File size: 3,248 Bytes
1e92f2d |
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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
import debug from 'debug';
import TraceKit from 'tracekit';
import wpcom from 'calypso/lib/wp';
/**
* Module variables
*/
/**
* Interval for error reports so we don't flood te endpoint. More frequent
* reports get throttled.
* @type {number}
*/
const REPORT_INTERVAL = 60000;
/**
* Debug logger
* @type {Function}
*/
const log = debug( 'calypso:error-logger' );
export default class ErrorLogger {
constructor() {
this.diagnosticData = {
commit: COMMIT_SHA,
extra: {
previous_paths: [],
throttled: 0,
},
};
this.diagnosticReducers = [];
this.lastReport = 0;
if ( ! window.onerror ) {
TraceKit.report.subscribe( ( errorReport ) => {
const error = {
message: errorReport.message,
url: document.location.href,
};
if ( Array.isArray( errorReport.stack ) ) {
const trace = errorReport.stack.slice( 0, 10 );
trace.forEach( ( report ) =>
Object.keys( report ).forEach( ( key ) => {
if ( key === 'context' && report[ key ] ) {
report[ key ] = JSON.stringify( report[ key ] ).substring( 0, 256 );
} else if ( typeof report[ key ] === 'string' && report[ key ].length > 512 ) {
report[ key ] = report[ key ].substring( 0, 512 );
} else if ( Array.isArray( report[ key ] ) ) {
report[ key ] = report[ key ].slice( 0, 3 );
}
} )
);
if ( JSON.stringify( trace ).length < 8192 ) {
error.trace = trace;
}
}
const now = Date.now();
if ( this.lastReport + REPORT_INTERVAL < now ) {
this.lastReport = now;
this.diagnose();
this.sendToApi( Object.assign( error, this.diagnosticData ) );
this.diagnosticData.extra.throttled = 0;
} else {
this.diagnosticData.extra.throttled++;
}
} );
}
}
saveNewPath( newPath ) {
const paths = this.diagnosticData.extra.previous_paths;
paths.unshift( newPath );
this.diagnosticData.extra.previous_paths = paths.slice( 0, 5 );
this.diagnosticData.calypso_path = newPath;
}
saveDiagnosticReducer( fn ) {
this.diagnosticReducers.push( fn );
}
saveDiagnosticData( data ) {
if ( typeof data.extra === 'object' ) {
this.saveExtraData( data );
} else {
Object.assign( this.diagnosticData, data );
}
}
saveExtraData( data ) {
Object.assign( this.diagnosticData.extra, data );
}
diagnose() {
this.diagnosticReducers.forEach( ( diagnosticReducer ) => {
try {
this.saveDiagnosticData( diagnosticReducer() );
} catch ( e ) {
this.saveDiagnosticData( { diagnosticError: e.message } );
log( 'diagnostic: %o', this.diagnosticData );
}
} );
return this.diagnosticData;
}
log( msg, data ) {
if ( typeof data === 'object' ) {
this.saveExtraData( data );
}
try {
TraceKit.report( new Error( msg ) );
} catch ( e ) {}
}
sendToApi( error ) {
const onUnableToRecordError = () => {
// eslint-disable-next-line no-console
console.error( 'Error: Unable to record the error in Logstash.' );
};
try {
wpcom.req.post(
{
path: '/js-error',
apiNamespace: 'rest/v1.1',
body: {
error: JSON.stringify( error ),
},
},
( err ) => err && onUnableToRecordError()
);
} catch {
onUnableToRecordError();
}
}
}
|