From 1e0ce5cf77f6f0e737fee4bbcd1b66d4d0bfa00c Mon Sep 17 00:00:00 2001 From: Vili Ketonen <25618592+viliket@users.noreply.github.com> Date: Fri, 14 Jun 2024 18:06:38 +0300 Subject: [PATCH] Make VR API function fetch new token if token refresh fails (#171) --- functions/vr-api.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/functions/vr-api.ts b/functions/vr-api.ts index 60152d8..6250b19 100644 --- a/functions/vr-api.ts +++ b/functions/vr-api.ts @@ -116,9 +116,16 @@ async function getToken(env: Env): Promise { } if (now < new Date(existingAuthCredentials.refreshTokenExpiresOn)) { - const refreshedToken = await refreshToken(existingAuthCredentials, env); - await env.KV.put(authCredentialsCacheKey, JSON.stringify(refreshedToken)); - return refreshedToken; + try { + const refreshedToken = await refreshToken(existingAuthCredentials, env); + await env.KV.put( + authCredentialsCacheKey, + JSON.stringify(refreshedToken) + ); + return refreshedToken; + } catch (error) { + console.warn('Failed to refresh token:', error); + } } }