Skip to content

Improve error handling and test robustness across CLI and crawler#46

Merged
YusukeHirao merged 4 commits intomainfrom
claude/add-conditional-assertions-zUNmU
Mar 5, 2026
Merged

Improve error handling and test robustness across CLI and crawler#46
YusukeHirao merged 4 commits intomainfrom
claude/add-conditional-assertions-zUNmU

Conversation

@YusukeHirao
Copy link
Copy Markdown
Member

@YusukeHirao YusukeHirao commented Mar 5, 2026

Summary

This PR improves error handling patterns and test reliability across the codebase by introducing proper error aggregation, better exception handling in async operations, and more robust test assertions.

closes: #16

Key Changes

Error Handling Improvements

  • CLI Error Propagation: Modified crawl and pipeline commands to throw CrawlAggregateError instead of calling process.exit(1), allowing callers to handle errors programmatically. The error is caught at the CLI entry point and exits there.
  • New CrawlAggregateError Class: Created a custom error class that wraps multiple errors from crawl operations, providing structured error information to callers.
  • Lighthouse Error Normalization: Added toError() utility function to safely convert any thrown value (including non-Error types) into an Error instance, preserving stack traces for actual Errors.
  • Lighthouse Plugin Robustness: Updated the Lighthouse plugin to wrap non-Error exceptions into Error objects and return error results instead of propagating unexpected throws.

Test Improvements

  • Archive Tests: Wrapped test cleanup in try-finally blocks to ensure mocks are restored and resources are cleaned up even when assertions fail.
  • New Test Coverage: Added comprehensive tests for toError() utility covering Error instances, strings, null, undefined, and Error subclasses.
  • Test Assertions: Replaced conditional assertions with explicit expect().toBeDefined() checks in e2e tests, making test failures more obvious and preventing silent test passes.
  • WriteQueue Tests: Added test for drain() on empty queue to verify immediate resolution.

CLI Enhancements

  • Silent Mode Support: Added --silent flag support to the pipeline command to suppress console output during execution, useful for programmatic usage.
  • Documentation: Updated JSDoc comments to reflect that errors now propagate as exceptions rather than causing process exit.

Code Quality

  • WriteQueue Signature: Updated enqueue() to accept both async and synchronous operations (Promise<T> | T), improving flexibility.
  • Documentation: Added TODO comment in pipeline.ts noting flag definition duplication across commands and the need for manual synchronization.

Implementation Details

  • Error handling now follows a consistent pattern: operations throw exceptions, which are caught at appropriate boundaries (CLI entry points) where process.exit() is called.
  • The toError() utility ensures that even unexpected non-Error throws are properly converted to Error instances with meaningful messages.
  • Test cleanup is now guaranteed via try-finally blocks, preventing resource leaks and mock pollution between tests.

https://claude.ai/code/session_01Gz7eS3SnV1N6LakV8b7CxM

claude added 4 commits March 5, 2026 10:18
if文でラップされたexpect()を削除し、find()/filter()の結果を
expect().toBeDefined() / expect().toBeGreaterThan(0) で明示的に
アサートするように変更。条件がfalseの場合にテストがサイレントに
パスする問題を解消。

対象ファイル:
- options.e2e.ts: externalPage の存在を事前アサート
- single-page.e2e.ts: aboutPages の要素数を事前アサート
- scope.e2e.ts: docsPage の存在を事前アサート

Closes #16

https://claude.ai/code/session_01Gz7eS3SnV1N6LakV8b7CxM
- startCrawl: process.exit(1)をCrawlAggregateError throwに変更し、
  crawl CLIハンドラ側でcatchしてexit。pipelineからの呼び出しで
  エラーハンドリングが効くようにする
- pipeline: --silent時にpipeline自身のconsole.logを抑制
- analyze-lighthouse: toErrorヘルパーで全例外を統一的にError化
- archive.spec.ts: try/finallyでクリーンアップを保護
- WriteQueue.enqueue: 型シグネチャを() => Promise<T> | Tに修正
- テスト追加: --silent抑制、drain()空キュー、clearHtmlPath失敗伝搬

https://claude.ai/code/session_01Gz7eS3SnV1N6LakV8b7CxM
- crawl.ts: resumeCrawlをtry/catchブロック内に移動し、
  --resumeモードのCrawlAggregateErrorも適切にprocess.exit(1)する
- pipeline.ts: フラグ定義重複に関するTODOコメントを追加

https://claude.ai/code/session_01Gz7eS3SnV1N6LakV8b7CxM
- pipeline.ts: process.exit(1)ではなく例外伝搬である旨に修正
- crawl.ts: CrawlAggregateErrorコンストラクタに@paramタグ追加
- database.ts: getJSON関数に@templateタグ追加

https://claude.ai/code/session_01Gz7eS3SnV1N6LakV8b7CxM
@YusukeHirao YusukeHirao force-pushed the claude/add-conditional-assertions-zUNmU branch from aedc80f to 9bee88b Compare March 5, 2026 10:20
@YusukeHirao YusukeHirao merged commit b5c306f into main Mar 5, 2026
3 checks passed
@YusukeHirao YusukeHirao deleted the claude/add-conditional-assertions-zUNmU branch March 5, 2026 10:23
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.

E2E テストに条件付きアサーションがある

2 participants