Skip to content

Commit 20f29fe

Browse files
Backport #1543: Initialize globalResponse in case of ignored HTTPError (#2017)
1 parent 0da732f commit 20f29fe

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

source/as-promise/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,13 @@ export default function asPromise<T>(normalizedOptions: NormalizedOptions): Canc
124124
return;
125125
}
126126

127+
globalResponse = response;
128+
127129
if (!isResponseOk(response)) {
128130
request._beforeError(new HTTPError(response));
129131
return;
130132
}
131133

132-
globalResponse = response;
133-
134134
resolve(request.options.resolveBodyOnly ? response.body as T : response as unknown as T);
135135
});
136136

test/promise.ts

+10
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,13 @@ test('promise.json() can be called before a file stream body is open', withServe
8585

8686
await Promise.all(checks);
8787
});
88+
89+
test('promise.json() does not fail when server returns an error and throwHttpErrors is disabled', withServer, async (t, server, got) => {
90+
server.get('/', (_request, response) => {
91+
response.statusCode = 400;
92+
response.end('{}');
93+
});
94+
95+
const promise = got('', {throwHttpErrors: false});
96+
await t.notThrowsAsync(promise.json());
97+
});

0 commit comments

Comments
 (0)