errname.js 841 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. 'use strict';
  2. // Older verions of Node.js might not have `util.getSystemErrorName()`.
  3. // In that case, fall back to a deprecated internal.
  4. const util = require('util');
  5. let uv;
  6. if (typeof util.getSystemErrorName === 'function') {
  7. module.exports = util.getSystemErrorName;
  8. } else {
  9. try {
  10. uv = process.binding('uv');
  11. if (typeof uv.errname !== 'function') {
  12. throw new TypeError('uv.errname is not a function');
  13. }
  14. } catch (err) {
  15. console.error('execa/lib/errname: unable to establish process.binding(\'uv\')', err);
  16. uv = null;
  17. }
  18. module.exports = code => errname(uv, code);
  19. }
  20. // Used for testing the fallback behavior
  21. module.exports.__test__ = errname;
  22. function errname(uv, code) {
  23. if (uv) {
  24. return uv.errname(code);
  25. }
  26. if (!(code < 0)) {
  27. throw new Error('err >= 0');
  28. }
  29. return `Unknown system error ${code}`;
  30. }