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

Commit 85509b9

Browse files
authored
Fix error responses when the response is not JSON (#85)
Fix error responses when the response is not JSON
2 parents 40d4590 + 9836831 commit 85509b9

File tree

2 files changed

+59
-6
lines changed

2 files changed

+59
-6
lines changed

src/index.test.js

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const test = require('ava')
22
const nock = require('nock')
33
const fromString = require('from2-string')
4-
const { TextHTTPError } = require('micro-api-client')
4+
const { TextHTTPError, JSONHTTPError } = require('micro-api-client')
55

66
const NetlifyAPI = require('./index')
77

@@ -310,7 +310,7 @@ test('Can parse text responses', async t => {
310310
t.true(scope.isDone())
311311
})
312312

313-
test('Handle error JSON responses', async t => {
313+
test('Handle error empty responses', async t => {
314314
const account_id = '8'
315315
const status = 404
316316
const scope = nock(origin)
@@ -347,6 +347,44 @@ test('Handle error text responses', async t => {
347347
t.true(scope.isDone())
348348
})
349349

350+
test('Handle error text responses on JSON endpoints', async t => {
351+
const account_id = '9'
352+
const status = 404
353+
const expectedResponse = 'test'
354+
const scope = nock(origin)
355+
.get(`${pathPrefix}/accounts/${account_id}`)
356+
.reply(status, expectedResponse, { 'Content-Type': 'application/json' })
357+
358+
const client = getClient()
359+
const error = await t.throwsAsync(client.getAccount({ account_id }))
360+
361+
t.is(error.status, status)
362+
t.is(error.message, 'Not Found')
363+
t.is(error.data, expectedResponse)
364+
t.true(error instanceof TextHTTPError)
365+
t.true(error.stack !== undefined)
366+
t.true(scope.isDone())
367+
})
368+
369+
test('Handle error JSON responses', async t => {
370+
const account_id = '8'
371+
const status = 404
372+
const errorJson = { error: true }
373+
const scope = nock(origin)
374+
.get(`${pathPrefix}/accounts/${account_id}`)
375+
.reply(status, errorJson)
376+
377+
const client = getClient()
378+
const error = await t.throwsAsync(client.getAccount({ account_id }))
379+
380+
t.is(error.status, status)
381+
t.is(error.message, 'Not Found')
382+
t.deepEqual(error.json, errorJson)
383+
t.true(error instanceof JSONHTTPError)
384+
t.true(error.stack !== undefined)
385+
t.true(scope.isDone())
386+
})
387+
350388
test('Handle network errors', async t => {
351389
const account_id = '10'
352390
const expectedResponse = 'test'

src/methods/response.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ const { JSONHTTPError, TextHTTPError } = require('micro-api-client')
22

33
// Read and parse the HTTP response
44
const parseResponse = async function(response) {
5-
const { method, ErrorType } = getResponseType(response)
6-
const parsedResponse = await response[method]()
5+
const responseType = getResponseType(response)
6+
const textResponse = await response.text()
7+
8+
const parsedResponse = parseJsonResponse(response, textResponse, responseType)
79

810
if (!response.ok) {
11+
const ErrorType = responseType === 'json' ? JSONHTTPError : TextHTTPError
912
throw new ErrorType(response, parsedResponse)
1013
}
1114

@@ -16,10 +19,22 @@ const getResponseType = function({ headers }) {
1619
const contentType = headers.get('Content-Type')
1720

1821
if (contentType != null && contentType.includes('json')) {
19-
return { method: 'json', ErrorType: JSONHTTPError }
22+
return 'json'
23+
}
24+
25+
return 'text'
26+
}
27+
28+
const parseJsonResponse = function(response, textResponse, responseType) {
29+
if (responseType === 'text') {
30+
return textResponse
2031
}
2132

22-
return { method: 'text', ErrorType: TextHTTPError }
33+
try {
34+
return JSON.parse(textResponse)
35+
} catch (error) {
36+
throw new TextHTTPError(response, textResponse)
37+
}
2338
}
2439

2540
const getFetchError = function(error, url, opts) {

0 commit comments

Comments
 (0)