Skip to content

Commit

Permalink
fix: resolve unhandled rejection promise
Browse files Browse the repository at this point in the history
  • Loading branch information
kkhanhluu committed Jul 23, 2021
1 parent d5c7c04 commit 4a6670b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 55 deletions.
63 changes: 50 additions & 13 deletions src/base/Requestable.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import axios, { AxiosResponse } from 'axios';
import Bottleneck from 'bottleneck';
import { log } from '../logger';
import { catchAsyncAxios } from '../utils/catchAsyncAxios';
import { EasybillError } from './EasybillError';

const limiter = new Bottleneck({
Expand All @@ -26,7 +25,7 @@ function handleOnLimiterFailed(
message: `Reason: "${error.message}". Retrying job ${jobInfo.options.id}`,
label: 'requestable',
});
// retry maximal 6 times. The delay between each retry is set to double (starting at 10s) with each attempt, but not exceed 60 seconds.
// retry maximal 10 times. The delay between each retry is set to double (starting at 10s) with each attempt, but not exceed 60 seconds.
return Math.min(5000 * 2 ** (jobInfo.retryCount + 1), 60000);
}
return null;
Expand Down Expand Up @@ -59,18 +58,56 @@ export class Requestable {
}): Promise<T> {
const { method, url, params, data, headers } = config;

const promise = catchAsyncAxios<T>(
this.axiosInstance.request<T, AxiosResponse<T>>({
method,
url,
data,
params,
headers,
cancelToken: this.axiosCancelTokenSource.token,
}),
);
return limiter.schedule(async () => {
try {
const res = await this.axiosInstance.request<T, AxiosResponse<T>>({
method,
url,
data,
params,
headers,
cancelToken: this.axiosCancelTokenSource.token,
});
return res.data;
} catch (error) {
// The request was made and the server responded with a status code
if (error.response) {
log({
level: 'error',
message: JSON.stringify(
{
data: error.response.data,
statusCode: error.response.status,
headers: error.response.headers,
},
null,
3,
),
label: 'EasybillAPI',
});
} else if (error.request) {
// The request was made but no response is received. Request is an instance of http.ClientRequest
log({
level: 'error',
message: JSON.stringify(
{
request: error.request,
},
null,
3,
),
label: 'EasybillAPI',
});
}

return limiter.schedule(() => promise);
throw new EasybillError(
error.message,
error.response?.status,
error.response?.statusText,
error,
);
}
});
}

/**
Expand Down
41 changes: 0 additions & 41 deletions src/utils/catchAsyncAxios.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/utils/index.ts

This file was deleted.

0 comments on commit 4a6670b

Please sign in to comment.