No Description

error.js 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. 'use strict';
  2. const {signalsByName} = require('human-signals');
  3. const getErrorPrefix = ({timedOut, timeout, errorCode, signal, signalDescription, exitCode, isCanceled}) => {
  4. if (timedOut) {
  5. return `timed out after ${timeout} milliseconds`;
  6. }
  7. if (isCanceled) {
  8. return 'was canceled';
  9. }
  10. if (errorCode !== undefined) {
  11. return `failed with ${errorCode}`;
  12. }
  13. if (signal !== undefined) {
  14. return `was killed with ${signal} (${signalDescription})`;
  15. }
  16. if (exitCode !== undefined) {
  17. return `failed with exit code ${exitCode}`;
  18. }
  19. return 'failed';
  20. };
  21. const makeError = ({
  22. stdout,
  23. stderr,
  24. all,
  25. error,
  26. signal,
  27. exitCode,
  28. command,
  29. timedOut,
  30. isCanceled,
  31. killed,
  32. parsed: {options: {timeout}}
  33. }) => {
  34. // `signal` and `exitCode` emitted on `spawned.on('exit')` event can be `null`.
  35. // We normalize them to `undefined`
  36. exitCode = exitCode === null ? undefined : exitCode;
  37. signal = signal === null ? undefined : signal;
  38. const signalDescription = signal === undefined ? undefined : signalsByName[signal].description;
  39. const errorCode = error && error.code;
  40. const prefix = getErrorPrefix({timedOut, timeout, errorCode, signal, signalDescription, exitCode, isCanceled});
  41. const execaMessage = `Command ${prefix}: ${command}`;
  42. const isError = Object.prototype.toString.call(error) === '[object Error]';
  43. const shortMessage = isError ? `${execaMessage}\n${error.message}` : execaMessage;
  44. const message = [shortMessage, stderr, stdout].filter(Boolean).join('\n');
  45. if (isError) {
  46. error.originalMessage = error.message;
  47. error.message = message;
  48. } else {
  49. error = new Error(message);
  50. }
  51. error.shortMessage = shortMessage;
  52. error.command = command;
  53. error.exitCode = exitCode;
  54. error.signal = signal;
  55. error.signalDescription = signalDescription;
  56. error.stdout = stdout;
  57. error.stderr = stderr;
  58. if (all !== undefined) {
  59. error.all = all;
  60. }
  61. if ('bufferedData' in error) {
  62. delete error.bufferedData;
  63. }
  64. error.failed = true;
  65. error.timedOut = Boolean(timedOut);
  66. error.isCanceled = isCanceled;
  67. error.killed = killed && !timedOut;
  68. return error;
  69. };
  70. module.exports = makeError;