Merge pull request #232 from furality/spr/main/2346ea7d

feat: log retries
This commit is contained in:
ttshivers 2023-12-23 21:46:12 -06:00 committed by GitHub
commit a2fa2930fe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,5 +1,5 @@
import type { DynamicModule, Provider } from '@nestjs/common'; 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 { randomStringGenerator } from '@nestjs/common/utils/random-string-generator.util';
import axios from 'axios'; import axios from 'axios';
import axiosRetry, { exponentialDelay } from 'axios-retry'; import axiosRetry, { exponentialDelay } from 'axios-retry';
@ -13,10 +13,28 @@ import type {
} from './interfaces'; } from './interfaces';
const createAxiosInstance = (config?: HttpModuleOptions) => { const createAxiosInstance = (config?: HttpModuleOptions) => {
const logger = new Logger(HttpService.name);
const axiosInstance = axios.create(config); const axiosInstance = axios.create(config);
axiosRetry(axiosInstance, { axiosRetry(axiosInstance, {
// Default exponential backoff // Default exponential backoff
retryDelay: exponentialDelay, 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, ...config,
}); });
return axiosInstance; return axiosInstance;