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