Skip to content
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

fix: add optional chaining operator in toHuman #5825

Merged
merged 2 commits into from
Mar 18, 2024

Conversation

Imod7
Copy link
Contributor

@Imod7 Imod7 commented Mar 18, 2024

Description

Fixes #10339 in apps

Issue

In apps, when rpc syncState_getSyncSpec is selected/called, it calls valueToText which then calls toHuman where it iterates through all the entries of the json result of the call

{
  'badBlocks' => Array(2), 
  'bootNodes' => Array(34), 
  'chainType' => 'Live', 
  'codeSubstitutes' => {…}, 
  'forkBlocks' => null, …}
...

and when it arrives at forkBlocks the value is null and that's where it breaks.

Proposed Solution

If the optional chaining (?.) operator is added here, the toHuman is accessed only if value is not null or undefined. If value is null or undefined (like in forkBlocks), the expression will return null at the corresponding entry (forkBlocks) instead of throwing an error.

Example Output

Example output (after making the change) when making this rpc call in Kusama:

{ 
badBlocks: [ 0x15b1b925b0aa5cfe43c88cd024f74258cb5cfe3af424882c901014e8acd0d241 0x2563260209012232649ab9dc003f62e274c684037de499a23062f8e0e816c605 ] 
bootNodes: [ /dns/kusama-bootnode-0.polkadot.io/tcp/30333/p2p/12D3KooWSueCPH3puP2PcvqPJdNaDNF3jMZjtJtDiSy35pWrbt5h /dns/kusama-bootnode-0.polkadot.io/...kusama.luckyfriday.io/tcp/30334/wss/p2p/12D3KooW9vu1GWHBuxyhm7rZgD3fhGZpNajPXFexadvhujWMgwfT ] 
chainType: Live 
codeSubstitutes: {} 
forkBlocks: null 
genesis: { raw: { childrenDefault: {} top: { 0x0b76934f4cc08dee0...4e4aaf9d53a9627400f9a948bb5f355bda38702dbdeda0c5d3455336c 0x1a736d37504c2e3fb

Tested

This change was tested in Chrome devtools by changing

json[key] = (0,_polkadot_util__WEBPACK_IMPORTED_MODULE_2__.isFunction)(value.toHuman)

to

json[key] = (0,_polkadot_util__WEBPACK_IMPORTED_MODULE_2__.isFunction)(value?.toHuman)

@TarikGul TarikGul self-requested a review March 18, 2024 15:05
Copy link
Member

@TarikGul TarikGul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome catch and fix. LGTM - Thank you

@TarikGul TarikGul merged commit 1a06778 into polkadot-js:master Mar 18, 2024
4 checks passed
@polkadot-js-bot
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@polkadot-js polkadot-js locked as resolved and limited conversation to collaborators Mar 20, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

syncstate.genSyncSpec throw "Cannot read properties of null (reading 'toHuman')" error
3 participants