exception-stream.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /**
  2. * exception-stream.js: TODO: add file header handler.
  3. *
  4. * (C) 2010 Charlie Robbins
  5. * MIT LICENCE
  6. */
  7. 'use strict';
  8. const Writable = require('readable-stream/writable');
  9. /**
  10. * TODO: add class description.
  11. * @type {ExceptionStream}
  12. * @extends {Writable}
  13. */
  14. module.exports = class ExceptionStream extends Writable {
  15. /**
  16. * Constructor function for the ExceptionStream responsible for wrapping a
  17. * TransportStream; only allowing writes of `info` objects with
  18. * `info.exception` set to true.
  19. * @param {!TransportStream} transport - Stream to filter to exceptions
  20. */
  21. constructor(transport) {
  22. super({ objectMode: true });
  23. if (!transport) {
  24. throw new Error('ExceptionStream requires a TransportStream instance.');
  25. }
  26. // Remark (indexzero): we set `handleExceptions` here because it's the
  27. // predicate checked in ExceptionHandler.prototype.__getExceptionHandlers
  28. this.handleExceptions = true;
  29. this.transport = transport;
  30. }
  31. /**
  32. * Writes the info object to our transport instance if (and only if) the
  33. * `exception` property is set on the info.
  34. * @param {mixed} info - TODO: add param description.
  35. * @param {mixed} enc - TODO: add param description.
  36. * @param {mixed} callback - TODO: add param description.
  37. * @returns {mixed} - TODO: add return description.
  38. * @private
  39. */
  40. _write(info, enc, callback) {
  41. if (info.exception) {
  42. return this.transport.log(info, callback);
  43. }
  44. callback();
  45. return true;
  46. }
  47. };