Skip to content

Commit b4a4751

Browse files
Treat response.text() as a promise in error handling. (#11)
We [recently added some better exception handling](#9) but unfortunately printing the response body isn't working because [`response.text()` is a Promise](https://github.github.io/fetch/#response-body). The message looks like this: `ThriftRequestError: Thrift request failed: HTTP 503 Service Unavailable: [object Promise]` The fix is to use `.then()` to get the body. I have not tested this. I did run `npm run test` and some of the tests pass, but 75 out of 108 thrift-integration tests fail. I'm seeing this error a lot: ``` request to http://localhost:8090/thrift failed, reason: connect ECONNREFUSED ::1:8090 ``` I think it also might have been happening before this change. It looks unrelated. It seems like it's happening because [Hapi listens on 127.0.0.1](https://hapi.dev/api/?v=20.2.2#-serveroptionsaddress) and [Node v17 and higher stopped forcing IPv64addresses to be listed first when resolving domains](nodejs/node#40537 (comment)) so now IPv6 addresses can be returned first. I'll probably post a separate PR for that, but for now I'd prefer not to block this change.
1 parent 9c02a7d commit b4a4751

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

packages/thrift-client/src/main/connections/HttpConnection.ts

+13-7
Original file line numberDiff line numberDiff line change
@@ -207,14 +207,20 @@ export class HttpConnection extends ThriftConnection<RequestOptions> {
207207
if (shouldRetry(response, retry, this.withEndpointPerMethod)) {
208208
resolve(this.write(dataToWrite, methodName, options, true))
209209
} else if (isErrorResponse(response)) {
210-
reject(
211-
new ThriftRequestError(
212-
`Thrift request failed: HTTP ${response.status} ${
213-
response.statusText
214-
}: ${response.text()}`,
215-
response
210+
const baseMessage = `Thrift request failed: HTTP ${response.status} ${response.statusText}`
211+
response
212+
.text()
213+
.then(responseText =>
214+
reject(new ThriftRequestError(`${baseMessage}: ${responseText}`, response))
216215
)
217-
)
216+
.catch(reason => {
217+
reject(
218+
new ThriftRequestError(
219+
`${baseMessage} and the response body could not be read: ${reason}`,
220+
response
221+
)
222+
)
223+
})
218224
} else {
219225
response.arrayBuffer().then(arrayBuffer =>
220226
resolve({

0 commit comments

Comments
 (0)