cakephp: ExceptionRenderer doesn't handle custom status code as expected
(The original issue was reported from @nojimage)
This is a (multiple allowed):
- bug
- enhancement
- feature-discussion (RFC)
- CakePHP Version: 3.2.10 / 2.8.4
- Platform and Target: non-related
What you did
Throw an exception with a custom error code.
$this->response->httpCodes([
509 => 'Bandwidth Limit Exceeded',
]);
throw new HttpException('We are sorry', 509);
Expected Behavior
Get an error page with http code 509.
Actual Behavior
Get an error page with http code 500. Because ExceptionRenderer only allows HTTP codes within range 400 to 506:
$errorCode = $exception->getCode();
if ($errorCode >= 400 && $errorCode < 506) {
$code = $errorCode;
}
return $code;
But I think it would be better to allow any status code if a caught exception is an instance of HttpException.
Solution
Change the above to the following:
$errorCode = $exception->getCode();
if ( $errorCode >= 400 && $errorCode < 506
|| $exception instanceof HttpException && $this->controller->response->httpCodes($errorCode)
) {
$code = $errorCode;
}
return $code;
Do you think this change is backward compatible?
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 2
- Comments: 17 (17 by maintainers)
Commits related to this issue
- Allow using any error code in range 4xx-5xx as HTTP status code. Closes #8962. — committed to cakephp/cakephp by ADmad 6 years ago
@dereuromark Sorry for my late reply. I totally forgot about this issue. Since cakephp/app#432 has been already merged, the original approach could work. I will re-think this issue when I have time.
Why can’t we do that now?
@ADmad Thank you for your comment.
See #6094. To be honest, I want to remove the
$errorCode >= 400 && $errorCode < 506check in the future release.Aside from that,
Responseclass only allows status codes defined in the_statusCodesproperty. So we would need some way to register custom http codes into theResponseobject created in theErrorRenderer.@markstory After implementing this feature, I noticed another issue. Even if I registered a custom http code into the
Responseclass via thehttpCodes()method, they were not available in theExceptionRenderer, because after an exception was thrown, theExceptionRendererwould instantiate a differentResponseobject here. SinceErrorControlleris not a subclass ofAppController, I couldn’t register any custom http codes into the response object of theErrorControllerby handling controller events. A workaround would be to create myAppErrorController. But it’s annoying little bit.So what do you think about adding a new configuration to allow users to register custom http codes? The configuration and implementation would be like the following code: