Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: allow invalid arguments to throw appropriate Error message #2367

Merged
merged 2 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions source/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ const create = (defaults: InstanceDefaults): Got => {
const lastHandler = (normalized: Options): GotReturn => {
// Note: `options` is `undefined` when `new Options(...)` fails
request.options = normalized;
request._noPipe = !normalized.isStream;
request._noPipe = !normalized?.isStream;
void request.flush();

if (normalized.isStream) {
if (normalized?.isStream) {
return request;
}

Expand All @@ -72,7 +72,7 @@ const create = (defaults: InstanceDefaults): Got => {

const result = handler(newOptions, iterateHandlers) as GotReturn;

if (is.promise(result) && !request.options.isStream) {
if (is.promise(result) && !request.options?.isStream) {
promise ||= asPromise(request);

if (result !== promise) {
Expand Down
10 changes: 10 additions & 0 deletions test/arguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ test('throws if the url option is missing', async t => {
});
});

test('throws if an invalid argument is passed', async t => {
await t.throwsAsync(
// @ts-expect-error Error tests
got(false),
{
instanceOf: RequestError,
message: 'Expected values which are `string`, `URL`, `Object`, or `undefined`. Received values of type `boolean`.',
});
});

test('throws an error if the protocol is not specified', async t => {
const error = await t.throwsAsync(got('example.com'));
invalidUrl(t, error!, 'example.com');
Expand Down