Skip to content

Conversation

@ecancino
Copy link

@ecancino ecancino commented Nov 27, 2025

Summary

This PR enhances the .catcher() method to accept multiple error codes via an array, improving developer ergonomics when handling groups of related HTTP status codes with the same error handler.

Motivation

Previously, catching multiple error codes required chaining multiple .catcher() calls with the same handler:

const api = wretch("https://api.example.com")
  .catcher(401, err => redirect("/login"))
  .catcher(403, err => redirect("/login"))
  .catcher(407, err => redirect("/login"))

This was verbose and repetitive. The enhancement allows:

const api = wretch("https://api.example.com")
  .catcher([401, 403, 407], err => redirect("/login"))

Changes

Implementation (src/core.ts)

  • Updated .catcher() to accept both single error IDs and arrays
  • Converts single values to arrays internally for consistent handling
  • Loops through array to register each error ID with the same handler

Type Definitions (src/types.ts)

  • Updated signature: errorId: number | string | symbolerrorId: ErrorId | ErrorId[]
  • Added new ErrorId type export for reusability
  • Enhanced JSDoc with examples showing array usage

Documentation (README.md)

  • Added "Multiple Error Codes" section under Catchers
  • Included real-world examples (auth errors, server errors)

Tests (test/shared/wretch.spec.ts)

Added 4 comprehensive test cases:

  • ✅ Catching multiple error codes with a single catcher
  • ✅ Mixing single and array error IDs
  • ✅ Handling empty arrays gracefully
  • ✅ Supporting mixed error ID types (number, string, symbol)

Testing

All tests passing (78/78):

✔ should catch multiple error codes with a single catcher (1.942ms)
✔ should support mixing single and array error IDs (1.698ms)
✔ should handle empty array gracefully (0.491ms)
✔ should support array of mixed error ID types (0.538ms)

Code coverage: 98.52% statements, 95.62% branches

Backward Compatibility

✅ Fully backward compatible - single error IDs work exactly as before:

.catcher(404, err => handleNotFound(err)) // Still works!

CONTRIBUTING.md Compliance

  • ✅ Tests added for new functionality
  • ✅ Documentation updated (JSDoc + README)
  • ✅ Linter passes
  • ✅ All tests pass
  • ✅ Proper indentation (2 spaces)

@ecancino ecancino marked this pull request as draft November 27, 2025 06:23
@ecancino ecancino marked this pull request as ready for review November 29, 2025 21:16
@coveralls
Copy link

Coverage Status

coverage: 97.926% (+0.006%) from 97.92%
when pulling 7943a04 on ecancino:catcher-errors
into c381be2 on elbywan:master.

@elbywan elbywan changed the base branch from master to dev November 30, 2025 09:21
@elbywan elbywan requested a review from Copilot November 30, 2025 10:04
Copy link

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 enhances the .catcher() method to accept multiple error codes via an array, reducing boilerplate when handling groups of related HTTP status codes with the same error handler. The implementation is backward compatible and maintains the existing API for single error IDs.

Key Changes:

  • Modified .catcher() to accept both single error IDs and arrays of error IDs
  • Added ErrorId type alias for better type reusability
  • Enhanced documentation with examples of the new array syntax

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
src/core.ts Updated catcher implementation to normalize input to array and loop through error IDs, registering each with the same handler
src/types.ts Updated catcher signature to accept ErrorId | ErrorId[], added ErrorId type export, and enhanced JSDoc with array usage examples
README.md Added "Multiple Error Codes" section with practical examples showing auth and server error handling patterns
test/shared/wretch.spec.ts Added 4 comprehensive test cases covering multiple error codes, mixed single/array usage, empty arrays, and mixed error ID types

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

@elbywan
Copy link
Owner

elbywan commented Dec 6, 2025

Hey @ecancino, thanks for the PR! Can you fix the failing test before I review?

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.

3 participants