Fix "X is not a constructor" TypeError on API error responses - #128
Merged
Merged
Conversation
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.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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, sonew 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
functiondeclarations, 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