-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add HttpErrorCodeInterface. #18110
Add HttpErrorCodeInterface. #18110
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
Using this interface allows exception classes to indicate that they use HTTP error codes and avoid having to maintain an error class to HTTP error code map in WebExceptionRenderer.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org) | ||
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) | ||
* | ||
* Licensed under The MIT License | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) | ||
* @since 5.2.0 | ||
* @license https://opensource.org/licenses/mit-license.php MIT License | ||
*/ | ||
namespace Cake\Core\Exception; | ||
|
||
use Throwable; | ||
|
||
interface HttpErrorCodeInterface extends Throwable | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,25 +18,20 @@ | |
|
||
use Cake\Controller\Controller; | ||
use Cake\Controller\ControllerFactory; | ||
use Cake\Controller\Exception\InvalidParameterException; | ||
use Cake\Controller\Exception\MissingActionException; | ||
use Cake\Core\App; | ||
use Cake\Core\Configure; | ||
use Cake\Core\Container; | ||
use Cake\Core\Exception\CakeException; | ||
use Cake\Core\Exception\HttpErrorCodeInterface; | ||
use Cake\Core\Exception\MissingPluginException; | ||
use Cake\Datasource\Exception\RecordNotFoundException; | ||
use Cake\Datasource\Paging\Exception\PageOutOfBoundsException; | ||
use Cake\Error\Debugger; | ||
use Cake\Error\ExceptionRendererInterface; | ||
use Cake\Http\Exception\HttpException; | ||
use Cake\Http\Exception\MissingControllerException; | ||
use Cake\Http\Response; | ||
use Cake\Http\ResponseEmitter; | ||
use Cake\Http\ServerRequest; | ||
use Cake\Http\ServerRequestFactory; | ||
use Cake\Log\Log; | ||
use Cake\Routing\Exception\MissingRouteException; | ||
use Cake\Routing\Router; | ||
use Cake\Utility\Inflector; | ||
use Cake\View\Exception\MissingLayoutException; | ||
|
@@ -45,6 +40,7 @@ | |
use Psr\Http\Message\ResponseInterface; | ||
use ReflectionMethod; | ||
use Throwable; | ||
use function Cake\Core\deprecationWarning; | ||
use function Cake\Core\h; | ||
use function Cake\Core\namespaceSplit; | ||
use function Cake\I18n\__d; | ||
|
@@ -110,21 +106,11 @@ class WebExceptionRenderer implements ExceptionRendererInterface | |
* This can be customized for users that don't want specific exceptions to throw 404 errors | ||
* or want their application exceptions to be automatically converted. | ||
* | ||
* @var array<string, int> | ||
* @psalm-var array<class-string<\Throwable>, int> | ||
* @var array<class-string<\Throwable>, int> | ||
* @deprecated 5.2.0 Exceptions returning HTTP error codes should extend | ||
* HttpErrorCodeInterface instead of using this array. | ||
*/ | ||
protected array $exceptionHttpCodes = [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could end up breaking userland exception rendering with this change. If we wanted to avoid the breakage, we could retain this as an empty list and also retain line 408 as a fallback for exceptions that don't implement There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, I'll add it back for BC. |
||
// Controller exceptions | ||
InvalidParameterException::class => 404, | ||
MissingActionException::class => 404, | ||
// Datasource exceptions | ||
PageOutOfBoundsException::class => 404, | ||
RecordNotFoundException::class => 404, | ||
// Http exceptions | ||
MissingControllerException::class => 404, | ||
// Routing exceptions | ||
MissingRouteException::class => 404, | ||
]; | ||
protected array $exceptionHttpCodes = []; | ||
|
||
/** | ||
* Creates the controller to perform rendering on the error response. | ||
|
@@ -401,11 +387,21 @@ protected function _template(Throwable $exception, string $method, int $code): s | |
*/ | ||
protected function getHttpCode(Throwable $exception): int | ||
{ | ||
if ($exception instanceof HttpException) { | ||
if ($exception instanceof HttpErrorCodeInterface) { | ||
return $exception->getCode(); | ||
} | ||
|
||
return $this->exceptionHttpCodes[$exception::class] ?? 500; | ||
if (isset($this->exceptionHttpCodes[$exception::class])) { | ||
deprecationWarning( | ||
'5.2.0', | ||
'Exceptions returning a HTTP error code should implement HttpErrorCodeInterface,' | ||
. ' instead of using the WebExceptionRenderer::$exceptionHttpCodes property.' | ||
); | ||
|
||
return $this->exceptionHttpCodes[$exception::class]; | ||
} | ||
|
||
return 500; | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally this should have been in the
Http
package but couple of exceptions of theDatasource
package are also mapped to 404 code :/There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is ok. Hard to create a generic map with one way dependencies with our split packages.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we get much from extending
Throwable
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not really, I did it as this interface is only meant to be used by exception classes.