Problem/Motivation

In PHP 8.1 exceptions must use integers in the $code parameter, NULL is no longer allowed.

All our FileTransferExceptions currently pass NULL as this argument.

Steps to reproduce

Proposed resolution

Replace NULL with 0 in throw new FileTransferException().

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Comments

longwave created an issue. See original summary.

longwave’s picture

Component: base system » file system
Status: Active » Needs review
StatusFileSize
new12.07 KB
daffie’s picture

Status: Needs review » Needs work
  1. Should we add a deprecation warning that the exception code should be an integer and add a test for that.
  2. Is this the only exception with this problem? Or do we need to do all by core added exceptions?
andypost’s picture

Status: Needs work » Needs review
StatusFileSize
new1.02 KB
new13.1 KB

Using git grep -E 'throw new \w+Exception.*NULL' I found 2 more places

Re #3 I see no way to add deprecation message because all exceptions are inherited from \Exception which declared as public function __construct($message = "", $code = 0, Throwable $previous = null) {} I found few places where it defined as int $code = 0 and few places in SF ?int $code = 0 so probably only static analysis may help

daffie’s picture

Status: Needs review » Needs work

@andypost: Thank you for the git grep command. Learned something new.

When I use the command git grep -E 'throw new \w+Exception.*NULL' after applying the patch, I get a number of results.
Most of them are fake positives, only 2 are not:
- core/modules/jsonapi/src/Revisions/ResourceVersionRouteEnhancer.php: throw new CacheableHttpException($cacheability, 501, $message, NULL, []);
- core/modules/jsonapi/src/Revisions/ResourceVersionRouteEnhancer.php: throw new CacheableHttpException($cacheability, 501, $message, NULL, []);
The last parameter is the one for the variable $code. Only when calling the parent exception the value of code is put into the variable $headers. The parent is vendor/symfony/http-kernel/Exception/HttpException.php. Not sure if we want to fix it in this issue.

All other changes are for me RTBC.

longwave’s picture

longwave’s picture

Status: Needs work » Needs review
daffie’s picture

Status: Needs review » Reviewed & tested by the community

All changes look good to me.
I could not find any other instances with this problem.
For me it is RTBC.

@longwave: Thanks for the reply.

andypost’s picture

andypost’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new2.34 KB
new15.44 KB

I think it still makes sense to fix broken arguments and decide about headers in related issue because @alexpott suggests to remove it in #3002352-29: CacheableHttpException must pass a $headers argument to HttpException

IMO the fix perfectly fits into scope of this issue

daffie’s picture

Status: Needs review » Needs work
  1. +++ b/core/lib/Drupal/Core/Http/Exception/CacheableHttpException.php
    @@ -18,7 +18,7 @@ class CacheableHttpException extends HttpException implements CacheableDependenc
    -    parent::__construct($statusCode, $message, $previous, $code);
    +    parent::__construct($statusCode, $message, $previous, [], $code);
    
    +++ b/core/modules/jsonapi/src/Revisions/ResourceVersionRouteEnhancer.php
    @@ -105,7 +105,7 @@ public function enhance(array $defaults, Request $request) {
    -        throw new CacheableHttpException($cacheability, 501, $message, NULL, []);
    

    I do not think we can do this change. It now works when you do: throw new CacheableHttpException($cacheability, 501, $message, NULL, []);. With this change it will be a BC break.

  2. +++ b/core/modules/jsonapi/src/Revisions/ResourceVersionRouteEnhancer.php
    @@ -105,7 +105,7 @@ public function enhance(array $defaults, Request $request) {
    -        throw new CacheableHttpException($cacheability, 501, $message, NULL, []);
    +        throw new CacheableHttpException($cacheability, 501, $message, NULL, 0);
    
    @@ -151,7 +151,7 @@ public function enhance(array $defaults, Request $request) {
    -        throw new CacheableHttpException($cacheability, 501, $message, NULL, []);
    +        throw new CacheableHttpException($cacheability, 501, $message, NULL, 0);
    

    Could we change this to: throw new CacheableHttpException($cacheability, 501, $message, NULL); or maybe even to: throw new CacheableHttpException($cacheability, 501, $message);. We do not need to add the default parameter values.

longwave’s picture

#11.1 can we do something like

if (is_array($code)) {
  parent::__construct($statusCode, $message, $previous, $code);
}
else {
  parent::__construct($statusCode, $message, $previous, [], $code);
}

However the only uses in core are touched here and the only use in contrib is jsonapi: http://grep.xnddx.ru/search?text=CacheableHttpException&filename=

daffie’s picture

The solution from #12 looks good to me. Can we also add a comment with why we have this solution.

andypost’s picture

Status: Needs work » Needs review
StatusFileSize
new964 bytes
new15.64 KB

Added workaround with todo but I'm sure as there's no usage in contrib and jsonapi is "fresh" it needs no reason

daffie’s picture

Status: Needs review » Reviewed & tested by the community

It looks good to me now.
Back to RTBC.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 65fb382 and pushed to 9.3.x. Thanks!

  • alexpott committed 65fb382 on 9.3.x
    Issue #3224420 by andypost, longwave, daffie: [PHP 8.1] Exception codes...
mondrake’s picture

Issue tags: +PHP 8.1

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.