js-bigchaindb-driver
js-bigchaindb-driver copied to clipboard
For http requests, return the original response to the caller, not just 400
Currently the calls to requests, such as postTransactionCommit(createTranfer), when error happens, the driver returns an error that just includes blanket 400 response, which is not useful.

In many cases, the server, even though it returns a status code of 400, it also returns a valid response body that will have much more meaningful information to the caller, which is not being supplied to the caller.

Instead of returning blanket 400 to the caller, return the original response also to the caller so that they can either log it or display it to the user.
Expected: the values sent to the promise catch handler should include detailed server response, and not just explanation of 400
conn.postTransactionCommit(createTranfer)
.then(res => {
//...
})
.catch(err => {
// Here the err should somehow include the original response from the server.
// Currently it just says 400 and explanation of 400, which is practically useless
})
The code at https://github.com/bigchaindb/js-bigchaindb-driver/blob/master/src/baseRequest.js#L47 creates a new error object. Instead it should either throw the existing object directly, or add it to the throwing object as below:
function handleResponse(res) {
if (!(res && res.ok)) {
throw res; //<-- throw the response object as is. Callers know how to handle fetch errors
}
return res
}
or, else:
function handleResponse(res) {
if (!(res && res.ok)) {
const errorObject = {
message: 'HTTP Error: Requested page not reachable',
status: `${res.status} ${res.statusText}`,
requestURI: res.url,
response: res //<--- add the original error response here
}
throw errorObject
}
return res
}
This is really biting hard in the development, since we are almost blind as to why the server is not accepting the post transaction, and we have to manually check the post body externally every time there is a failure, which is very tedious.