Browse Source

Merge pull request #12955 from cakephp/improve-errors

4.x - Improve errors
Mark Story 7 years ago
parent
commit
bbe5e2d8b7
3 changed files with 28 additions and 15 deletions
  1. 15 7
      src/ORM/Association.php
  2. 10 5
      src/ORM/Table.php
  3. 3 3
      tests/TestCase/ORM/AssociationTest.php

+ 15 - 7
src/ORM/Association.php

@@ -252,7 +252,11 @@ abstract class Association
         if ($this->_targetTable !== null) {
             $alias = $this->_targetTable->getAlias();
             if ($alias !== $name) {
-                throw new InvalidArgumentException('Association name does not match target table alias.');
+                throw new InvalidArgumentException(sprintf(
+                    'Association name "%s" does not match target table alias "%s".',
+                    $name,
+                    $alias
+                ));
             }
         }
 
@@ -308,9 +312,11 @@ abstract class Association
         if ($this->_targetTable !== null &&
             get_class($this->_targetTable) !== App::className($className, 'Model/Table', 'Table')
         ) {
-            throw new InvalidArgumentException(
-                'The class name doesn\'t match the target table\'s class name.'
-            );
+            throw new InvalidArgumentException(sprintf(
+                'The class name "%s" doesn\'t match the target table class name of "%s".',
+                $className,
+                get_class($this->_targetTable)
+            ));
         }
 
         $this->_className = $className;
@@ -620,9 +626,11 @@ abstract class Association
     public function setStrategy(string $name)
     {
         if (!in_array($name, $this->_validStrategies)) {
-            throw new InvalidArgumentException(
-                sprintf('Invalid strategy "%s" was provided', $name)
-            );
+            throw new InvalidArgumentException(sprintf(
+                'Invalid strategy "%s" was provided. Valid options are (%s).',
+                $name,
+                implode(', ', $this->_validStrategies)
+            ));
         }
         $this->_strategy = $name;
 

+ 10 - 5
src/ORM/Table.php

@@ -1530,7 +1530,10 @@ class Table implements RepositoryInterface, EventListenerInterface, EventDispatc
         } elseif ($search instanceof Query) {
             $query = $search;
         } else {
-            throw new InvalidArgumentException('Search criteria must be an array, callable or Query');
+            throw new InvalidArgumentException(sprintf(
+                'Search criteria must be an array, callable or Query. Got "%s"',
+                getTypeName($search)
+            ));
         }
         $row = $query->first();
         if ($row !== null) {
@@ -2225,9 +2228,11 @@ class Table implements RepositoryInterface, EventListenerInterface, EventDispatc
             return $this->_behaviors->callFinder($type, [$query, $options]);
         }
 
-        throw new BadMethodCallException(
-            sprintf('Unknown finder method "%s"', $type)
-        );
+        throw new BadMethodCallException(sprintf(
+            'Unknown finder method "%s" on %s.',
+            $type,
+            static::class
+        ));
     }
 
     /**
@@ -2315,7 +2320,7 @@ class Table implements RepositoryInterface, EventListenerInterface, EventDispatc
         }
 
         throw new BadMethodCallException(
-            sprintf('Unknown method "%s"', $method)
+            sprintf('Unknown method "%s" called on %s', $method, static::class)
         );
     }
 

+ 3 - 3
tests/TestCase/ORM/AssociationTest.php

@@ -126,7 +126,7 @@ class AssociationTest extends TestCase
     public function testSetNameAfterTarget()
     {
         $this->expectException(\InvalidArgumentException::class);
-        $this->expectExceptionMessage('Association name does not match target table alias.');
+        $this->expectExceptionMessage('Association name "Bar" does not match target table alias');
         $this->association->getTarget();
         $this->association->setName('Bar');
     }
@@ -163,7 +163,7 @@ class AssociationTest extends TestCase
     public function testSetClassNameAfterTarget()
     {
         $this->expectException(\InvalidArgumentException::class);
-        $this->expectExceptionMessage('The class name doesn\'t match the target table\'s class name.');
+        $this->expectExceptionMessage('The class name "\TestApp\Model\Table\AuthorsTable" doesn\'t match the target table class name of');
         $this->association->getTarget();
         $this->association->setClassName('\TestApp\Model\Table\AuthorsTable');
     }
@@ -176,7 +176,7 @@ class AssociationTest extends TestCase
     public function testSetClassNameWithShortSyntaxAfterTarget()
     {
         $this->expectException(\InvalidArgumentException::class);
-        $this->expectExceptionMessage('The class name doesn\'t match the target table\'s class name.');
+        $this->expectExceptionMessage('The class name "Authors" doesn\'t match the target table class name of');
         $this->association->getTarget();
         $this->association->setClassName('Authors');
     }