Skip to content

Conversation

@Paveltarno
Copy link
Contributor

@Paveltarno Paveltarno commented Jan 27, 2026

Description

This PR centralizes API error handling logic by implementing a beforeError hook directly in the ky HTTP client. Previously, every API method had to manually wrap calls and invoke formatApiError. Now, all API errors are automatically caught and formatted by the base client, eliminating boilerplate and ensuring consistent error handling across the codebase.

Related Issue

Addresses refactoring discussed in #112: #112 (comment)

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Refactoring (no functional changes)
  • Other (please describe):

Changes Made

  • Moved formatApiError function to be internal within base44-client.ts (no longer exported)
  • Implemented handleApiErrors hook using ky's beforeError mechanism to automatically format all API errors
  • Updated base44Client configuration to include the new error handling hook
  • Removed formatApiError export from index.ts
  • Simplified agent/api.ts by removing manual error handling and throwHttpErrors: false flags
  • Simplified entity/api.ts by removing manual error handling while preserving special 428 status code logic
  • Removed tests/core/errors.test.ts as formatApiError is now an internal implementation detail
  • Updated JSDoc comments to reflect automatic error handling behavior

Testing

  • I have tested these changes locally
  • I have added/updated tests as needed
  • All tests pass (npm test)

Checklist

  • My code follows the project's style guidelines
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation (if applicable)
  • My changes generate no new warnings
  • I have updated AGENTS.md if I made architectural changes

Additional Notes

This refactoring reduces boilerplate code by 13 net lines (83 additions, 96 deletions) while improving maintainability. The error handling behavior remains functionally identical from the consumer's perspective - all API errors are still caught and formatted into readable error messages. The 428 status code special handling in syncEntities is preserved using ky's HTTPError type.


🤖 Generated by Claude | 2026-01-27 21:45 UTC

This commit moves API error handling logic directly into the base ky client
using the beforeError hook, eliminating the need to manually call formatApiError
in every API method.

Changes:
- Made formatApiError internal to base44-client.ts
- Added handleApiErrors hook using ky's beforeError mechanism
- Updated base44Client to include error handling in beforeError hooks
- Removed formatApiError export from index.ts
- Simplified agent/api.ts by removing manual error handling
- Simplified entity/api.ts by removing manual error handling
- Preserved 428 status code handling in syncEntities
- Removed tests/core/errors.test.ts as formatApiError is now internal

Benefits:
- Reduces boilerplate in API client methods
- Ensures consistent error handling across all API calls
- Centralizes error formatting logic
- Makes the codebase easier to maintain

Co-authored-by: paveltarno <Paveltarno@users.noreply.github.com>
@github-actions
Copy link
Contributor

🚀 Package Preview Available!


Install this PR's preview build with npm:

npm i @base44-preview/cli@0.0.22-pr.135.f4b78d8

Prefer not to change any import paths? Install using npm alias so your code still imports base44:

npm i "base44@npm:@base44-preview/cli@0.0.22-pr.135.f4b78d8"

Or add it to your package.json dependencies:

{
  "dependencies": {
    "base44": "npm:@base44-preview/cli@0.0.22-pr.135.f4b78d8"
  }
}

Preview published to npm registry — try new features instantly!

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.

2 participants