Skip to content

Commit

Permalink
Fix HTTP request error handling, v1.3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
m-mohr committed May 27, 2021
1 parent 55ddeab commit 51d8c05
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 15 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.3.2] - 2021-05-27

### Fixed

- Fixed error handling for HTTP requests

## [1.3.1] - 2021-04-29

### Fixed
Expand Down Expand Up @@ -80,7 +86,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

All prior releases have been documented in the [GitHub Releases](https://github.com/Open-EO/openeo-js-client/releases).

[Unreleased]: https://github.com/olivierlacan/keep-a-changelog/compare/v1.3.1...HEAD
[Unreleased]: https://github.com/olivierlacan/keep-a-changelog/compare/v1.3.2...HEAD
[1.3.2]: https://github.com/olivierlacan/keep-a-changelog/compare/v1.3.1...v1.3.2
[1.3.1]: https://github.com/olivierlacan/keep-a-changelog/compare/v1.3.0...v1.3.1
[1.3.0]: https://github.com/olivierlacan/keep-a-changelog/compare/v1.2.0...v1.3.0
[1.2.0]: https://github.com/olivierlacan/keep-a-changelog/compare/v1.1.0...v1.2.0
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

JavaScript/TypeScript client for the openEO API.

* [Documentation](https://open-eo.github.io/openeo-js-client/1.3.1/).
* [Documentation](https://open-eo.github.io/openeo-js-client/1.3.2/).

The version of this client is **1.3.1** and supports **openEO API versions 1.x.x**.
The version of this client is **1.3.2** and supports **openEO API versions 1.x.x**.
Legacy versions are available as releases.
See the [CHANGELOG](CHANGELOG.md) for recent changes.

Expand Down Expand Up @@ -53,7 +53,7 @@ In Node.js:
In Typescript:
* [Basic Discovery (promises)](examples/typescript/discovery.ts)

More information can be found in the [documentation](https://open-eo.github.io/openeo-js-client/1.3.1/).
More information can be found in the [documentation](https://open-eo.github.io/openeo-js-client/1.3.2/).

## Development

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openeo/js-client",
"version": "1.3.1",
"version": "1.3.2",
"author": "openEO Consortium",
"contributors": [
{
Expand Down
4 changes: 2 additions & 2 deletions src/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Environment {
* @returns {Promise<void>}
*/
static handleErrorResponse(error) {
return new Promise((_, reject) => {
return new Promise((resolve, reject) => {
let fileReader = new FileReader();
fileReader.onerror = event => {
fileReader.abort();
Expand All @@ -36,7 +36,7 @@ class Environment {
// ArrayBuffer to String conversion is from https://developers.google.com/web/updates/2012/06/How-to-convert-ArrayBuffer-to-and-from-String
let res = fileReader.result instanceof ArrayBuffer ? String.fromCharCode.apply(null, new Uint16Array(fileReader.result)) : fileReader.result;
let obj = typeof res === 'string' ? JSON.parse(res) : res;
reject(obj);
resolve(obj);
};
fileReader.readAsText(error.response.data);
});
Expand Down
27 changes: 22 additions & 5 deletions src/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -943,14 +943,31 @@ class Connection {
try {
return await axios(options);
} catch(error) {
if (Utils.isObject(error.response) && Utils.isObject(error.response.data) && ((typeof error.response.data.type === 'string' && error.response.data.type.indexOf('/json') !== -1) || (Utils.isObject(error.response.data.headers) && typeof error.response.data.headers['content-type'] === 'string' && error.response.data.headers['content-type'].indexOf('/json') !== -1))) {
const checkContentType = type => (typeof type === 'string' && type.indexOf('/json') !== -1);
const enrichError = (origin, response) => {
if (typeof response.message === 'string') {
origin.message = response.message;
}
origin.code = typeof response.code === 'string' ? response.code : "";
origin.id = response.id;
origin.links = Array.isArray(response.links) ? response.links : [];
return origin;
};
if (Utils.isObject(error.response) && Utils.isObject(error.response.data) && (checkContentType(error.response.data.type) || (Utils.isObject(error.response.headers) && checkContentType(error.response.headers['content-type'])))) {
// JSON error responses are Blobs and streams if responseType is set as such, so convert to JSON if required.
// See: https://github.com/axios/axios/issues/815
if (options.responseType === Environment.getResponseType()) {
// JSON error responses are Blobs and streams if responseType is set as such, so convert to JSON if required.
// See: https://github.com/axios/axios/issues/815
return Environment.handleErrorResponse(error);
try {
let errorResponse = await Environment.handleErrorResponse(error);
throw enrichError(error, errorResponse);
} catch (error2) {
console.error(error2);
}
}
else {
throw enrichError(error, error.response.data);
}
}
// Re-throw error if it was not handled yet.
throw error;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ class Environment {
* @returns {Promise<void>}
*/
static handleErrorResponse(error) {
return new Promise((_, reject) => {
return new Promise((resolve, reject) => {
let chunks = [];
error.response.data.on("data", chunk => chunks.push(chunk));
error.response.data.on("error", streamError => reject(streamError));
error.response.data.on("end", () => reject(JSON.parse(Buffer.concat(chunks).toString())));
error.response.data.on("end", () => resolve(JSON.parse(Buffer.concat(chunks).toString())));
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/openeo.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class OpenEO {
* @returns {string} Version number (according to SemVer).
*/
static clientVersion() {
return "1.3.1";
return "1.3.2";
}

}
Expand Down

0 comments on commit 51d8c05

Please sign in to comment.