diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e5458ca4..54893cb32 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ 1. [#1005](https://github.com/influxdata/influxdb-client-js/pull/1005): Token stored as a private class property. 2. [#1019](https://github.com/influxdata/influxdb-client-js/pull/1019): Propagates headers from HTTP response when an error is returned from the server. +3. [#1020](https://github.com/influxdata/influxdb-client-js/pull/1020): Adds example of working with `HttpError` response. ### CI diff --git a/examples/README.md b/examples/README.md index 34e1d307b..69d222b4a 100644 --- a/examples/README.md +++ b/examples/README.md @@ -30,6 +30,8 @@ This directory contains javascript and typescript examples for node.js, browser, Shows how to configure the client to follow HTTP redirects. - [delete.ts](./delete.ts) Shows how to delete data from a bucket. + - [httpErrorHandled.mjs](./httpErrorHandled.mjs) + Shows handling HTTP Error response. - Browser examples - Change `token, org, bucket, username, password` variables in [./env_browser.mjs](env_browser.mjs) to match your InfluxDB instance - Run `npm run browser` diff --git a/examples/httpErrorHandled.mjs b/examples/httpErrorHandled.mjs new file mode 100755 index 000000000..265cf67f2 --- /dev/null +++ b/examples/httpErrorHandled.mjs @@ -0,0 +1,31 @@ +#!/usr/bin/env node +/////////////////////////////////////////////////////////// +// Shows how to handle InfluxDB write API - HTTP Errors. // +// To be run against cloud account // +/////////////////////////////////////////////////////////// + +import {InfluxDB} from '@influxdata/influxdb-client' +import {url, token, org, bucket} from './env.mjs' + +console.log('*** FORCE AND HANDLE HTTP ERROR ***') + +const writeApi = new InfluxDB({url, token}).getWriteApi(org, bucket, 'ns') + +try { + await writeApi.writeRecord('asdf') //invalid lineprotocol + await writeApi.close() +} catch (e) { + if (e.statusCode !== 400) { + throw new Error(`Expected HTTP 400 but received ${e.statusCode}`) + } + console.log() + console.log('Handle HTTP Error') + console.log(`Caught expected HTTP ${e.statusCode}`) + console.log(` At: ${e.headers.date}`) + console.log(` During request: ${e.headers['x-influxdb-request-id']}`) + console.log(` On build: ${e.headers['x-influxdb-build']}`) + console.log(` Traceable: ${e.headers['trace-id']}`) + console.log( + ` Can retry: ${e._retryAfter === 0 ? false : e._retryAfter}` + ) +} diff --git a/examples/package.json b/examples/package.json index b811d91cd..f01f6b937 100644 --- a/examples/package.json +++ b/examples/package.json @@ -9,8 +9,8 @@ "esr": "esr" }, "dependencies": { - "@influxdata/influxdb-client": "*", - "@influxdata/influxdb-client-apis": "*" + "@influxdata/influxdb-client": "link:../packages/core", + "@influxdata/influxdb-client-apis": "link:../packages/apis" }, "devDependencies": { "@types/express": "^4.17.2", diff --git a/packages/core/test/unit/WriteApi.test.ts b/packages/core/test/unit/WriteApi.test.ts index fe30bd95b..83b0597d0 100644 --- a/packages/core/test/unit/WriteApi.test.ts +++ b/packages/core/test/unit/WriteApi.test.ts @@ -680,6 +680,37 @@ describe('WriteApi', () => { expect(logs.warn).deep.equals([]) expect(authorization).equals(`Token customToken`) }) + it('handles HTTP Header in error', async () => { + useSubject({}) + nock(clientOptions.url) + .post(WRITE_PATH_NS) + .reply(function (_uri, _requestBody) { + return [ + 429, + '{ "message": "see status code" }', + { + 'retry-after': '60', + 'trace-id': '123456789ABCDEF', + 'content-type': 'application/json', + }, + ] + }) + .persist() + try { + subject.writePoint(new Point('test').floatField('value', 1)) + await subject.close() + } catch (e: any) { + expect(logs.error).has.length(1) + expect(logs.warn).has.length(1) + expect(e instanceof HttpError).to.be.true + expect(e._retryAfter).to.equal(60) + expect(Object.getOwnPropertyNames(e.headers)).to.have.length(3) + expect(e.headers['retry-after']).to.equal('60') + expect(e.headers['trace-id']).to.equal('123456789ABCDEF') + expect(e.headers['content-type']).to.equal('application/json') + expect(JSON.parse(e.body).message).to.equal('see status code') + } + }) it('sends consistency param when specified', async () => { useSubject({ consistency: 'quorum',