Problem/Motivation

The constructor of the class \Drupal\jsonrpc\JsonRpcObject\Error has a parameter that is documented as optional:

   *  @param mixed $data
   *     (optional) A primitive or structured value that contains additional
   *     information about the error. This may be omitted.

But if it is actually omitted then the data property of the error object won't be initialized at all,
which leads to a crash when the data is accessed, as is done by the getData() method:

Error: Typed property Drupal\jsonrpc\JsonRpcObject\Error::$data must not be accessed before initialization in
Drupal\jsonrpc\JsonRpcObject\Error->getData() (line 126 of modules/contrib/jsonrpc/src/JsonRpcObject/Error.php).
Drupal\jsonrpc\Shaper\RpcResponseFactory->doNormalize() (Line: 79)
Drupal\jsonrpc\Shaper\RpcResponseFactory->closure:Drupal\jsonrpc\Shaper\RpcResponseFactory::doTransform():77}()

I'm using PHP 8.4.

Steps to reproduce

Throw a JsonRpcException with an error object that leaves out the optional parameter:

$id = 'whatever';
$jsonrpc_code = -10;
$message = 'hello world';
$error = new Error($jsonrpc_code, $message));
throw JsonRpcException::fromError($error, $id)

Proposed resolution

Remove the "if" in the constructor. Then the property will be set to NULL if left out.
This would currently lead to a NULL value for "data" in the error response, though.

Issue fork jsonrpc-3567234

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

cspitzlay created an issue. See original summary.

ptmkenny made their first commit to this issue’s fork.

ptmkenny’s picture

Title: Crash due to access to an uninitialized property » Error object: crash due to access to an uninitialized property
Version: 3.0.1 » 3.x-dev

Thank you for the detailed report. Let's try your suggestion against the tests.

ptmkenny’s picture

Status: Active » Needs review
cspitzlay’s picture

The tests are green.

ptmkenny’s picture

Status: Needs review » Fixed

Yep, I will go ahead and commit this! Thanks for the report and the fix!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

  • ptmkenny committed 4917a438 on 3.x
    fix: #3567234 Error object: crash due to access to an uninitialized...
cspitzlay’s picture

Thanks.

Status: Fixed » Closed (fixed)

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