Browse Source

Update error messages to be more helpful.

Name the missing column in constraint/index errors.
mark_story 12 years ago
parent
commit
a5690367a7
2 changed files with 15 additions and 4 deletions
  1. 14 4
      src/Database/Schema/Table.php
  2. 1 0
      tests/TestCase/Database/Schema/TableTest.php

+ 14 - 4
src/Database/Schema/Table.php

@@ -352,12 +352,17 @@ class Table {
 			throw new Exception(sprintf('Invalid index type "%s"', $attrs['type']));
 		}
 		if (empty($attrs['columns'])) {
-			throw new Exception('Indexes must define columns.');
+			throw new Exception('Indexes must have at least one column.');
 		}
 		$attrs['columns'] = (array)$attrs['columns'];
 		foreach ($attrs['columns'] as $field) {
 			if (empty($this->_columns[$field])) {
-				throw new Exception('Columns used in indexes must already exist.');
+				$msg = sprintf(
+					'Columns used in indexes must be added to the Table schema first. ' .
+					'The column "%s" was not found.',
+					$field
+				);
+				throw new Exception($msg);
 			}
 		}
 		$this->_indexes[$name] = $attrs;
@@ -432,12 +437,17 @@ class Table {
 			throw new Exception(sprintf('Invalid constraint type "%s"', $attrs['type']));
 		}
 		if (empty($attrs['columns'])) {
-			throw new Exception('Constraints must define columns.');
+			throw new Exception('Constraints must have at least one column.');
 		}
 		$attrs['columns'] = (array)$attrs['columns'];
 		foreach ($attrs['columns'] as $field) {
 			if (empty($this->_columns[$field])) {
-				throw new Exception('Columns used in constraints must already exist.');
+				$msg = sprintf(
+					'Columns used in constraints must be added to the Table schema first. ' .
+					'The column "%s" was not found.',
+					$field
+				);
+				throw new Exception($msg);
 			}
 		}
 		if ($attrs['type'] === static::CONSTRAINT_FOREIGN) {

+ 1 - 0
tests/TestCase/Database/Schema/TableTest.php

@@ -168,6 +168,7 @@ class TableTest extends TestCase {
 			[['columns' => 'author_id', 'type' => 'derp']],
 		];
 	}
+
 /**
  * Test that an exception is raised when constraints
  * are added for fields that do not exist.