jsErrYLT.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /**
  2. * Meters the number of page errors, and provides traces as offenders for "jsErrors" metric
  3. */
  4. exports.version = '0.3.a';
  5. exports.module = function(phantomas) {
  6. 'use strict';
  7. phantomas.setMetric('jsErrors'); // @desc number of JavaScript errors
  8. function formatTrace(trace) {
  9. var ret = [];
  10. if(Array.isArray(trace)) {
  11. trace.forEach(function(entry) {
  12. ret.push((entry.function ? entry.function + ' ' : '') + (entry.sourceURL || entry.file) + ':' + entry.line);
  13. });
  14. }
  15. return ret;
  16. }
  17. phantomas.on('jserror', function(msg, trace) {
  18. trace = formatTrace(trace);
  19. phantomas.log(msg);
  20. phantomas.log('Backtrace: ' + trace.join(' / '));
  21. phantomas.incrMetric('jsErrors');
  22. phantomas.addOffender('jsErrors', msg + ' - ' + trace.join(' / '));
  23. // Yeah, this is weird, i'm sending the error back to the browser...
  24. phantomas.evaluate(function(msg, caller, trace) {
  25. (function(phantomas) {
  26. phantomas.pushContext({
  27. type: 'error',
  28. callDetails: {
  29. arguments: [msg]
  30. },
  31. caller: caller,
  32. backtrace: trace
  33. });
  34. })(window.__phantomas);
  35. }, msg, trace[0], trace.join(' / '));
  36. });
  37. };