ソースを参照

Add InvalidPrimaryKeyException

Used with findOrFail when passed an invalid pk
AD7six 11 年 前
コミット
526bcae69f

+ 36 - 0
src/Datasource/Exception/InvalidPrimaryKeyException.php

@@ -0,0 +1,36 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link          http://cakephp.org CakePHP(tm) Project
+ * @since         3.0.0
+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\Datasource\Exception;
+
+use RuntimeException;
+
+/**
+ * Exception raised when a particular record was not found
+ *
+ */
+class InvalidPrimaryKeyException extends RuntimeException {
+
+/**
+ * Constructor.
+ *
+ * @param string $message The error message
+ * @param int $code The code of the error, is also the HTTP status code for the error.
+ * @param \Exception|null $previous the previous exception.
+ */
+	public function __construct($message, $code = 404, $previous = null) {
+		parent::__construct($message, $code, $previous);
+	}
+
+}

+ 4 - 4
src/ORM/Table.php

@@ -19,7 +19,7 @@ use Cake\Core\App;
 use Cake\Database\Schema\Table as Schema;
 use Cake\Database\Type;
 use Cake\Datasource\EntityInterface;
-use Cake\Datasource\Exception\RecordNotFoundException;
+use Cake\Datasource\Exception\InvalidPrimaryKeyException;
 use Cake\Datasource\RepositoryInterface;
 use Cake\Event\EventListenerInterface;
 use Cake\Event\EventManager;
@@ -945,7 +945,7 @@ class Table implements RepositoryInterface, EventListenerInterface {
 /**
  * {@inheritDoc}
  *
- * @throws Cake\Datasource\Exception\RecordNotFoundException When $primaryKey has an
+ * @throws Cake\Datasource\Exception\InvalidPrimaryKeyException When $primaryKey has an
  * 		incorrect number of elements.
  */
 	public function get($primaryKey, $options = []) {
@@ -961,8 +961,8 @@ class Table implements RepositoryInterface, EventListenerInterface {
 				return var_export($key, true);
 			}, $primaryKey);
 
-			throw new RecordNotFoundException(sprintf(
-				'Invalid primary key, record not found in table "%s" with primary key [%s]',
+			throw new InvalidPrimaryKeyException(sprintf(
+				'Record not found in table "%s" with primary key [%s]',
 				$this->table(),
 				implode($primaryKey, ', ')
 			));

+ 4 - 4
tests/TestCase/ORM/TableTest.php

@@ -3534,8 +3534,8 @@ class TableTest extends TestCase {
 /**
  * Test that an exception is raised when there are not enough keys.
  *
- * @expectedException Cake\Datasource\Exception\RecordNotFoundException
- * @expectedExceptionMessage Invalid primary key, record not found in table "articles" with primary key [NULL]
+ * @expectedException Cake\Datasource\Exception\InvalidPrimaryKeyException
+ * @expectedExceptionMessage Record not found in table "articles" with primary key [NULL]
  * @return void
  */
 	public function testGetExceptionOnNoData() {
@@ -3550,8 +3550,8 @@ class TableTest extends TestCase {
 /**
  * Test that an exception is raised when there are too many keys.
  *
- * @expectedException Cake\Datasource\Exception\RecordNotFoundException
- * @expectedExceptionMessage Invalid primary key, record not found in table "articles" with primary key [1, 'two']
+ * @expectedException Cake\Datasource\Exception\InvalidPrimaryKeyException
+ * @expectedExceptionMessage Record not found in table "articles" with primary key [1, 'two']
  * @return void
  */
 	public function testGetExceptionOnTooMuchData() {