Skip to content

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

Open
giwaov wants to merge 2 commits into
KiiChain:mainfrom
giwaov:fix/initchainer-panic-to-error
Open

fix(app): replace panic with error returns in InitChainer#309
giwaov wants to merge 2 commits into
KiiChain:mainfrom
giwaov:fix/initchainer-panic-to-error

Conversation

@giwaov
Copy link
Copy Markdown

@giwaov giwaov commented Apr 2, 2026

Summary

Replaces three panic() calls in InitChainer with proper error returns using mt.Errorf wrapping, preventing node crashes on initialization failures.

Changes

Modified: �pp/app.go

  • mjson.Unmarshal failure: panic(err) replaced with
    eturn nil, fmt.Errorf("failed to unmarshal genesis state: %w", err)
  • SetModuleVersionMap failure: panic(err) replaced with
    eturn nil, fmt.Errorf("failed to set module version map: %w", err)
  • InitGenesis failure: panic(err) replaced with
    eturn nil, fmt.Errorf("failed to init genesis: %w", err)

Modified: CHANGELOG.md

  • Added entry under ## Unreleased

Rationale

The function signature already returns (*abci.ResponseInitChain, error) but was using panic instead of the error return, violating its own contract. CometBFT ABCI layer handles returned errors gracefully, so there is no need to crash the process.

Closes #264

Replace three panic() calls in InitChainer with proper error returns
using fmt.Errorf wrapping. The function signature already returns error
but was using panic instead, violating its own contract and crashing the
node on initialization failures.

Closes KiiChain#264
@giwaov giwaov requested a review from jhelison as a code owner April 2, 2026 09:40
Copilot AI review requested due to automatic review settings April 2, 2026 09:40
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 2, 2026

Walkthrough

InitChainer in app/app.go no longer panics on initialization failures; it now returns wrapped errors for (1) failing to unmarshal req.AppStateBytes into GenesisState, (2) UpgradeKeeper.SetModuleVersionMap failures, and (3) mm.InitGenesis failures. A unit test TestInitChainerReturnsErrorOnInvalidJSON was added to assert InitChainer returns an error (and does not panic) on invalid genesis JSON. A changelog entry under Unreleased -> Fixed documents the fix.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically summarizes the main change: replacing panic calls with error returns in the InitChainer function.
Description check ✅ Passed The description is related to the changeset, outlining the panic-to-error replacements in InitChainer and explaining the rationale.
Linked Issues check ✅ Passed All objectives from issue #264 are met: three panic calls replaced with fmt.Errorf wrapping [#264], test added verifying error returns instead of panics [#264], and CHANGELOG.md updated [#264].
Out of Scope Changes check ✅ Passed All changes are scoped to the PR objectives: app/app.go error handling, test coverage, and CHANGELOG documentation. No out-of-scope modifications detected.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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 and usage tips.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates InitChainer to return wrapped errors instead of panicking, avoiding process crashes during chain initialization failures.

Changes:

  • Replace panic(err) with return nil, fmt.Errorf(...: %w, err) for genesis JSON unmarshal failures.
  • Replace panic(err) with error returns for module version map initialization failures.
  • Replace panic(err) with error returns for InitGenesis failures, and add a corresponding changelog entry.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
app/app.go Converts three initialization panics in InitChainer into proper error returns with context.
CHANGELOG.md Documents the fix under ## Unreleased -> ### Fixed.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread app/app.go
Comment on lines 378 to 382
func (app *KiichainApp) InitChainer(ctx sdk.Context, req *abci.RequestInitChain) (*abci.ResponseInitChain, error) {
var genesisState GenesisState
if err := tmjson.Unmarshal(req.AppStateBytes, &genesisState); err != nil {
panic(err)
return nil, fmt.Errorf("failed to unmarshal genesis state: %w", err)
}
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

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

Consider adding/adjusting a unit test to cover this new behavior: when req.AppStateBytes contains invalid JSON, InitChainer should return a non-nil error (and not panic). This guards against regressions and matches the new contract implied by the signature.

Copilot uses AI. Check for mistakes.
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 2, 2026

Codecov Report

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

Files with missing lines Patch % Lines
app/app.go 0.00% 3 Missing ⚠️

📢 Thoughts on this report? Let us know!

Verify that InitChainer returns an error instead of panicking
when given invalid JSON input. Addresses Copilot review feedback
and improves patch coverage.
@giwaov
Copy link
Copy Markdown
Author

giwaov commented Apr 2, 2026

Added unit test TestInitChainerReturnsErrorOnInvalidJSON to verify InitChainer returns an error (instead of panicking) when given invalid JSON input. This addresses the Copilot review suggestion and improves patch coverage on the error-return paths.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

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_test.go (1)

55-77: Consider adding tests for the other two converted error paths.

Current coverage protects the unmarshal branch only. Adding targeted tests for SetModuleVersionMap and InitGenesis error returns would better prevent regressions for all three panic removals.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@app/app_test.go` around lines 55 - 77, Add two new unit tests alongside
TestInitChainerReturnsErrorOnInvalidJSON that call InitChainer but simulate
failures in SetModuleVersionMap and in InitGenesis: for SetModuleVersionMap,
construct a malformed or hookable genesis that causes SetModuleVersionMap to
return an error and assert InitChainer returns that error (check error message
contains or wraps the SetModuleVersionMap failure); for InitGenesis, provide a
genesis state that causes a module's InitGenesis to return an error and assert
InitChainer returns that specific error. Use the same app.NewKiichainApp /
app.NewContext setup and mirror the require.NotPanics wrapper used in
TestInitChainerReturnsErrorOnInvalidJSON, referencing InitChainer,
SetModuleVersionMap, and InitGenesis in the test names and assertions so
failures map directly to those code paths.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@app/app_test.go`:
- Around line 55-77: Add two new unit tests alongside
TestInitChainerReturnsErrorOnInvalidJSON that call InitChainer but simulate
failures in SetModuleVersionMap and in InitGenesis: for SetModuleVersionMap,
construct a malformed or hookable genesis that causes SetModuleVersionMap to
return an error and assert InitChainer returns that error (check error message
contains or wraps the SetModuleVersionMap failure); for InitGenesis, provide a
genesis state that causes a module's InitGenesis to return an error and assert
InitChainer returns that specific error. Use the same app.NewKiichainApp /
app.NewContext setup and mirror the require.NotPanics wrapper used in
TestInitChainerReturnsErrorOnInvalidJSON, referencing InitChainer,
SetModuleVersionMap, and InitGenesis in the test names and assertions so
failures map directly to those code paths.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: f0cd6138-6331-404d-9c53-8a9b6ea030e3

📥 Commits

Reviewing files that changed from the base of the PR and between d7a1624 and d56081f.

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

@giwaov
Copy link
Copy Markdown
Author

giwaov commented Apr 10, 2026

Hi @jhelison — friendly ping! This PR is ready for review whenever you have a moment. All CI checks are passing. Happy to address any feedback. Thanks!

@giwaov
Copy link
Copy Markdown
Author

giwaov commented Apr 21, 2026

Hey @Thaleszh, would love to get your eyes on this when you have a moment. Thanks!

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

2 participants