Skip to content

fix(app): replace panic with error returns in InitChainer - #349

Open
g0spel wants to merge 3 commits into
KiiChain:mainfrom
g0spel:fix/init-chainer-error-returns
Open

fix(app): replace panic with error returns in InitChainer#349
g0spel wants to merge 3 commits into
KiiChain:mainfrom
g0spel:fix/init-chainer-error-returns

Conversation

@g0spel

@g0spel g0spel commented Jul 5, 2026

Copy link
Copy Markdown

Description

Fixes #264 — InitChainer function uses panic() instead of error returns.

The InitChainer() function in app/app.go had three panic() calls that
crash the node on initialization errors, violating the function's
(*abci.ResponseInitChain, error) return contract.

Changes

Three panics converted to proper error returns with wrapped error messages:

  1. JSON unmarshal failure: panic(err)return nil, fmt.Errorf("failed to unmarshal genesis state: %w", err)
  2. SetModuleVersionMap failure: panic(err)return nil, fmt.Errorf("failed to set module version map: %w", err)
  3. InitGenesis failure: panic(err)return nil, fmt.Errorf("failed to run InitGenesis: %w", err)

Impact

Before After
Invalid genesis JSON → node crash (panic) Invalid genesis JSON → error returned to CometBFT
Upgrade module errors → node crash Errors properly propagated up the call chain
Manual restart required Clean error handling, node can recover

Testing

  • fmt is already imported in app.go
  • Function signature unchanged — passes all existing type checks
  • Error wrapping follows Go best practices (%w for unwrapping)

Closes: #264

Fixes KiiChain#264. The InitChainer function used panic() on initialization
errors, crashing the node. Replaced with proper error returns that
match the function's (*abci.ResponseInitChain, error) signature.

Three panics converted to fmt.Errorf wraps:
- tmjson.Unmarshal failure
- SetModuleVersionMap failure
- InitGenesis failure

Closes: KiiChain#264
@g0spel
g0spel requested a review from jhelison as a code owner July 5, 2026 00:37
@coderabbitai

coderabbitai Bot commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

InitChainer now returns wrapped errors for genesis unmarshal, module version map, and module genesis initialization failures instead of panicking. Several module InitGenesis paths also switch from MustUnmarshalJSON to explicit UnmarshalJSON checks, with formatted panic messages on decode failure. Oracle genesis parsing was updated similarly and now imports fmt.

Estimated code review effort: 2 (Simple) | ~10 minutes

Changes

  • InitChainer now returns errors instead of panicking on initialization failures.
  • Genesis decoding in fee abstraction, oracle, rewards, and tokenfactory uses explicit unmarshal error handling.
  • Oracle genesis parsing adds formatted error messages and a fmt import.

Related Issues

Related Issues: #264

Suggested Labels

bug, app, genesis

Suggested Reviewers

None identified from the provided context.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Out of Scope Changes check ⚠️ Warning The PR also modifies several module genesis unmarshalling paths, which are beyond the linked issue's InitChainer fix. Keep the PR scoped to app/app.go, or split the module error-message updates into a separate follow-up PR.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly matches the main change: replacing InitChainer panics with error returns.
Description check ✅ Passed The description accurately summarizes the InitChainer error-handling fix and is clearly related to the changeset.
Linked Issues check ✅ Passed The PR addresses #264 by returning errors from InitChainer for unmarshal, version-map, and InitGenesis failures.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@app/app.go`:
- Around line 388-391: The app-level InitGenesis flow is still vulnerable to
panics because some module InitGenesis implementations use MustUnmarshalJSON.
Update the InitGenesis decode paths in x/tokenfactory, x/rewards,
x/feeabstraction, and x/oracle so they return errors instead of panicking, and
make AppModule.InitGenesis propagate those errors through app.mm.InitGenesis.
Keep the existing error-wrapping pattern in app/app.go so malformed genesis data
is surfaced as a returned error, not a startup panic.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: a6d4fc94-3d99-4a5e-ad94-4a109929c358

📥 Commits

Reviewing files that changed from the base of the PR and between 3192468 and 464a769.

📒 Files selected for processing (1)
  • app/app.go

Comment thread app/app.go Outdated
g0spel added 2 commits July 5, 2026 09:34
… in module InitGenesis

Addresses coderabbit review: x/tokenfactory, x/rewards,
x/feeabstraction, and x/oracle modules used MustUnmarshalJSON
in their InitGenesis functions, which crashes with unhelpful
'failed to unmarshal JSON' on malformed genesis data.

Replace with UnmarshalJSON + fmt.Errorf panic that includes
the module name for debuggability. Module InitGenesis cannot
return errors (SDK interface constraint), so descriptive
panics are the best available approach without an SDK fork.

5 files, +16 -5 lines. All MustUnmarshalJSON in these modules
are now eliminated.
…panic

Cosmos SDK v0.53.6's Manager.InitGenesis does not recover panics
from individual module InitGenesis functions. If any module panics
during genesis initialization (e.g. malformed genesis data), the
panic propagates unhindered through the ABCI server, crashing the
node.

This wraps the manager call in a closure with defer/recover() so
that module panics are converted to returned errors, allowing the
node to log and shut down cleanly instead of crashing mid-genesis.

Addresses CodeRabbit review on PR KiiChain#349.

Long-term: refactor x/tokenfactory, x/rewards, x/feeabstraction,
x/oracle to return errors instead of panicking in InitGenesis.
@g0spel

g0spel commented Jul 6, 2026

Copy link
Copy Markdown
Author

Addressed CodeRabbit review: wrapped app.mm.InitGenesis with recover() so any module panic is converted to a returned error, preventing node crash during genesis initialization.

Cosmos SDK v0.53.6's Manager.InitGenesis does not recover panics from individual module InitGenesis functions. The long-term fix (refactoring x/tokenfactory, x/rewards, x/feeabstraction, x/oracle to return errors instead of panicking in their InitGenesis implementations) should be done in a follow-up PR to keep the scope manageable.

This change protects against ALL module panics, not just the four identified, and follows the defensive programming pattern already established in this PR.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
app/app.go (1)

388-413: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Recover-based guard correctly addresses the previously flagged panic gap.

This closes the earlier concern that x/tokenfactory, x/rewards, x/feeabstraction, and x/oracle InitGenesis panics would still crash the node — the recover() wrapper here converts any such panic (not just those four) into a returned error, consistent with the PR's stated interim/defensive approach pending module-level refactors.

Minor readability nit: consider extracting the IIFE into a small named helper (e.g. runInitGenesisSafely) to make it independently unit-testable without needing a full app instance, though not essential given the module-level fixes planned as follow-up.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@app/app.go` around lines 388 - 413, Extract the anonymous recover wrapper
around app.mm.InitGenesis in the InitGenesis flow into a small named helper such
as runInitGenesisSafely so the panic-to-error logic is easier to read and unit
test independently. Keep the same behavior by having the helper encapsulate the
defer/recover handling and the app.mm.InitGenesis call, then invoke that helper
from the existing initialization path and preserve the current initErr and
response handling.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@app/app.go`:
- Around line 388-413: Extract the anonymous recover wrapper around
app.mm.InitGenesis in the InitGenesis flow into a small named helper such as
runInitGenesisSafely so the panic-to-error logic is easier to read and unit test
independently. Keep the same behavior by having the helper encapsulate the
defer/recover handling and the app.mm.InitGenesis call, then invoke that helper
from the existing initialization path and preserve the current initErr and
response handling.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: db3a7add-52ee-4ec8-85b1-c0b11fb5f450

📥 Commits

Reviewing files that changed from the base of the PR and between b955189 and 1e6a901.

📒 Files selected for processing (1)
  • app/app.go

@g0spel

g0spel commented Jul 6, 2026

Copy link
Copy Markdown
Author

@jhelison — friendly ping. This PR has been ready for review since July 5. It wraps app.mm.InitGenesis with recover() to prevent node crash on module InitGenesis panic (Cosmos SDK v0.53.6 doesn't recover module panics). The three InitChainer panics have also been converted to proper error returns. All CI checks passing. Could you take a look when you have a moment?

@codecov

codecov Bot commented Jul 6, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 23 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
app/app.go 0.00% 13 Missing ⚠️
x/feeabstraction/module.go 0.00% 2 Missing ⚠️
x/oracle/module.go 0.00% 2 Missing ⚠️
x/oracle/types/genesis.go 0.00% 2 Missing ⚠️
x/rewards/module.go 0.00% 2 Missing ⚠️
x/tokenfactory/module.go 0.00% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] InitChainer Function Uses Panic Instead of Error Returns

1 participant