1
0
mirror of https://github.com/bigchaindb/js-bigchaindb-driver.git synced 2024-12-27 07:07:50 +01:00

fix: improve request errors

Signed-off-by: getlarge <ed@getlarge.eu>
This commit is contained in:
getlarge 2021-03-09 07:52:03 +01:00
parent 597ac56f1f
commit 656de69c64
No known key found for this signature in database
GPG Key ID: E4E13243600F9566
2 changed files with 26 additions and 33 deletions

View File

@ -2,19 +2,24 @@
// SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0)
// Code is Apache-2.0 and docs are CC-BY-4.0
import {
Promise
} from 'es6-promise'
import { Promise } from 'es6-promise'
import fetchPonyfill from 'fetch-ponyfill'
import {
vsprintf
} from 'sprintf-js'
import { vsprintf } from 'sprintf-js'
import formatText from './format_text'
import stringifyAsQueryParam from './stringify_as_query_param'
const fetch = fetchPonyfill(Promise)
export function ResponseError(message, status, requestURI) {
this.name = 'ResponseError'
this.message = message
this.status = status
this.requestURI = requestURI
this.stack = (new Error()).stack
}
ResponseError.prototype = new Error;
/**
* @private
@ -45,12 +50,7 @@ function handleResponse(res) {
// If status is not a 2xx (based on Response.ok), assume it's an error
// See https://developer.mozilla.org/en-US/docs/Web/API/GlobalFetch/fetch
if (!(res && res.ok)) {
const errorObject = {
message: 'HTTP Error: Requested page not reachable',
status: `${res.status} ${res.statusText}`,
requestURI: res.url
}
throw errorObject
throw new ResponseError('HTTP Error: Requested page not reachable', `${res.status} ${res.statusText}`, res.url)
}
return res
}

View File

@ -46,28 +46,21 @@ export default class Transport {
connection = this.pickConnection()
// Date in milliseconds
const startTime = Date.now()
try {
// eslint-disable-next-line no-await-in-loop
response = await connection.request(
path,
headers,
this.timeout,
this.maxBackoffTime
)
const elapsed = Date.now() - startTime
if (connection.backoffTime > 0 && this.timeout > 0) {
this.timeout -= elapsed
} else {
// No connection error, the response is valid
return response
}
} catch (err) {
throw err
// eslint-disable-next-line no-await-in-loop
response = await connection.request(
path,
headers,
this.timeout,
this.maxBackoffTime
)
const elapsed = Date.now() - startTime
if (connection.backoffTime > 0 && this.timeout > 0) {
this.timeout -= elapsed
} else {
// No connection error, the response is valid
return response
}
}
const errorObject = {
message: 'TimeoutError',
}
throw errorObject
throw new Error('TimeoutError')
}
}