[Groonga-commit] groonga/gcs [master] Throw multiplexed errors for validation error of domain names

Back to archive index

YUKI Hiroshi null+****@clear*****
Mon Nov 5 14:47:20 JST 2012


YUKI Hiroshi	2012-11-05 14:47:20 +0900 (Mon, 05 Nov 2012)

  New Revision: 9974e45ece1d07630469dc7ca090cd486b344e02
  https://github.com/groonga/gcs/commit/9974e45ece1d07630469dc7ca090cd486b344e02

  Log:
    Throw multiplexed errors for validation error of domain names

  Modified files:
    lib/database/domain.js
    lib/logger.js

  Modified: lib/database/domain.js (+29 -14)
===================================================================
--- lib/database/domain.js    2012-11-05 13:36:03 +0900 (8f5ebab)
+++ lib/database/domain.js    2012-11-05 14:47:20 +0900 (eaee30a)
@@ -12,7 +12,7 @@ var IndexField = require('./index-field').IndexField;
 
 exports.MINIMUM_NAME_LENGTH = 3;
 exports.MAXIMUM_NAME_LENGTH = 28;
-exports.INVALID_NAME_CHARACTER_PATTERN = /[^\-a-z0-9]/g;
+exports.VALID_NAME_PATTERN = /[a-z][a-z0-9\-]+/;
 exports.INVALID_TABLE_NAME_CHARACTER_PATTERN = /[^_a-z0-9]/g;
 
 var DEFAULT_ID =
@@ -45,24 +45,39 @@ function assertValidDomainName(domain) {
   if (typeof domain != 'string')
     throw new Error('domain name must be a string');
 
+  var errors = [];
+
+  var commonPrefix = 'Value \'' + domain + '\' at \'domainName\' failed ' +
+                     'to satisfy constraint: ';
+
+  if (!domain.match(exports.VALID_NAME_PATTERN)) {
+    errors.push(new Error(commonPrefix + 'Member must satisfy regular ' +
+                          expression pattern: ' + exports.VALID_NAME_PATTERN);
+  } else {
+    var invalidCharacters = domain.match(exports.INVALID_TABLE_NAME_CHARACTER_PATTERN);
+    if (invalidCharacters) {
+      invalidCharacters = Array.prototype.slice.call(invalidCharacters, 1)
+                            .map(function(aCharacter) {
+                              return "'" + aCharacter + "'";
+                            });
+      errors.push(new Error(commonPrefix + 'Member cannot include these ' +
+                            'characters: ' + invalidCharacters.join(', '));
+    }
+  }
+
   if (domain.length < exports.MINIMUM_NAME_LENGTH)
-    throw new Error('too short domain name (minimum length = ' +
-                    exports.MINIMUM_NAME_LENGTH + ')');
+    errors.push(new Error(commonPrefix + 'Member must have length greater ' +
+                          'than or equal to ' + exports.MINIMUM_NAME_LENGTH);
 
   if (domain.length > exports.MAXIMUM_NAME_LENGTH)
-    throw new Error('too long domain name (max length = ' +
-                   exports.MAXIMUM_NAME_LENGTH + ')');
-
-  var invalidCharacter = domain.match(exports.INVALID_NAME_CHARACTER_PATTERN) ||
-                         domain.match(exports.INVALID_TABLE_NAME_CHARACTER_PATTERN);
-  if (invalidCharacter) {
-    var characters = Array.prototype.map.call(invalidCharacter, function(aCharacter) {
-                       return '"' + aCharacter + '"';
-                     });
-    throw new Error(characters.join(', ') + ' cannot appear in a domain name');
-  }
+    errors.push(new Error(commonPrefix + 'Member must have length smaller ' +
+                          'than or equal to ' + exports.MAXIMUM_NAME_LENGTH);
+
+  if (errors.length)
+    throw errors;
 }
 
+
 function toValidHostAndPort(hostAndPort) {
   hostAndPort = hostAndPort.split(':');
   if (hostAndPort[0] == 'localhost')

  Modified: lib/logger.js (+9 -6)
===================================================================
--- lib/logger.js    2012-11-05 13:36:03 +0900 (ee8da74)
+++ lib/logger.js    2012-11-05 14:47:20 +0900 (d988812)
@@ -18,12 +18,15 @@ function getLogger(context) {
   return loggers[context] = logger;
 }
 
-function logError(error) {
-  var message = error;
-  if (error && error.stack) {
-    message = error + '\n' + error.stack;
-  }
-  getLogger('error').error(message);
+function logError(errors) {
+  if (!Array.isArray(errors))
+    errors = [errors];
+  errors.forEach(function(error) {
+    if (error.stack) {
+      error = error + '\n' + error.stack;
+    }
+    getLogger('error').error(error);
+  });
 }
 exports.error = logError;
 
-------------- next part --------------
HTML����������������������������...
下載 



More information about the Groonga-commit mailing list
Back to archive index