Skip to content

Commit

Permalink
Merge pull request #232 from furality/spr/main/2346ea7d
Browse files Browse the repository at this point in the history
feat: log retries
  • Loading branch information
ttshivers authored Dec 24, 2023
2 parents 1d3190e + d141239 commit a2fa293
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion lib/http.module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { DynamicModule, Provider } from '@nestjs/common';
import { Module } from '@nestjs/common';
import { Logger, Module } from '@nestjs/common';
import { randomStringGenerator } from '@nestjs/common/utils/random-string-generator.util';
import axios from 'axios';
import axiosRetry, { exponentialDelay } from 'axios-retry';
Expand All @@ -13,10 +13,28 @@ import type {
} from './interfaces';

const createAxiosInstance = (config?: HttpModuleOptions) => {
const logger = new Logger(HttpService.name);
const axiosInstance = axios.create(config);
axiosRetry(axiosInstance, {
// Default exponential backoff
retryDelay: exponentialDelay,
onRetry(retryCount, error, requestConfig) {
logger.warn(
{
retryCount,
name: error.name,
message: error.message,
code: error.code,
method: requestConfig.method,
baseURL: requestConfig.baseURL,
url: requestConfig.url,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
params: requestConfig.params,
data: error.response?.data,
},
'Retrying request after error',
);
},
...config,
});
return axiosInstance;
Expand Down

0 comments on commit a2fa293

Please sign in to comment.