Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: adds httpErrorHandled example and integration test for HttpError #1020

Merged
merged 2 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
31 changes: 31 additions & 0 deletions examples/httpErrorHandled.mjs
Original file line number Diff line number Diff line change
@@ -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}`
)
}
4 changes: 2 additions & 2 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
31 changes: 31 additions & 0 deletions packages/core/test/unit/WriteApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down