mirror of
https://github.com/bigchaindb/js-bigchaindb-driver.git
synced 2024-11-22 09:46:58 +01:00
fix: improve request errors
This commit is contained in:
parent
57a3e89871
commit
1f27cd2300
@ -2,19 +2,24 @@
|
|||||||
// SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0)
|
// SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0)
|
||||||
// Code is Apache-2.0 and docs are CC-BY-4.0
|
// Code is Apache-2.0 and docs are CC-BY-4.0
|
||||||
|
|
||||||
import {
|
import { Promise } from 'es6-promise'
|
||||||
Promise
|
|
||||||
} from 'es6-promise'
|
|
||||||
import fetchPonyfill from 'fetch-ponyfill'
|
import fetchPonyfill from 'fetch-ponyfill'
|
||||||
import {
|
import { vsprintf } from 'sprintf-js'
|
||||||
vsprintf
|
|
||||||
} from 'sprintf-js'
|
|
||||||
|
|
||||||
import formatText from './format_text'
|
import formatText from './format_text'
|
||||||
import stringifyAsQueryParam from './stringify_as_query_param'
|
import stringifyAsQueryParam from './stringify_as_query_param'
|
||||||
|
|
||||||
const fetch = fetchPonyfill(Promise)
|
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
|
* @private
|
||||||
@ -45,12 +50,7 @@ function handleResponse(res) {
|
|||||||
// If status is not a 2xx (based on Response.ok), assume it's an error
|
// 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
|
// See https://developer.mozilla.org/en-US/docs/Web/API/GlobalFetch/fetch
|
||||||
if (!(res && res.ok)) {
|
if (!(res && res.ok)) {
|
||||||
const errorObject = {
|
throw new ResponseError('HTTP Error: Requested page not reachable', `${res.status} ${res.statusText}`, res.url)
|
||||||
message: 'HTTP Error: Requested page not reachable',
|
|
||||||
status: `${res.status} ${res.statusText}`,
|
|
||||||
requestURI: res.url
|
|
||||||
}
|
|
||||||
throw errorObject
|
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
@ -46,28 +46,21 @@ export default class Transport {
|
|||||||
connection = this.pickConnection()
|
connection = this.pickConnection()
|
||||||
// Date in milliseconds
|
// Date in milliseconds
|
||||||
const startTime = Date.now()
|
const startTime = Date.now()
|
||||||
try {
|
// eslint-disable-next-line no-await-in-loop
|
||||||
// eslint-disable-next-line no-await-in-loop
|
response = await connection.request(
|
||||||
response = await connection.request(
|
path,
|
||||||
path,
|
headers,
|
||||||
headers,
|
this.timeout,
|
||||||
this.timeout,
|
this.maxBackoffTime
|
||||||
this.maxBackoffTime
|
)
|
||||||
)
|
const elapsed = Date.now() - startTime
|
||||||
const elapsed = Date.now() - startTime
|
if (connection.backoffTime > 0 && this.timeout > 0) {
|
||||||
if (connection.backoffTime > 0 && this.timeout > 0) {
|
this.timeout -= elapsed
|
||||||
this.timeout -= elapsed
|
} else {
|
||||||
} else {
|
// No connection error, the response is valid
|
||||||
// No connection error, the response is valid
|
return response
|
||||||
return response
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
throw err
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const errorObject = {
|
throw new Error('TimeoutError')
|
||||||
message: 'TimeoutError',
|
|
||||||
}
|
|
||||||
throw errorObject
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user