diff --git a/package.json b/package.json index 18866ab0..9d0d827b 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "bootstrap": "lerna bootstrap", "clean": "lerna clean", "lint": "lerna run lint --stream", + "postinstall": "npm run bootstrap", "publish": "lerna publish", "test": "lerna run test --stream", "version": "conventional-changelog --pkg lerna.json -i CHANGELOG.md -s && git add CHANGELOG.md" diff --git a/packages/api/__tests__/index.test.js b/packages/api/__tests__/index.test.js index f4fb8544..062386a0 100644 --- a/packages/api/__tests__/index.test.js +++ b/packages/api/__tests__/index.test.js @@ -147,6 +147,25 @@ describe('#accessors', () => { describe('#fetch', () => { const petId = 123; + it('should reject for error-level status codes', () => { + expect.assertions(2); + + const response = { + error: 'ENDPOINT_NOTFOUND', + message: `The endpoint you called (GET /pets/${petId}) doesn't exist`, + }; + + const mock = nock(petstoreServerUrl).delete(`/pets/${petId}`).reply(404, response); + + return petstoreSdk.deletePet({ id: petId }).catch(async err => { + expect(err.status).toBe(404); + + const json = await err.json(); + expect(json).toStrictEqual(response); + mock.done(); + }); + }); + it('should contain a custom user agent for the library in requests', () => { expect.assertions(2); diff --git a/packages/api/src/index.js b/packages/api/src/index.js index 3ee41509..85e080e0 100644 --- a/packages/api/src/index.js +++ b/packages/api/src/index.js @@ -37,7 +37,13 @@ class Sdk { function fetchOperation(spec, operation, body, metadata) { const har = oasToHar(spec, operation, prepareParams(operation, body, metadata), prepareAuth(authKeys, operation)); - return fetchHar(har, `${pkg.name} (node)/${pkg.version}`); + return fetchHar(har, `${pkg.name} (node)/${pkg.version}`).then(res => { + if (res.status >= 400 && res.status <= 599) { + throw res; + } + + return res; + }); } function loadMethods(spec) {