From 5b03ba55bf4424e22ef10c71fa54f82d2380d2d0 Mon Sep 17 00:00:00 2001 From: Aristides Staffieri Date: Fri, 3 Nov 2023 11:28:02 -0600 Subject: [PATCH] updated renewAndRetry to use response status as key --- src/service/mercury/index.ts | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/service/mercury/index.ts b/src/service/mercury/index.ts index 66bed04..a86d4a6 100644 --- a/src/service/mercury/index.ts +++ b/src/service/mercury/index.ts @@ -1,5 +1,5 @@ import { Client } from "@urql/core"; -import axios from "axios"; +import axios, { AxiosError } from "axios"; import { Logger } from "pino"; import { Address, nativeToScVal, xdr } from "soroban-client"; import { mutation, query } from "./queries"; @@ -73,14 +73,23 @@ export class MercuryClient { } }; - // right now, Mercury does not differentiate expired jwts from other errors with error codes - // until Mercury returns 401s for expired tokens, on any error we renew the jwt and try again once. renewAndRetry = async (method: () => Promise) => { try { return await method(); - } catch (error) { - await this.renewMercuryToken(); - return await method(); + } catch (error: unknown) { + let status = 400; + if (error instanceof AxiosError) { + status = error.response?.status || 400; + } + + if (status === 401) { + this.logger.debug("renewing jwt, and retrying"); + await this.renewMercuryToken(); + return await method(); + } + const _error = JSON.stringify(error); + this.logger.error(_error); + throw new Error(_error); } };