throw.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import {makeEarlyError} from '../return/result.js';
  2. import {abortSourceStream, endDestinationStream} from '../io/pipeline.js';
  3. // When passing invalid arguments to `source.pipe()`, throw asynchronously.
  4. // We also abort both subprocesses.
  5. export const handlePipeArgumentsError = ({
  6. sourceStream,
  7. sourceError,
  8. destinationStream,
  9. destinationError,
  10. fileDescriptors,
  11. sourceOptions,
  12. startTime,
  13. }) => {
  14. const error = getPipeArgumentsError({
  15. sourceStream,
  16. sourceError,
  17. destinationStream,
  18. destinationError,
  19. });
  20. if (error !== undefined) {
  21. throw createNonCommandError({
  22. error,
  23. fileDescriptors,
  24. sourceOptions,
  25. startTime,
  26. });
  27. }
  28. };
  29. const getPipeArgumentsError = ({sourceStream, sourceError, destinationStream, destinationError}) => {
  30. if (sourceError !== undefined && destinationError !== undefined) {
  31. return destinationError;
  32. }
  33. if (destinationError !== undefined) {
  34. abortSourceStream(sourceStream);
  35. return destinationError;
  36. }
  37. if (sourceError !== undefined) {
  38. endDestinationStream(destinationStream);
  39. return sourceError;
  40. }
  41. };
  42. // Specific error return value when passing invalid arguments to `subprocess.pipe()` or when using `unpipeSignal`
  43. export const createNonCommandError = ({error, fileDescriptors, sourceOptions, startTime}) => makeEarlyError({
  44. error,
  45. command: PIPE_COMMAND_MESSAGE,
  46. escapedCommand: PIPE_COMMAND_MESSAGE,
  47. fileDescriptors,
  48. options: sourceOptions,
  49. startTime,
  50. isSync: false,
  51. });
  52. const PIPE_COMMAND_MESSAGE = 'source.pipe(destination)';