Skip to content

fix(middleware): guard typed-nil *fiber.Error matched by errors.As - #48

Merged
fredcamaral merged 3 commits into
developfrom
fix/typed-nil-fiber-error-substitution
Aug 10, 2026
Merged

fix(middleware): guard typed-nil *fiber.Error matched by errors.As#48
fredcamaral merged 3 commits into
developfrom
fix/typed-nil-fiber-error-substitution

Conversation

@fredcamaral

@fredcamaral fredcamaral commented Aug 10, 2026

Copy link
Copy Markdown
Member

What

errors.As matches a typed-nil *fiber.Error inside a joined chain (errors.Join((*fiber.Error)(nil), ...)) and leaves the target nil. Reading .Code then panics with a nil-pointer dereference, inside WithHTTPErrorHandling, the middleware whose purpose is closing exactly this defect class. Found by a downstream consumption review in Matcher (one of the six rows of its own typed-nil test table), confirmed by execution against the released version.

Because errors.As stops at the first type match, a bare nil guard was not enough: a typed-nil *fiber.Error joined before a valid fiber.NewError(400, ...) would shadow it and downgrade the mapped status to a generic 500. The fix is a shared extractor that mirrors errors.As traversal but skips nil matches and keeps searching, used both in the substitution branch of WithHTTPErrorHandling and in httpStatusCode:

func asFiberError(err error) *fiber.Error {
	if err == nil {
		return nil
	}
	if fiberErr, ok := err.(*fiber.Error); ok && fiberErr != nil {
		return fiberErr
	}
	// ... As(any) bool, then Unwrap() error / Unwrap() []error,
	// depth-first in join order, skipping nil matches ...
}

Regression tests cover joined/delegating typed-nil shapes against Fiber's default ErrorHandler, including a typed-nil *fiber.Error preceding a valid one (flat and nested in an earlier subtree).

Affects both released lines: this PR fixes v3; the identical fix for the v2.1.x maintenance line is pushed on fix/typed-nil-fiber-error-substitution-v2 (cut from the v2.1.2 tag, needs a v2.1.3 tag on that branch, since main is now the /v3 module path).

errors.As matches a typed-nil *fiber.Error inside a joined chain
(errors.Join((*fiber.Error)(nil), ...)) and leaves the target nil;
reading .Code then panics with a nil-pointer dereference - inside the
finalizer whose purpose is closing exactly this defect class.
Confirmed by execution on the released line. Same fix shipped for the
v2 maintenance line on fix/typed-nil-fiber-error-substitution-v2.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019J6AsXF9EbWtJ7CV7WtSdD
@coderabbitai

coderabbitai Bot commented Aug 10, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: afc2611e-d408-4210-be90-74935a422b00

📥 Commits

Reviewing files that changed from the base of the PR and between 7103562 and 7cefe45.

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

📝 Walkthrough

Walkthrough

The middleware now traverses wrapped and joined errors safely. Typed-nil *fiber.Error values no longer cause panics or mask later valid Fiber errors. Regression tests cover HTTP handling and status extraction.

Changes

HTTP error handling

Layer / File(s) Summary
Fiber error traversal and integrations
middleware/error_handling.go, middleware/logging.go
asFiberError traverses direct, custom, wrapped, and joined errors. HTTP error handling and logging use it to ignore typed-nil matches and preserve valid mapped errors.
Unsafe error-chain regression coverage
middleware/error_handling_regression_test.go
Tests cover typed-nil errors, nil-cause wrappers, nested joined chains, panic-free handling, internal-server-error fallback, and valid later Fiber errors.

Possibly related PRs

✨ Finishing Touches
✨ Simplify code
  • Create PR with simplified code
  • Commit simplified code in branch fix/typed-nil-fiber-error-substitution

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

@lerian-studio

Copy link
Copy Markdown
Contributor

⚠️ Invalid Source Branch

Pull requests to main can only come from:

  • develop
  • release-candidate
  • hotfix/*

Your source branch: fix/typed-nil-fiber-error-substitution

Please change the base branch or create a PR from an allowed branch.

@lerian-studio

lerian-studio commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

🔍 PR Validation Summary

✅ PR Mergeable — no blocking failures

Check Status Blocking
Source Branch ✅ success yes
PR Title ✅ success yes
PR Description ✅ success yes
PR Size ✅ success no
Auto Labels ✅ success no
PR Metadata ✅ success no

🔍 View workflow run

@lerian-studio

lerian-studio commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

🔒 Security Scan Results — lib-observability

✅ PR Mergeable — no blocking findings

Stage Status Blocking?
Filesystem Scan ✅ Clean
Docker Image Scan ➖ Skipped
Docker Hub Health Score ➖ Skipped
Pre-release Version Check ✅ Clean

Trivy

Filesystem Scan

✅ No vulnerabilities or secrets found.


Pre-release Version Check

✅ No unstable version pins found.


🔍 View full scan logs

@fredcamaral
fredcamaral changed the base branch from main to develop August 10, 2026 20:41

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

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 `@middleware/error_handling.go`:
- Around line 100-108: The error handling must skip typed-nil *fiber.Error
values and continue searching for a later valid Fiber error. Add a shared
extractor that traverses joined and wrapped error trees, returns only non-nil
*fiber.Error values, and reuse it in this normalization branch and
httpStatusCode; add regression coverage for a typed-nil Fiber error preceding a
valid one.
🪄 Autofix

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: ASSERTIVE

Plan: Pro

Run ID: 810ed5b2-1c98-4ee6-8bdd-e796e598e488

📥 Commits

Reviewing files that changed from the base of the PR and between f540cf0 and a1aba12.

📒 Files selected for processing (2)
  • middleware/error_handling.go
  • middleware/error_handling_regression_test.go

Comment thread middleware/error_handling.go Outdated
@lerian-studio lerian-studio added size/S PR changes 50–199 lines middleware HTTP/gRPC observability middleware tests Unit, integration and end-to-end tests labels Aug 10, 2026
@lerian-studio

Copy link
Copy Markdown
Contributor

📊 Unit Test Coverage Report: lib-observability

Metric Value
Overall Coverage 87.6% ✅ PASS
Threshold 80%

Coverage by Package

Package Coverage
github.com/LerianStudio/lib-observability/v3/assert 98.2%
github.com/LerianStudio/lib-observability/v3/constants 83.3%
github.com/LerianStudio/lib-observability/v3/grpcmiddleware 78.7%
github.com/LerianStudio/lib-observability/v3/httpobs 77.8%
github.com/LerianStudio/lib-observability/v3/log 90.7%
github.com/LerianStudio/lib-observability/v3/messagingobs 89.4%
github.com/LerianStudio/lib-observability/v3/metrics 91.4%
github.com/LerianStudio/lib-observability/v3/middleware 84.1%
github.com/LerianStudio/lib-observability/v3/redaction 95.8%
github.com/LerianStudio/lib-observability/v3/redisobs 63.1%
github.com/LerianStudio/lib-observability/v3/runtime 80.9%
github.com/LerianStudio/lib-observability/v3/sqlobs 64.2%
github.com/LerianStudio/lib-observability/v3/telemetrycore 84.2%
github.com/LerianStudio/lib-observability/v3/tracing 88.0%
github.com/LerianStudio/lib-observability/v3/zap 96.1%
github.com/LerianStudio/lib-observability/v3 91.5%

Generated by Go PR Analysis workflow

… joined chains

X-Lerian-Ref: 0x1

Co-authored-by: Codesmith <codesmith-bot@users.noreply.github.com>
@lerian-studio lerian-studio added size/M PR changes 200–499 lines and removed size/S PR changes 50–199 lines labels Aug 10, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

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 `@middleware/error_handling.go`:
- Around line 134-145: Update asFiberError to resolve the reported errorlint and
inamedparam violations while preserving its manual traversal and typed-nil
handling. Rename the parameter in the local As interface to satisfy inamedparam,
and add narrow linter suppressions for the intentional direct type assertions
and inspection in the fiber.Error checks. Run golangci-lint run ./... and
confirm no configured violations remain.
🪄 Autofix

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: ASSERTIVE

Plan: Pro

Run ID: e747f629-e48f-4e23-833e-c85bd63ae3db

📥 Commits

Reviewing files that changed from the base of the PR and between a1aba12 and 7103562.

📒 Files selected for processing (3)
  • middleware/error_handling.go
  • middleware/error_handling_regression_test.go
  • middleware/logging.go

Comment thread middleware/error_handling.go Outdated
X-Lerian-Ref: 0x1

Co-authored-by: Codesmith <codesmith-bot@users.noreply.github.com>
@fredcamaral
fredcamaral merged commit f61444c into develop Aug 10, 2026
24 checks passed
@github-actions
github-actions Bot deleted the fix/typed-nil-fiber-error-substitution branch August 10, 2026 21:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

middleware HTTP/gRPC observability middleware size/M PR changes 200–499 lines tests Unit, integration and end-to-end tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants