feat: add possibility to configure retrying of requests

This commit is contained in:
Karl-Philipp Wulfert
2019-11-18 15:00:10 +01:00
parent a64e975df1
commit 1796e88227

View File

@@ -61,14 +61,33 @@ export interface ApiRequestOptions {
* HTTP verb to use for the request
*/
method?: 'DELETE' | 'GET' | 'POST' | 'PUT';
/**
* Whether or not to retry on any error
*/
retryOnAnyError?: boolean;
/**
* Amount of tries
*/
tries?: number;
}
/**
* GitLab API get issues options
*/
export interface ApiGetIssuesOptions {
/**
* Filter issues by group ID
*/
groupId?: number;
/**
* Filter issues by milestone
*/
milestone?: 'Backlog' | 'No Milestone';
/**
* Filter issues by state
*/
state?: IssueState;
}
@@ -446,8 +465,11 @@ export class Api {
// remove leading slash
url = url.replace(/^\/+/g, '');
const options: ApiRequestOptions = {
const options: Required<ApiRequestOptions> = {
data: undefined,
method: 'GET',
retryOnAnyError: false,
tries: 5,
..._options,
};
@@ -472,7 +494,7 @@ export class Api {
let body;
let tries = 0;
while (typeof body === 'undefined' && tries++ < 5) {
while (typeof body === 'undefined' && tries++ < options.tries) {
try {
const requestUrl = url + concatenator + 'page=' + currentPage + '&per_page=100';
@@ -493,7 +515,7 @@ export class Api {
},
});
} catch (error) {
if (error.error === 'GitLab is not responding') {
if (error.error.includes('not responding') || options.retryOnAnyError) {
const seconds = 5;
logger.warn(`GitLab was not responding. Waiting ${seconds}s and retrying...`);
@@ -511,7 +533,7 @@ export class Api {
return;
}
if (Array.isArray(apiResult) && currentPage > 1) {
if (typeof apiResult !== 'undefined' && Array.isArray(apiResult) && currentPage > 1) {
// add items to previously fetched items
apiResult = apiResult.concat(body);
} else {