Browse Source

Merge pull request #11057 from cakephp/paginator

Passing paging params to PageOutOfBoundException.
Mark Story 8 years ago
parent
commit
fb3e845b06

+ 2 - 2
src/Datasource/Exception/PageOutOfBoundsException.php

@@ -22,12 +22,12 @@ class PageOutOfBoundsException extends Exception
     /**
      * {@inheritDoc}
      */
-    protected $_messageTemplate = 'Page number "%s" could not be found.';
+    protected $_messageTemplate = 'Page number %s could not be found.';
 
     /**
      * Constructor
      *
-     * @param string|null $message The error message.
+     * @param array|null $message Paging info.
      * @param int $code The code of the error, is also the HTTP status code for the error.
      * @param \Exception|null $previous The previous exception.
      */

+ 4 - 1
src/Datasource/Paginator.php

@@ -220,7 +220,10 @@ class Paginator implements PaginatorInterface
         $this->_pagingParams = [$alias => $paging];
 
         if ($requestedPage > $page) {
-            throw new PageOutOfBoundsException([$requestedPage]);
+            throw new PageOutOfBoundsException([
+                'requestedPage' => $requestedPage,
+                'pagingParams' => $this->_pagingParams
+            ]);
         }
 
         return $results;

+ 11 - 5
tests/TestCase/Datasource/PaginatorTest.php

@@ -643,12 +643,18 @@ class PaginatorTest extends TestCase
         try {
             $this->Paginator->paginate($table, $params);
             $this->fail('No exception raised');
-        } catch (PageOutOfBoundsException $e) {
-            $pagingParams = $this->Paginator->getPagingParams();
+        } catch (PageOutOfBoundsException $exception) {
             $this->assertEquals(
-                1,
-                $pagingParams['PaginatorPosts']['page'],
-                'Page number should not be 0'
+                'Page number 3000 could not be found.',
+                $exception->getMessage()
+            );
+
+            $this->assertSame(
+                [
+                    'requestedPage' => 3000,
+                    'pagingParams' => $this->Paginator->getPagingParams()
+                ],
+                $exception->getAttributes()
             );
         }
     }