diff --git a/package-lock.json b/package-lock.json index a99427b12..cb5b56979 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7278,7 +7278,7 @@ "lunr": { "version": "2.3.9", "resolved": "https://registry.npmjs.org/lunr/-/lunr-2.3.9.tgz", - "integrity": "sha1-GLEjFCgyM33W6WTfGlp3B7JdNeE=", + "integrity": "sha512-zTU3DaZaF3Rt9rhN3uBMGQD3dD2/vFQqnvZCDv4dl5iOzq2IZQqTxu90r4E5J+nP70J3ilqVCrbho2eWaeW8Ow==", "dev": true }, "make-dir": { diff --git a/src/http-client.ts b/src/http-client.ts index 82f32aa09..1d3de2104 100644 --- a/src/http-client.ts +++ b/src/http-client.ts @@ -104,23 +104,6 @@ export class HttpClient { ) } - // We don't have a body, so it's an unpredictable error, but let's try to structure it - // anyways for completeness sake - if (badRequest && !data) { - throw apiErrorFactory( - { - code: status.toString(), - details: res.statusText, - }, - headers, - ) - } - - // should not happen, but a catch all just in case - if (badRequest) { - throw new InvalidProgramStateError(data) - } - if (status < this.httpStatus.MULTIPLE_CHOICES && data) { return data } diff --git a/test/__recordings__/HttpClient_3032634686/get_1410115415/should-throw-an-exception-base-on-http-status-when-request-return-object-without-code-_3353912599/recording.har b/test/__recordings__/HttpClient_3032634686/get_1410115415/should-throw-an-exception-base-on-http-status-when-request-return-object-without-code-_3353912599/recording.har new file mode 100644 index 000000000..e06a8c6ed --- /dev/null +++ b/test/__recordings__/HttpClient_3032634686/get_1410115415/should-throw-an-exception-base-on-http-status-when-request-return-object-without-code-_3353912599/recording.har @@ -0,0 +1,115 @@ +{ + "log": { + "_recordingName": "HttpClient/get/should throw an exception base on http status when request return object without code ", + "creator": { + "comment": "persister:fs", + "name": "Polly.JS", + "version": "5.1.0" + }, + "entries": [ + { + "_id": "2da808cdd3d89cb18c7128a19ffd1e4d", + "_order": 0, + "cache": {}, + "request": { + "bodySize": 0, + "cookies": [], + "headers": [ + { + "name": "accept", + "value": "application/json" + }, + { + "name": "content-type", + "value": "application/json" + }, + { + "name": "user-agent", + "value": "@scaleleap/amazon-advertising-api-sdk/0.0.0" + }, + { + "name": "amazon-advertising-api-scope", + "value": "-1" + }, + { + "name": "bidding_controls_on", + "value": "true" + }, + { + "name": "accept-encoding", + "value": "application/json" + }, + { + "name": "host", + "value": "advertising-api-test.amazon.com" + } + ], + "headersSize": 372, + "httpVersion": "HTTP/1.1", + "method": "GET", + "queryString": [], + "url": "https://advertising-api-test.amazon.com/foobar" + }, + "response": { + "bodySize": 153, + "content": { + "mimeType": "application/json; charset=utf-8", + "size": 153, + "text": { + "details": "Request was rate-limited. Retry later." + } + }, + "cookies": [], + "headers": [ + { + "name": "server", + "value": "Server" + }, + { + "name": "date", + "value": "Thu, 14 Jan 2021 15:33:08 GMT" + }, + { + "name": "content-type", + "value": "application/json; charset=utf-8" + }, + { + "name": "content-length", + "value": "153" + }, + { + "name": "connection", + "value": "close" + }, + { + "name": "x-amz-request-id", + "value": "3323JGVBWRRQMXR22A31" + }, + { + "name": "vary", + "value": "Content-Type,Accept-Encoding,X-Amzn-CDN-Cache,X-Amzn-AX-Treatment,User-Agent" + } + ], + "headersSize": 299, + "httpVersion": "HTTP/1.1", + "redirectURL": "", + "status": 429, + "statusText": "OK" + }, + "startedDateTime": "2021-01-14T15:33:07.160Z", + "time": 1554, + "timings": { + "blocked": -1, + "connect": -1, + "dns": -1, + "receive": 0, + "send": 0, + "ssl": -1, + "wait": 1554 + } + } + ], + "pages": [], + "version": "1.2" + } +} diff --git a/test/http-client.test.ts b/test/http-client.test.ts index d4e6a0571..8d61f05dd 100644 --- a/test/http-client.test.ts +++ b/test/http-client.test.ts @@ -76,6 +76,22 @@ describe('HttpClient', () => { expect(err.retryAfter).toBe(42) }) }) + + it('should throw an exception base on http status when request return object without code ', async () => { + const server = jestPollyContext.polly.server + const details = 'Request was rate-limited. Retry later.' + + server.get(SANDBOX_URI + '/foobar').on('beforeResponse', (req, res) => { + res.status(HttpStatus.TOO_MANY_REQUESTS) + res.send({ + details, + }) + }) + + await client.get('foobar').catch((err: ThrottlingError) => { + expect(err.message).toContain(details) + }) + }) }) describe('download', () => {