|
1 | 1 | const test = require('ava') |
2 | 2 | const nock = require('nock') |
3 | 3 | const fromString = require('from2-string') |
4 | | -const { TextHTTPError } = require('micro-api-client') |
| 4 | +const { TextHTTPError, JSONHTTPError } = require('micro-api-client') |
5 | 5 |
|
6 | 6 | const NetlifyAPI = require('./index') |
7 | 7 |
|
@@ -310,7 +310,7 @@ test('Can parse text responses', async t => { |
310 | 310 | t.true(scope.isDone()) |
311 | 311 | }) |
312 | 312 |
|
313 | | -test('Handle error JSON responses', async t => { |
| 313 | +test('Handle error empty responses', async t => { |
314 | 314 | const account_id = '8' |
315 | 315 | const status = 404 |
316 | 316 | const scope = nock(origin) |
@@ -347,6 +347,44 @@ test('Handle error text responses', async t => { |
347 | 347 | t.true(scope.isDone()) |
348 | 348 | }) |
349 | 349 |
|
| 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 | + |
350 | 388 | test('Handle network errors', async t => { |
351 | 389 | const account_id = '10' |
352 | 390 | const expectedResponse = 'test' |
|
0 commit comments