|
|
@@ -18,12 +18,17 @@ namespace Cake\Error;
|
|
|
|
|
|
use Cake\Controller\Controller;
|
|
|
use Cake\Controller\ControllerFactory;
|
|
|
+use Cake\Controller\Exception\MissingActionException;
|
|
|
use Cake\Core\App;
|
|
|
use Cake\Core\Configure;
|
|
|
use Cake\Core\Exception\Exception as CakeException;
|
|
|
use Cake\Core\Exception\MissingPluginException;
|
|
|
+use Cake\Datasource\Exception\InvalidPrimaryKeyException;
|
|
|
+use Cake\Datasource\Exception\PageOutOfBoundsException;
|
|
|
+use Cake\Datasource\Exception\RecordNotFoundException;
|
|
|
use Cake\Event\Event;
|
|
|
use Cake\Http\Exception\HttpException;
|
|
|
+use Cake\Http\Exception\MissingControllerException;
|
|
|
use Cake\Http\Response;
|
|
|
use Cake\Http\ServerRequest;
|
|
|
use Cake\Http\ServerRequestFactory;
|
|
|
@@ -90,6 +95,17 @@ class ExceptionRenderer implements ExceptionRendererInterface
|
|
|
*/
|
|
|
protected $request;
|
|
|
|
|
|
+ protected $exceptionHttpCodes = [
|
|
|
+ // Controller exceptions
|
|
|
+ MissingActionException::class => 404,
|
|
|
+ // Datasource exceptions
|
|
|
+ InvalidPrimaryKeyException::class => 404,
|
|
|
+ PageOutOfBoundsException::class => 404,
|
|
|
+ RecordNotFoundException::class => 404,
|
|
|
+ // Http exceptions
|
|
|
+ MissingControllerException::class => 404,
|
|
|
+ ];
|
|
|
+
|
|
|
/**
|
|
|
* Creates the controller to perform rendering on the error response.
|
|
|
* If the error is a Cake\Core\Exception\Exception it will be converted to either a 400 or a 500
|
|
|
@@ -192,7 +208,7 @@ class ExceptionRenderer implements ExceptionRendererInterface
|
|
|
public function render(): ResponseInterface
|
|
|
{
|
|
|
$exception = $this->error;
|
|
|
- $code = $this->_code($exception);
|
|
|
+ $code = $this->getHttpCode($exception);
|
|
|
$method = $this->_method($exception);
|
|
|
$template = $this->_template($exception, $method, $code);
|
|
|
$this->clearOutput();
|
|
|
@@ -341,20 +357,18 @@ class ExceptionRenderer implements ExceptionRendererInterface
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Get HTTP status code.
|
|
|
+ * Gets the appropriate http status code for exception.
|
|
|
*
|
|
|
* @param \Throwable $exception Exception.
|
|
|
- * @return int A valid HTTP error status code.
|
|
|
+ * @return int A valid HTTP status code.
|
|
|
*/
|
|
|
- protected function _code(Throwable $exception): int
|
|
|
+ protected function getHttpCode(Throwable $exception): int
|
|
|
{
|
|
|
- $code = 500;
|
|
|
- $errorCode = (int)$exception->getCode();
|
|
|
- if ($errorCode >= 400 && $errorCode < 600) {
|
|
|
- $code = $errorCode;
|
|
|
+ if ($exception instanceof HttpException) {
|
|
|
+ return (int)$exception->getCode();
|
|
|
}
|
|
|
|
|
|
- return $code;
|
|
|
+ return $this->exceptionHttpCodes[get_class($exception)] ?? 500;
|
|
|
}
|
|
|
|
|
|
/**
|