Skip to content

fix(webpack): forward async load hook errors to the loader callback#610

Open
greymoth-jp wants to merge 1 commit into
unjs:mainfrom
greymoth-jp:fix/webpack-load-hook-error
Open

fix(webpack): forward async load hook errors to the loader callback#610
greymoth-jp wants to merge 1 commit into
unjs:mainfrom
greymoth-jp:fix/webpack-load-hook-error

Conversation

@greymoth-jp

@greymoth-jp greymoth-jp commented Jun 28, 2026

Copy link
Copy Markdown

The webpack load loader awaits the user load hook but never catches a rejection. When a load hook throws, the error escapes the loader function instead of being passed to webpack's loader callback. Since the loader calls this.async(), webpack's loader runner doesn't await the returned promise, so the throw surfaces as an unhandled promise rejection rather than a normal build error.

The sibling loaders already forward the error to callback:

This wraps the webpack load loader the same way, and adds a unit test that mirrors the existing transform loader error test. Reverting only the source change makes the new test fail (the rejection propagates out of load instead of reaching the callback); with the change the webpack and rspack loader test files pass.

Summary by CodeRabbit

  • Bug Fixes

    • Improved loader error handling so failed async handlers now correctly report errors through the callback.
    • Non-standard thrown values are converted into standard errors before being returned, making failures more consistent.
    • Existing successful result handling remains unchanged for null, string, and object responses.
  • Tests

    • Added coverage for rejected or thrown loader handlers to verify errors are passed to the callback.

@coderabbitai

coderabbitai Bot commented Jun 28, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 38d815ee-5aac-4c97-9580-3e0aa3186ea9

📥 Commits

Reviewing files that changed from the base of the PR and between df47aa2 and 571e620.

📒 Files selected for processing (2)
  • src/webpack/loaders/load.ts
  • test/unit-tests/webpack/loaders/load.test.ts

📝 Walkthrough

Walkthrough

The load webpack loader now wraps its async handler.call(...) invocation in a try/catch. On error, it calls callback(error) for Error instances or converts non-Error throws to new Error(String(error)). A unit test verifies that a rejected plugin.load causes the callback to receive the error.

Changes

Load Loader Error Handling

Layer / File(s) Summary
try/catch error forwarding
src/webpack/loaders/load.ts, test/unit-tests/webpack/loaders/load.test.ts
Handler invocation and result dispatch are wrapped in a try block; the catch block forwards Error instances or stringified non-Error throws to the webpack callback. A new test asserts that a rejected plugin.load results in the error being passed to the callback.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~5 minutes

Poem

🐇 A try and a catch, now errors won't flee,
The loader reports them with callback(e).
Non-Error? No worries, we'll String it away,
Our webpack won't crash in dismay.
Hop hop, all is well! 🎉

🚥 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
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and accurately summarizes the main change: forwarding async load hook errors to the loader callback.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ 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.

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.

1 participant