Commit 46992b94de38484d615c7ebb4dee8856d5856a0f

Authored by Olivier El Mekki
1 parent 06d8000e
Exists in master and in 1 other branch production

ADD allow js client to add filters

A new `Airbrake.addFilter( callback )` method has been added.

Before sending any error, notifier.js will try each provided callback,
passing error data as parameter. If any returns false, error won't be
sent to errbit.

This allows users to define fine grain filters for their javascript
exceptions, like filtering out any error on scripts not coming from
their domain :

    Airbrake.addFilter( function( error ){
      return error.request_url.match( /^https?:\/\/my\.domain\.com\// )
    });
Showing 1 changed file with 31 additions and 7 deletions   Show diff stats
public/javascripts/notifier.js
... ... @@ -870,6 +870,12 @@ printStackTrace.implementation.prototype = {
870 870 // Share to global scope as Airbrake ("window.Hoptoad" for backward compatibility)
871 871 Global = window.Airbrake = window.Hoptoad = Util.generatePublicAPI(_publicAPI, Config);
872 872  
  873 + Global._filters = [];
  874 +
  875 + Global.addFilter = function (cb) {
  876 + Global._filters.push(cb);
  877 + };
  878 +
873 879 function Notifier() {
874 880 this.options = Util.merge({}, Config.options);
875 881 this.xmlData = Util.merge(this.DEF_XML_DATA, Config.xmlData);
... ... @@ -921,7 +927,7 @@ printStackTrace.implementation.prototype = {
921 927 }
922 928  
923 929 return function (error) {
924   - var outputData = '',
  930 + var outputData = '', jsonData,
925 931 url = '';
926 932 //
927 933  
... ... @@ -934,9 +940,12 @@ printStackTrace.implementation.prototype = {
934 940  
935 941 switch (this.options['outputFormat']) {
936 942 case 'XML':
937   - outputData = encodeURIComponent(this.generateXML(this.generateDataJSON(error)));
938   - url = ('https:' == document.location.protocol ? 'https://' : 'http://') + this.options.host + '/notifier_api/v2/notices';
939   - _sendGETRequest(url, outputData);
  943 + jsonData = this.generateDataJSON(error);
  944 + if (this.shouldSendData(jsonData)){
  945 + outputData = encodeURIComponent(this.generateXML(jsonData));
  946 + url = ('https:' == document.location.protocol ? 'https://' : 'http://') + this.options.host + '/notifier_api/v2/notices';
  947 + _sendGETRequest(url, outputData);
  948 + }
940 949 break;
941 950  
942 951 case 'JSON':
... ... @@ -945,9 +954,12 @@ printStackTrace.implementation.prototype = {
945 954 * http://collect.airbrake.io/api/v3/projects/[PROJECT_ID]/notices?key=[API_KEY]
946 955 * url = window.location.protocol + '://' + this.options.host + '/api/v3/projects' + this.options.projectId + '/notices?key=' + this.options.key;
947 956 */
948   - outputData = JSON.stringify(this.generateJSON(this.generateDataJSON(error)));
949   - url = ('https:' == document.location.protocol ? 'https://' : 'http://') + this.options.host + '/api/v3/projects/' + this.options.projectId + '/notices?key=' + this.xmlData.key;
950   - _sendPOSTRequest(url, outputData);
  957 + jsonData = this.generateDataJSON(error);
  958 + if (this.shouldSendData(jsonData)){
  959 + outputData = JSON.stringify(this.generateJSON(jsonData));
  960 + url = ('https:' == document.location.protocol ? 'https://' : 'http://') + this.options.host + '/api/v3/projects/' + this.options.projectId + '/notices?key=' + this.xmlData.key;
  961 + _sendPOSTRequest(url, outputData);
  962 + }
951 963 break;
952 964  
953 965 default:
... ... @@ -1175,6 +1187,18 @@ printStackTrace.implementation.prototype = {
1175 1187 }
1176 1188  
1177 1189 return true;
  1190 + },
  1191 +
  1192 + shouldSendData: function (jsonData) {
  1193 + var shouldSend = true, i;
  1194 +
  1195 + for ( i = 0; i < Global._filters.length; i++ ) {
  1196 + if ( ! Global._filters[i](jsonData) ){
  1197 + shouldSend = false;
  1198 + }
  1199 + }
  1200 +
  1201 + return shouldSend;
1178 1202 }
1179 1203 };
1180 1204  
... ...