Skip to content
This repository was archived by the owner on Oct 10, 2022. It is now read-only.

Commit 5372d8b

Browse files
committed
chore!: fix new version of node-fetch
1 parent 46ca4c6 commit 5372d8b

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

src/index.test.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -314,14 +314,15 @@ test('Can parse text responses', async (t) => {
314314
test('Handle error empty responses', async (t) => {
315315
const accountId = uuidv4()
316316
const status = 404
317-
const scope = nock(origin).get(`${pathPrefix}/accounts/${accountId}`).reply(status)
317+
const expectedResponse = 'test'
318+
const scope = nock(origin).get(`${pathPrefix}/accounts/${accountId}`).reply(status, expectedResponse)
318319

319320
const client = getClient()
320321
const error = await t.throwsAsync(client.getAccount({ account_id: accountId }))
321322

322323
t.is(error.status, status)
323-
t.is(error.message, 'Not Found')
324-
t.is(error.data, '')
324+
t.is(error.message, expectedResponse)
325+
t.is(error.data, expectedResponse)
325326
t.true(error instanceof TextHTTPError)
326327
t.true(error.stack !== undefined)
327328
t.true(scope.isDone())
@@ -337,7 +338,7 @@ test('Handle error text responses', async (t) => {
337338
const error = await t.throwsAsync(client.getAccount({ account_id: accountId }))
338339

339340
t.is(error.status, status)
340-
t.is(error.message, 'Not Found')
341+
t.is(error.message, expectedResponse)
341342
t.is(error.data, expectedResponse)
342343
t.true(error instanceof TextHTTPError)
343344
t.true(error.stack !== undefined)
@@ -356,7 +357,7 @@ test('Handle error text responses on JSON endpoints', async (t) => {
356357
const error = await t.throwsAsync(client.getAccount({ account_id: accountId }))
357358

358359
t.is(error.status, status)
359-
t.is(error.message, 'Not Found')
360+
t.is(error.message, expectedResponse)
360361
t.is(error.data, expectedResponse)
361362
t.true(error instanceof TextHTTPError)
362363
t.true(error.stack !== undefined)
@@ -373,7 +374,7 @@ test('Handle error JSON responses', async (t) => {
373374
const error = await t.throwsAsync(client.getAccount({ account_id: accountId }))
374375

375376
t.is(error.status, status)
376-
t.is(error.message, 'Not Found')
377+
t.notThrows(() => JSON.parse(error.message))
377378
t.deepEqual(error.json, errorJson)
378379
t.true(error instanceof JSONHTTPError)
379380
t.true(error.stack !== undefined)
@@ -550,7 +551,7 @@ test('Gives up retrying on API rate limiting after a timeout', async (t) => {
550551
const error = await t.throwsAsync(client.getAccount({ account_id: accountId }))
551552

552553
t.is(error.status, 429)
553-
t.is(error.message, 'Too Many Requests')
554+
t.is(error.message, JSON.stringify({ retryAt }))
554555
t.true(Number.isInteger(error.json.retryAt))
555556

556557
t.false(scope.isDone())

src/methods/response.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const parseResponse = async function (response) {
1010

1111
if (!response.ok) {
1212
const ErrorType = responseType === 'json' ? JSONHTTPError : TextHTTPError
13-
throw new ErrorType(response, parsedResponse)
13+
throw addFallbackErrorMessage(new ErrorType(response, parsedResponse), textResponse)
1414
}
1515

1616
return parsedResponse
@@ -34,13 +34,20 @@ const parseJsonResponse = function (response, textResponse, responseType) {
3434
try {
3535
return JSON.parse(textResponse)
3636
} catch {
37-
throw new TextHTTPError(response, textResponse)
37+
throw addFallbackErrorMessage(new TextHTTPError(response, textResponse), textResponse)
3838
}
3939
}
4040

41+
const addFallbackErrorMessage = function (error, textResponse) {
42+
error.message = error.message || textResponse
43+
return error
44+
}
45+
4146
export const getFetchError = function (error, url, opts) {
4247
const data = omit.default(opts, ['Authorization'])
43-
error.name = 'FetchError'
48+
if (error.name !== 'FetchError') {
49+
error.name = 'FetchError'
50+
}
4451
error.url = url
4552
error.data = data
4653
return error

0 commit comments

Comments
 (0)