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:
parent
597ac56f1f
commit
656de69c64
@ -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
|
||||
}
|
||||
|
@ -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')
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user