2018-04-11 17:05:31 -04:00
'use strict' ;
/* global FrankerFaceZ: false */
// ============================================================================
// Raven Logging
// ============================================================================
import { DEBUG , SENTRY _ID } from 'utilities/constants' ;
import { has } from 'utilities/object' ;
import Module from 'utilities/module' ;
import Raven from 'raven-js' ;
const BAD _URLS = [
'hls.ttvnw.net' ,
'trowel.twitch.tv' ,
'client-event-reporter.twitch.tv' ,
'.twitch.tv/gql' ,
'spade.twitch.tv'
] ;
const BAD _QUERIES = [
'ChannelPage_SetSessionStatus'
] ;
2018-04-11 20:48:02 -04:00
const ERROR _TYPES = [
Error ,
TypeError ,
SyntaxError ,
ReferenceError
] ;
const ERROR _STRINGS = [
'the ducks are on fire' ,
"it's raining in shanghai" ,
'can we just not do this today?' ,
'cbenni likes butts' ,
'guys can you please not spam the errors my mom bought me this new error report server and it gets really hot when the errors are being spammed now my leg is starting to hurt because it is getting so hot'
] ;
2018-04-11 17:05:31 -04:00
// ============================================================================
// Raven Logger
// ============================================================================
export default class RavenLogger extends Module {
constructor ( ... args ) {
super ( ... args ) ;
this . inject ( 'settings' ) ;
this . inject ( 'site' ) ;
this . inject ( 'experiments' ) ;
2018-04-11 20:48:02 -04:00
// Do these in an event handler because we're initialized before
// settings are even ready.
this . once ( 'settings:enabled' , ( ) => {
this . settings . add ( 'reports.error.enable' , {
default : true ,
ui : {
path : 'Data Management > Reporting >> Error Reports' ,
title : 'Automatically send reports when an error occurs.' ,
component : 'setting-check-box'
}
} ) ;
this . settings . add ( 'reports.error.include-user' , {
default : true ,
ui : {
path : 'Data Management > Reporting >> Error Reports' ,
title : 'Include user IDs in reports.' ,
description : "Occasionally, it's useful to know which users are encountering issues so that we can check for specific badges, emote sets, or user flags that are causing issues." ,
component : 'setting-check-box'
}
} ) ;
this . settings . add ( 'reports.error.include-settings' , {
default : true ,
ui : {
path : 'Data Management > Reporting >> Error Reports' ,
title : 'Include a settings snapshot in reports.' ,
description : 'Knowing exactly what settings are in effect when an error happens can be incredibly useful for recreating the issue.' ,
component : 'setting-check-box'
}
} ) ;
this . settings . addUI ( 'reports.error.example' , {
path : 'Data Management > Reporting >> Error Reports' ,
component : 'example-report' ,
watch : [
'reports.error.enable' ,
'reports.error.include-user' ,
'reports.error.include-settings'
] ,
data : ( ) => new Promise ( r => {
// Why fake an error when we can *make* an error?
this . _ _example _waiter = r ;
// Generate the error in a timeout so that the end user
// won't have a huge wall of a fake stack trace wasting
// their time.
const type = ERROR _TYPES [ Math . floor ( Math . random ( ) * ERROR _TYPES . length ) ] ,
msg = ERROR _STRINGS [ Math . floor ( Math . random ( ) * ERROR _STRINGS . length ) ] ;
setTimeout ( ( ) => this . log . capture ( new type ( msg ) , {
tags : {
example : true
}
} ) ) ;
} )
} )
} ) ;
2018-04-11 17:05:31 -04:00
this . raven = Raven ;
Raven . config ( SENTRY _ID , {
autoBreadcrumbs : {
console : false
} ,
release : FrankerFaceZ . version _info . toString ( ) ,
environment : DEBUG ? 'development' : 'production' ,
captureUnhandledRejections : false ,
ignoreErrors : [
'InvalidAccessError' ,
'out of memory'
] ,
whitelistUrls : [
/cdn\.frankerfacez\.com/
] ,
sanitizeKeys : [
/Token$/
] ,
breadcrumbCallback ( crumb ) {
if ( crumb . category === 'gql' ) {
for ( const matcher of BAD _QUERIES )
if ( crumb . message . includes ( matcher ) )
return false ;
}
if ( crumb . type === 'http' ) {
const url = crumb . data . url ;
for ( const matcher of BAD _URLS )
if ( url . includes ( matcher ) )
return false ;
}
return true ;
} ,
2018-04-11 20:48:02 -04:00
shouldSendCallback : data => {
if ( this . settings && ! this . settings . get ( 'reports.error.enable' ) ) {
2018-04-13 13:36:05 -04:00
if ( data . tags && data . tags . example && this . _ _example _waiter ) {
2018-04-11 20:48:02 -04:00
this . _ _example _waiter ( null ) ;
this . _ _example _waiter = null ;
}
return false ;
}
2018-04-13 13:36:05 -04:00
const exc = data . exception && data . exception . values [ 0 ] ;
2018-04-11 20:48:02 -04:00
// We don't want any of Sentry's junk.
2018-04-13 13:36:05 -04:00
if ( data . message && data . messages . includes ( 'raven-js/' ) || ( exc && JSON . stringify ( exc ) . includes ( 'raven-js/' ) ) )
2018-04-11 17:05:31 -04:00
return false ;
2018-04-11 20:48:02 -04:00
// We don't want any of Mozilla's junk either.
if ( exc && exc . type . startsWith ( 'NS_' ) )
return false ;
// Apparently, something is completely screwing up the DOM for
// at least two users? Not our problem.
if ( ! document . body || ! document . body . querySelector )
return false ;
if ( this . settings && this . settings . get ( 'reports.error.include-user' ) ) {
const user = this . site && this . site . getUser ( ) ;
if ( user )
data . user = { id : user . id , username : user . login }
}
data . extra = Object . assign ( this . buildExtra ( ) , data . extra ) ;
data . tags = Object . assign ( this . buildTags ( ) , data . tags ) ;
if ( data . tags . example ) {
if ( this . _ _example _waiter ) {
this . _ _example _waiter ( data ) ;
this . _ _example _waiter = null ;
}
return false ;
}
2018-04-11 17:05:31 -04:00
return true ;
}
} ) . install ( ) ;
}
onEnable ( ) {
2018-04-11 20:48:02 -04:00
this . log . info ( 'Installed error tracking.' ) ;
2018-04-11 17:05:31 -04:00
}
buildExtra ( ) {
2018-04-11 20:48:02 -04:00
const modules = { } ,
2018-04-11 17:05:31 -04:00
experiments = { } ,
twitch _experiments = { } ,
out = {
experiments ,
twitch _experiments ,
2018-04-11 20:48:02 -04:00
modules
2018-04-11 17:05:31 -04:00
} ;
for ( const key in this . _ _modules )
if ( has ( this . _ _modules , key ) ) {
const mod = this . _ _modules [ key ] ;
2018-04-11 20:48:02 -04:00
modules [ key ] = ` ${
mod . loaded ? 'loaded' : mod . loading ? 'loading' : 'unloaded' } $ {
mod . enabled ? 'enabled' : mod . enabling ? 'enabling' : 'disabled' } ` ;
2018-04-11 17:05:31 -04:00
}
2018-04-11 20:48:02 -04:00
if ( this . settings && this . settings . get ( 'reports.error.include-settings' ) ) {
const context = this . settings . main _context ,
chat = this . resolve ( 'chat' ) ,
chat _context = chat && chat . context ,
settings = out . settings = { } ,
chat _settings = out . chat _setting = chat _context ? { } : undefined ;
2018-04-11 17:05:31 -04:00
2018-04-11 20:48:02 -04:00
for ( const [ key , value ] of context . _ _cache . entries ( ) )
settings [ key ] = value ;
if ( chat _context )
for ( const [ key , value ] of chat _context . _ _cache . entries ( ) )
chat _settings [ key ] = value ;
}
2018-04-11 17:05:31 -04:00
for ( const [ key , value ] of Object . entries ( this . experiments . getTwitchExperiments ( ) ) )
if ( this . experiments . usingTwitchExperiment ( key ) )
twitch _experiments [ value . name ] = this . experiments . getTwitchAssignment ( key ) ;
for ( const key of Object . keys ( this . experiments . experiments ) )
experiments [ key ] = this . experiments . getAssignment ( key ) ;
return out ;
}
buildTags ( ) {
const core = this . site . getCore ( ) ,
out = { } ;
out . build = _ _webpack _hash _ _ ;
if ( core )
out . twitch _build = core . config . buildID ;
return out ;
}
addPlugin ( ... args ) { return this . raven . addPlugin ( ... args ) }
2018-04-11 20:48:02 -04:00
captureException ( exc , opts ) { return this . raven . captureException ( exc , opts ) }
captureMessage ( msg , opts ) { return this . raven . captureMessage ( msg , opts ) }
2018-04-11 17:05:31 -04:00
captureBreadcrumb ( ... args ) { return this . raven . captureBreadcrumb ( ... args ) }
}