You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.2 KiB

4 years ago
  1. 'use strict';
  2. var format = require('util').format;
  3. var ERR_CODE = 'ARGError';
  4. /*:nodoc:*
  5. * argumentError(argument, message) -> TypeError
  6. * - argument (Object): action with broken argument
  7. * - message (String): error message
  8. *
  9. * Error format helper. An error from creating or using an argument
  10. * (optional or positional). The string value of this exception
  11. * is the message, augmented with information
  12. * about the argument that caused it.
  13. *
  14. * #####Example
  15. *
  16. * var argumentErrorHelper = require('./argument/error');
  17. * if (conflictOptionals.length > 0) {
  18. * throw argumentErrorHelper(
  19. * action,
  20. * format('Conflicting option string(s): %s', conflictOptionals.join(', '))
  21. * );
  22. * }
  23. *
  24. **/
  25. module.exports = function (argument, message) {
  26. var argumentName = null;
  27. var errMessage;
  28. var err;
  29. if (argument.getName) {
  30. argumentName = argument.getName();
  31. } else {
  32. argumentName = '' + argument;
  33. }
  34. if (!argumentName) {
  35. errMessage = message;
  36. } else {
  37. errMessage = format('argument "%s": %s', argumentName, message);
  38. }
  39. err = new TypeError(errMessage);
  40. err.code = ERR_CODE;
  41. return err;
  42. };