Skip to content

Fix "X is not a constructor" TypeError on API error responses - #128

Merged
shuaitseng merged 1 commit into
masterfrom
fix/error-classes-not-constructor
Jul 13, 2026
Merged

shuaitseng merged 1 commit into
masterfrom
fix/error-classes-not-constructor

Conversation

@shuaitseng

Copy link
Copy Markdown
Contributor

lib/error.js defined ValidationError, PermissionError, HttpError, RateLimitError, and ServiceError as arrow functions, but lib/Methods.js and lib/onfleet.js throw them with new. Arrow functions have no [[Construct]] behavior, so new HttpError(...) (and friends) throws "HttpError is not a constructor" instead of the intended error, swallowing the real Onfleet API error details in the process.

Switch the factory functions to function declarations, which are constructible, while keeping their existing call signature and the returned Error shape (name/status/cause/request) unchanged.

Add regression tests covering all five error branches in Methods.js (RateLimitError, PermissionError, ServiceError x2, HttpError) plus the ValidationError path in the Onfleet constructor, none of which had any test coverage before.

Describe the solution
A clear and concise description of what this PR is trying to accomplish.


Added

Changed

Deprecated

Removed

Fixed

Security

lib/error.js defined ValidationError, PermissionError, HttpError,
RateLimitError, and ServiceError as arrow functions, but lib/Methods.js
and lib/onfleet.js throw them with `new`. Arrow functions have no
[[Construct]] behavior, so `new HttpError(...)` (and friends) throws
"HttpError is not a constructor" instead of the intended error,
swallowing the real Onfleet API error details in the process.

Switch the factory functions to `function` declarations, which are
constructible, while keeping their existing call signature and the
returned Error shape (name/status/cause/request) unchanged.

Add regression tests covering all five error branches in Methods.js
(RateLimitError, PermissionError, ServiceError x2, HttpError) plus the
ValidationError path in the Onfleet constructor, none of which had any
test coverage before.
@shuaitseng
shuaitseng requested a review from gonzalogrisafi July 7, 2026 18:32

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Re-trigger cubic

@gonzalogrisafi gonzalogrisafi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@shuaitseng
shuaitseng merged commit 5c3ad96 into master Jul 13, 2026
10 checks passed
joao-onfleet pushed a commit that referenced this pull request Sep 15, 2026
Describe the solution
lib/error.js defined ValidationError, PermissionError, HttpError, RateLimitError, and ServiceError as arrow functions, but they're invoked with `new` in lib/Methods.js and lib/onfleet.js. Arrow functions can't be constructed, so every error path threw `TypeError: X is not a constructor` instead of the intended error, swallowing the real Onfleet API error (message, status, cause, request). This PR converts the five error factories to `function` declarations, which are constructible, so the existing `new X(...)` call sites now work as intended. No public API, call signature, or error shape changes.

Added
- Regression tests covering all five error branches (RateLimitError, PermissionError, ServiceError x2, HttpError, and the ValidationError path in the Onfleet constructor), none of which had prior test coverage.
Changed
- lib/error.js: ValidationError, PermissionError, HttpError, RateLimitError, and ServiceError changed from arrow functions to `function` declarations.

Deprecated
N/A

Removed
N/A

Fixed
- Fixed `TypeError: X is not a constructor` thrown whenever the Onfleet API returned a non-2xx response, which masked the real API error details (rate limiting, permission errors, service errors, generic HTTP errors) and the missing-API-key validation error.

Security
N/A
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