Browse Source

fix(api-gateway): logs severity on retry attempts

Karol Sójko 1 year ago
parent
commit
1c3d19cca4
1 changed files with 17 additions and 13 deletions
  1. 17 13
      packages/api-gateway/src/Service/Http/HttpServiceProxy.ts

+ 17 - 13
packages/api-gateway/src/Service/Http/HttpServiceProxy.ts

@@ -215,30 +215,24 @@ export class HttpServiceProxy implements ServiceProxyInterface {
       }
 
       if (retryAttempt) {
-        this.logger.info(
+        this.logger.debug(
           `Request to ${serverUrl}/${endpointOrMethodIdentifier} succeeded after ${retryAttempt} retries`,
         )
       }
 
       return serviceResponse
     } catch (error) {
-      const errorMessage = (error as AxiosError).isAxiosError
-        ? JSON.stringify((error as AxiosError).response?.data)
-        : (error as Error).message
-
-      this.logger.error(
-        `Could not pass the request to ${serverUrl}/${endpointOrMethodIdentifier} on underlying service: ${JSON.stringify(
-          error,
-        )}`,
-      )
-
       const requestTimedOut =
         'code' in (error as Record<string, unknown>) && (error as Record<string, unknown>).code === 'ETIMEDOUT'
       const tooManyRetryAttempts = retryAttempt && retryAttempt > 2
       if (!tooManyRetryAttempts && requestTimedOut) {
         await this.timer.sleep(50)
 
-        this.logger.info(`Retrying request to ${serverUrl}/${endpointOrMethodIdentifier} for the ${retryAttempt} time`)
+        const nextRetryAttempt = retryAttempt ? retryAttempt + 1 : 1
+
+        this.logger.debug(
+          `Retrying request to ${serverUrl}/${endpointOrMethodIdentifier} for the ${nextRetryAttempt} time`,
+        )
 
         return this.getServerResponse(
           serverUrl,
@@ -246,10 +240,20 @@ export class HttpServiceProxy implements ServiceProxyInterface {
           response,
           endpointOrMethodIdentifier,
           payload,
-          retryAttempt ? retryAttempt + 1 : 1,
+          nextRetryAttempt,
         )
       }
 
+      const errorMessage = (error as AxiosError).isAxiosError
+        ? JSON.stringify((error as AxiosError).response?.data)
+        : (error as Error).message
+
+      this.logger.error(
+        `Could not pass the request to ${serverUrl}/${endpointOrMethodIdentifier} on underlying service: ${JSON.stringify(
+          error,
+        )}`,
+      )
+
       this.logger.debug('Response error: %O', (error as AxiosError).response ?? error)
 
       if ((error as AxiosError).response?.headers['content-type']) {