Skip to content

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

Open
shuaitseng wants to merge 1 commit into
masterfrom
fix/error-classes-not-constructor
Open

Fix "X is not a constructor" TypeError on API error responses#128
shuaitseng wants to merge 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

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!

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