Skip to content

Conversation

@Paveltarno
Copy link
Contributor

@Paveltarno Paveltarno commented Jan 27, 2026

Description

This PR refactors API error handling by introducing a centralized ApiError.fromHttpError() method that converts HTTP client errors into structured ApiError instances. Previously, API methods used inconsistent patterns for error handling. Now, all API calls use a standardized try-catch pattern with ApiError.fromHttpError(), which automatically extracts status codes, formats error messages from response bodies, and adds contextual information about which operation failed.

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

  • Added ApiError.fromHttpError() static method in src/core/errors.ts to convert HTTP errors to ApiError with context
  • Moved formatApiError() function from src/core/clients/base44-client.ts to src/core/errors.ts (now internal)
  • Added HttpErrorLike interface and isHttpError() type guard for safe error parsing
  • Removed formatApiError and API error types from src/core/clients/index.ts exports
  • Updated all API methods in agent/api.ts, entity/api.ts, function/api.ts, project/api.ts, and site/api.ts to use try-catch with ApiError.fromHttpError()
  • Preserved special 428 status code handling in entity/api.ts using ky's HTTPError type
  • Updated ApiErrorResponseSchema naming in schemas.ts for clarity
  • Updated test expectations to match new error message format
  • Added comprehensive JSDoc comments explaining the new error handling approach
  • Added detailed error handling pattern documentation to AGENTS.md

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 improves error handling consistency across all API operations while maintaining identical functionality from the consumer's perspective. The new ApiError.fromHttpError() method provides better error context by including the operation name in error messages (e.g., "Error syncing agents" instead of generic error messages). The change reduces code duplication and makes it easier to maintain consistent error handling patterns as new API methods are added.


🤖 Generated by Claude | 2026-02-01 00:00 UTC

@github-actions
Copy link
Contributor

github-actions bot commented Jan 27, 2026

🚀 Package Preview Available!


Install this PR's preview build with npm:

npm i @base44-preview/cli@0.0.25-pr.135.55d3e79

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.25-pr.135.55d3e79"

Or add it to your package.json dependencies:

{
  "dependencies": {
    "base44": "npm:@base44-preview/cli@0.0.25-pr.135.55d3e79"
  }
}

Preview published to npm registry — try new features instantly!

github-actions bot and others added 2 commits February 1, 2026 09:53
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>
@kfirstri kfirstri force-pushed the claude/pr-112-20260127-1342 branch from 0253932 to 07d5d9b Compare February 1, 2026 08:07
@claude
Copy link

claude bot commented Feb 1, 2026

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

@kfirstri kfirstri merged commit f2086f0 into main Feb 1, 2026
6 checks passed
@kfirstri kfirstri deleted the claude/pr-112-20260127-1342 branch February 1, 2026 08:24
@github-project-automation github-project-automation bot moved this from In review to Done in CLI Development Feb 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants