Skip to content

fix(loader): replace panic with error handling in template loader when dialers are missing#7054

Open
a638011 wants to merge 3 commits intoprojectdiscovery:devfrom
a638011:fix/replace-panic-with-error-handling
Open

fix(loader): replace panic with error handling in template loader when dialers are missing#7054
a638011 wants to merge 3 commits intoprojectdiscovery:devfrom
a638011:fix/replace-panic-with-error-handling

Conversation

@a638011
Copy link

@a638011 a638011 commented Feb 27, 2026

Description

Replaces panic() calls with proper error propagation in the template loader when dialers are missing or wait group creation fails. Fixes #6674.

Changes

pkg/catalog/loader/loader.go

  • Changed LoadTemplatesWithTags signature to return ([]*templates.Template, error) instead of []*templates.Template
  • Changed LoadTemplates signature to return ([]*templates.Template, error) to match
  • Replaced panic("dialers with executionId ... not found") with return nil, fmt.Errorf(...)
  • Replaced panic("could not create wait group") with return nil, fmt.Errorf(...)
  • Moved the executionId/dialers check before the waitgroup creation to avoid unnecessary allocation on error
  • Removed unused dialers variable assignment (inlined the nil check)
  • Updated Load() to handle the error from LoadTemplates by logging it

internal/runner/lazy.go

  • Updated GetLazyAuthFetchCallback to handle the new error return from LoadTemplates

pkg/protocols/common/automaticscan/util.go

  • Updated LoadTemplatesWithTags caller to handle the new error return

pkg/catalog/loader/loader_bench_test.go

  • Updated benchmark tests to use _, _ = for the two-value return

Before

When dialers were missing or wait group creation failed, the application would panic(), causing an unrecoverable crash with no meaningful error message to the user.

After

Errors are properly propagated up the call chain. Callers can handle the error gracefully (log it, return it, etc.) instead of crashing.

Checklist

  • Replace panic with error return in LoadTemplatesWithTags
  • Propagate error through LoadTemplates
  • Update all callers to handle the new error return
  • Code compiles successfully (go build ./...)

/claim #6674

Summary by CodeRabbit

  • Bug Fixes

    • Template loading now surfaces and wraps errors instead of ignoring them; failures and validation issues are reported.
  • Refactor

    • Template loader APIs now return error values alongside results.
  • Tests

    • Benchmark tests updated to handle the loader's new return values.

@neo-by-projectdiscovery-dev
Copy link

neo-by-projectdiscovery-dev bot commented Feb 27, 2026

Neo - PR Security Review

No security issues found

Highlights

  • Benchmark tests now properly fail fast on LoadTemplates errors instead of silently continuing
  • Error handling follows Go best practices with explicit error checking and b.Fatalf on failures
Hardening Notes
  • The changes only affect test code and improve test correctness by detecting template loading failures during benchmarks
  • Error messages in test code are not exposed to external users or attackers - they only appear during development/testing

Comment @neo help for available commands. · Open in Neo

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 27, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5267cb3 and 467e47f.

📒 Files selected for processing (1)
  • pkg/catalog/loader/loader_bench_test.go

Walkthrough

LoadTemplates and LoadTemplatesWithTags now return ([]*templates.Template, error). The loader stops panicking when dialers are missing and returns errors instead; callers (lazy auth runner, automaticscan, benchmarks) were updated to handle and propagate these errors. (50 words)

Changes

Cohort / File(s) Summary
Template Loader
pkg/catalog/loader/loader.go
Signatures changed to ([]*templates.Template, error). Replaced panic on missing dialers with error returns; added error propagation across wait-group/dialer/template loading paths.
Lazy Auth Caller
internal/runner/lazy.go
GetLazyAuthFetchCallback now captures and returns errors from LoadTemplates instead of ignoring them; returns wrapped error on failure.
Automatic Scan Caller
pkg/protocols/common/automaticscan/util.go
Calls to LoadTemplatesWithTags updated to handle (templates, error) and return wrapped errors when loading fails.
Benchmarks / Tests
pkg/catalog/loader/loader_bench_test.go
Benchmarks updated to check and fail on errors returned by LoadTemplates instead of discarding the result.

Sequence Diagram(s)

sequenceDiagram
    participant Runner as Runner (internal/runner)
    participant Loader as Template Loader (pkg/catalog/loader)
    participant Protocol as ProtocolState/Dialers

    Runner->>Loader: LoadTemplates(...)
    Loader->>Protocol: GetDialersWithId(execId)
    alt dialers nil
        Loader-->>Runner: error "dialers with executionId ... not found"
    else dialers present
        Loader-->>Runner: ([]*templates.Template, nil)
    end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Poem

🐰 I hopped through code with careful paws,
No panics now — just tidy laws.
Templates either load or tell why,
I twitch my nose and blink my eye,
A tiny thump, a joyful sigh. 🎋

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.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
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and accurately describes the main change: replacing panic with error handling in the template loader when dialers are missing, which is the central objective of the PR.
Linked Issues check ✅ Passed All coding requirements from #6674 are met: panic replaced with fmt.Errorf, function signatures updated to return error, all callers (lazy.go, automaticscan/util.go, benchmarks) handle the error, and error is propagated properly.
Out of Scope Changes check ✅ Passed All changes are directly scoped to the PR objectives: replacing panics with error handling in the template loader and updating all callers to handle the new error return values.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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
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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@pkg/catalog/loader/loader_bench_test.go`:
- Line 213: The call sites incorrectly unpack two values from
Store.LoadTemplatesOnlyMetadata (which has signature func (store *Store)
LoadTemplatesOnlyMetadata() error); replace occurrences like "_, _ =
store.LoadTemplatesOnlyMetadata()" with a single-value call and handle the error
(for example: "if err := store.LoadTemplatesOnlyMetadata(); err != nil {
t.Fatal(err) }" or assign to err and check), and apply the same change to the
other places that currently try to unpack two return values from
LoadTemplatesOnlyMetadata.

ℹ️ Review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9142eae and 6be5f12.

📒 Files selected for processing (4)
  • internal/runner/lazy.go
  • pkg/catalog/loader/loader.go
  • pkg/catalog/loader/loader_bench_test.go
  • pkg/protocols/common/automaticscan/util.go

Copy link
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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@pkg/catalog/loader/loader_bench_test.go`:
- Line 74: The benchmark currently swallows errors by doing "_, _ =
store.LoadTemplates(...)" which can hide loader failures; change each call to
capture the error (e.g., "_, err := store.LoadTemplates(...)") and fail fast
when non-nil (use the benchmark's b.Fatalf or b.Fatal with a clear message and
the err) so the benchmark stops on real load errors; apply this fix for every
occurrence of store.LoadTemplates in the file (the calls shown at lines
referenced in the review).

ℹ️ Review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6be5f12 and 5267cb3.

📒 Files selected for processing (1)
  • pkg/catalog/loader/loader_bench_test.go

Don't swallow errors in benchmark loops — use b.Fatalf to surface
loader failures instead of silently benchmarking error paths.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Replace panic with error handling in template loader when dialers are missing

1 participant