Skip to content

Commit

Permalink
feat: add headers to http error responses (#1019)
Browse files Browse the repository at this point in the history
  • Loading branch information
twosdai authored Jul 25, 2024
1 parent e65b8fb commit 8cfa06c
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
### Features:

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.

### CI

Expand Down
10 changes: 9 additions & 1 deletion packages/core/src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {HttpHeaders} from './results'

/**
* Strategy for calculating retry delays.
*/
Expand Down Expand Up @@ -54,17 +56,23 @@ export class HttpError extends Error implements RetriableDecision {
/** json error response */
public json: any

public headers?: HttpHeaders | undefined

/* istanbul ignore next because of super() not being covered*/
constructor(
readonly statusCode: number,
readonly statusMessage: string | undefined,
readonly body?: string,
retryAfter?: string | undefined | null,
readonly contentType?: string | undefined | null,
message?: string
message?: string,
headers?: HttpHeaders | undefined
) {
super()
Object.setPrototypeOf(this, HttpError.prototype)

this.headers = headers

if (message) {
this.message = message
} else if (body) {
Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/impl/WriteApiImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,11 @@ export default class WriteApiImpl implements WriteApi {
}
return new Promise<void>((resolve, reject) => {
let responseStatusCode: number | undefined
let headers: Headers | undefined
const callbacks = {
responseStarted(_headers: Headers, statusCode?: number): void {
responseStatusCode = statusCode
headers = _headers
},
error(error: Error): void {
// call the writeFailed listener and check if we can retry
Expand Down Expand Up @@ -245,7 +247,10 @@ export default class WriteApiImpl implements WriteApi {
responseStatusCode,
message,
undefined,
'0'
'0',
undefined,
undefined,
headers
)
error.message = message
callbacks.error(error)
Expand Down
9 changes: 7 additions & 2 deletions packages/core/src/impl/browser/FetchTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,20 +153,25 @@ export default class FetchTransport implements Transport {
}
} catch (e) {
Log.warn('Unable to receive error body', e)

throw new HttpError(
response.status,
response.statusText,
undefined,
response.headers.get('retry-after'),
response.headers.get('content-type')
response.headers.get('content-type'),
undefined,
getResponseHeaders(response)
)
}
throw new HttpError(
response.status,
response.statusText,
text,
response.headers.get('retry-after'),
response.headers.get('content-type')
response.headers.get('content-type'),
undefined,
getResponseHeaders(response)
)
}
}
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/impl/node/NodeHttpTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,9 @@ export class NodeHttpTransport implements Transport {
res.statusMessage,
body,
res.headers['retry-after'],
res.headers['content-type']
res.headers['content-type'],
undefined,
res.headers
)
)
})
Expand Down
7 changes: 7 additions & 0 deletions packages/core/test/unit/errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ describe('errors', () => {
expect(new RequestTimedOutError().message).is.not.empty
expect(new AbortError().message).is.not.empty
})
describe('HttpError Headers Property is defined', () => {
expect(
new HttpError(200, 'OK', 'body', '10', 'text/plain', 'message', {
header: 'value',
}).headers
).is.deep.equal({header: 'value'})
})
describe('retriable errors', () => {
const testSetOK = [
new HttpError(503, 'Service Unavailable'),
Expand Down

0 comments on commit 8cfa06c

Please sign in to comment.