Skip to content

Commit

Permalink
fix(node errors): construct error message by server response
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Oct 29, 2021
1 parent 8621352 commit d556936
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,15 @@ const Node = AsyncInit.compose({
if (!url) throw new Error('"url" required')
this.url = url.replace(/\/$/, '')
this.internalUrl = internalUrl ? internalUrl.replace(/\/$/, '') : this.url
const client = await genSwaggerClient(`${this.url}/api?oas3`, { internalUrl: this.internalUrl })
const client = await genSwaggerClient(`${this.url}/api?oas3`, {
internalUrl: this.internalUrl,
responseInterceptor: response => {
if (response.ok) return
return Object.assign(response, {
statusText: `${new URL(response.url).pathname.slice(1)} error: ` + response.body.reason
})
}
})
this.version = client.spec.info.version
if (
!semverSatisfies(this.version, NODE_GE_VERSION, NODE_LT_VERSION) &&
Expand Down
5 changes: 5 additions & 0 deletions test/integration/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ describe('Node client', function () {
)
})

it('throws clear exceptions when can\'t get transaction by hash', async () => {
await expect(node.api.getTransactionByHash('th_test'))
.to.be.rejectedWith('v3/transactions/th_test error: Invalid hash')
})

describe('Node Pool', () => {
it('throw error on invalid node object', async () => {
const nodes = await NodePool()
Expand Down

0 comments on commit d556936

Please sign in to comment.