Browse Source

Merge pull request #12501 from cakephp/3.7-exception-renderer-error-code

Allow using any error code in range 4xx-5xx as HTTP status code.
Mark Story 7 years ago
parent
commit
5b8bf0904f
2 changed files with 11 additions and 4 deletions
  1. 4 3
      src/Error/ExceptionRenderer.php
  2. 7 1
      tests/TestCase/Error/ExceptionRendererTest.php

+ 4 - 3
src/Error/ExceptionRenderer.php

@@ -331,17 +331,18 @@ class ExceptionRenderer implements ExceptionRendererInterface
     }
 
     /**
-     * Get an error code value within range 400 to 506
+     * Get HTTP status code.
      *
      * @param \Exception $exception Exception.
-     * @return int Error code value within range 400 to 506
+     * @return int A valid HTTP error status code.
      */
     protected function _code(Exception $exception)
     {
         $code = 500;
+
         $exception = $this->_unwrap($exception);
         $errorCode = $exception->getCode();
-        if ($errorCode >= 400 && $errorCode < 506) {
+        if ($errorCode >= 400 && $errorCode < 600) {
             $code = $errorCode;
         }
 

+ 7 - 1
tests/TestCase/Error/ExceptionRendererTest.php

@@ -27,6 +27,7 @@ use Cake\Datasource\Exception\MissingDatasourceException;
 use Cake\Error\ExceptionRenderer;
 use Cake\Event\Event;
 use Cake\Event\EventManager;
+use Cake\Http\Exception\HttpException;
 use Cake\Http\Exception\InternalErrorException;
 use Cake\Http\Exception\MethodNotAllowedException;
 use Cake\Http\Exception\NotFoundException;
@@ -694,7 +695,12 @@ class ExceptionRendererTest extends TestCase
                 new CakeException('base class'),
                 ['/Internal Error/'],
                 500
-            ]
+            ],
+            [
+                new HttpException('Network Authentication Required', 511),
+                ['/Network Authentication Required/'],
+                511
+            ],
         ];
     }