Skip to content

Commit

Permalink
Allow AbortController in get requests, rethrow cancel error
Browse files Browse the repository at this point in the history
  • Loading branch information
m-mohr committed Jan 9, 2024
1 parent ed5eee6 commit 16d183d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
3 changes: 2 additions & 1 deletion openeo.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2400,11 +2400,12 @@ declare module OpenEO {
* @param {string} path
* @param {object.<string, *>} query
* @param {string} responseType - Response type according to axios, defaults to `json`.
* @param {?AbortController} [abortController=null] - An AbortController object that can be used to cancel the request.
* @returns {Promise<AxiosResponse>}
* @throws {Error}
* @see https://github.com/axios/axios#request-config
*/
protected _get(path: string, query: object<string, any>, responseType: string): Promise<AxiosResponse>;
protected _get(path: string, query: object<string, any>, responseType: string, abortController?: AbortController | null): Promise<AxiosResponse>;
/**
* Sends a POST request.
*
Expand Down
8 changes: 6 additions & 2 deletions src/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -1104,11 +1104,12 @@ class Connection {
* @param {string} path
* @param {object.<string, *>} query
* @param {string} responseType - Response type according to axios, defaults to `json`.
* @param {?AbortController} [abortController=null] - An AbortController object that can be used to cancel the request.
* @returns {Promise<AxiosResponse>}
* @throws {Error}
* @see https://github.com/axios/axios#request-config
*/
async _get(path, query, responseType) {
async _get(path, query, responseType, abortController = null) {
return await this._send({
method: 'get',
responseType: responseType,
Expand All @@ -1117,7 +1118,7 @@ class Connection {
// Without timeout connecting with a wrong server url may take forever.
timeout: path === '/' ? 5000 : 0,
params: query
});
}, abortController);
}

/**
Expand Down Expand Up @@ -1272,6 +1273,9 @@ class Connection {
}
return response;
} catch(error) {
if (axios.isCancel(error)) {
throw error;
}
const checkContentType = type => (typeof type === 'string' && type.indexOf('/json') !== -1);
const enrichError = (origin, response) => {
if (typeof response.message === 'string') {
Expand Down

0 comments on commit 16d183d

Please sign in to comment.