Skip to content

Commit

Permalink
Treat response.text() as a promise in error handling.
Browse files Browse the repository at this point in the history
We [recently added some better exception handling](#9) but unfortunately printing the response body didn't work 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.
  • Loading branch information
markdoliner-doma committed Jul 5, 2022
1 parent 9c02a7d commit 01802d2
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions packages/thrift-client/src/main/connections/HttpConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,20 @@ export class HttpConnection extends ThriftConnection<RequestOptions> {
if (shouldRetry(response, retry, this.withEndpointPerMethod)) {
resolve(this.write(dataToWrite, methodName, options, true))
} else if (isErrorResponse(response)) {
reject(
new ThriftRequestError(
`Thrift request failed: HTTP ${response.status} ${
response.statusText
}: ${response.text()}`,
response
const baseMessage = `Thrift request failed: HTTP ${response.status} ${response.statusText}`
response
.text()
.then(responseText =>
reject(new ThriftRequestError(`${baseMessage}: ${responseText}`, response))
)
)
.catch(reason => {
reject(
new ThriftRequestError(
`${baseMessage} and the response body could not be read: ${reason}`,
response
)
)
})
} else {
response.arrayBuffer().then(arrayBuffer =>
resolve({
Expand Down

0 comments on commit 01802d2

Please sign in to comment.