l0bsterssg

node.js static responsive blog post generator
Log | Files | Refs | README

error.js (1183B)


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