Error Handling & Resilience
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
The regxa project implements a robust error handling and resilience strategy designed to manage the complexities of querying multiple, disparate package registries. The system is built around a typed error hierarchy that normalizes remote API failures into predictable internal structures. This approach ensures that whether an error originates from npm, PyPI, or an Arch Linux repository, the library and CLI can respond with consistent and actionable information.
Key resilience features include a centralized HTTP client that manages retries and backoff policies, as well as specialized handlers for rate limiting and network timeouts. These components work together to provide a stable interface for users and developers, shielding them from transient network issues and registry-specific failure modes.
Sources: AGENTS.md:73-77, src/core/AGENTS.md:28-33, README.md:15-18
Typed Error Hierarchy
Regxa utilizes a class-based error hierarchy rooted in the PkioError class. This structure allows for precise error identification and specialized handling throughout the codebase.
Core Error Classes
All custom errors inherit from PkioError, which itself extends the native JavaScript Error. This hierarchy ensures compatibility with standard error-catching mechanisms while providing domain-specific metadata.
| Error Class | Purpose | Key Metadata |
|---|---|---|
PkioError | Base class for all library-specific errors. | message, name |
HTTPError | Represents failures in network requests. | statusCode, url, body |
NotFoundError | Specific for missing packages or versions. | ecosystem, packageName, version |
RateLimitError | Triggered when registry rate limits are hit. | retryAfter (seconds) |
UnknownEcosystemError | Thrown for unsupported registry types. | ecosystem |
InvalidPURLError | Indicates a malformed Package URL (PURL). | purl, reason |
Sources: test/unit/errors.test.ts:14-230, src/core/AGENTS.md:15-16
The following class diagram illustrates the relationship between the custom error types:
The hierarchy ensures that instanceof PkioError can be used to catch any library-specific failure.
Sources: test/unit/errors.test.ts:14-230
HTTP Resilience & Client Behavior
The Client class is the central point for all network interactions. It encapsulates resilience logic such as retry mechanisms and timeout handling, preventing individual registry adapters from needing to implement their own boilerplate logic.
Retry and Backoff Policy
Network calls are subject to a project-wide policy managed by the Client. This includes:
- Automatic Retries: Retries are triggered by specific HTTP status codes (e.g., 429 for rate limits, 5xx for server errors).
- Exponential Backoff: The client uses a backoff strategy with jitter to avoid overwhelming registries during recovery.
- Timeouts: Default timeouts are enforced to prevent hanging requests.
Sources: src/core/client.ts, AGENTS.md:79-81, src/core/AGENTS.md:21-22
Error Mapping in Adapters
Registry adapters are responsible for mapping upstream API responses to the typed error hierarchy. For example, if an npm request returns a 404, the adapter catches the HTTPError and rethrows it as a NotFoundError.
CLI Error Handling
The CLI implements a top-level error wrapper, withErrorHandling, to provide clean, user-friendly messages instead of raw stack traces. This wrapper intercepts specific error types and formats them for the console.
Shared Error Wrapper Logic
The withErrorHandling function performs the following actions based on the error type:
- RateLimitError: Prints "Rate limited by registry" and includes the
retryAfterduration. - HTTPError: Reports the status code and the failing URL.
- Server Errors (5xx): Suggests that the registry might be temporarily unavailable and advises retrying later.
- Process Exit: Consistently exits the process with a code of
1upon catching handled errors.
Sources: src/commands/shared.ts, test/unit/shared-command.test.ts:24-50, CHANGELOG.md:12
The sequence of a failed CLI command is shown below:
Resilience in Bulk Operations
When performing bulk package lookups via bulkFetchPackages, the system prioritizes overall progress over individual failures.
Worker Strategy
The bulk fetch utility uses a worker pool (defaulting to 15 concurrent requests). If a single package lookup fails, the error is caught within the worker loop, and the failure is "silently skipped" to allow the remaining items in the queue to be processed. The resulting Map will simply omit the keys for packages that could not be retrieved.
Sources: src/helpers.ts:47-66, AGENTS.md:124
| Component | Resilience Strategy |
|---|---|
| Bulk Fetch | Skip failed packages to complete the batch. |
| Abort Signals | Workers check signal.aborted to stop execution immediately on user cancellation. |
| Concurrency Limit | Prevents local resource exhaustion and remote rate limiting. |
Sources: src/helpers.ts:47-66, test/unit/helpers.test.ts:153-195
Conclusion
Error handling in regxa is a multi-layered system that transitions from low-level network resilience (Client retries) to semantic domain errors (NotFoundError) and finally to user-facing CLI presentation. By centralizing these concerns in src/core/errors.ts and src/core/client.ts, the project maintains a high degree of reliability and consistency across all supported package ecosystems.