fix: Salesforce by wrapping the refresh logic in a try/catch (#11508)

pull/11511/head
Alex van Andel 2023-09-25 12:33:53 +01:00 committed by GitHub
parent 3bee2d705f
commit 1d4eeab33e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 29 additions and 18 deletions

View File

@ -73,28 +73,39 @@ export default class SalesforceCalendarService implements Calendar {
const credentialKey = credential.key as unknown as ExtendedTokenResponse;
const response = await fetch("https://login.salesforce.com/services/oauth2/token", {
method: "POST",
body: new URLSearchParams({
grant_type: "refresh_token",
client_id: consumer_key,
client_secret: consumer_secret,
refresh_token: credentialKey.refresh_token,
format: "json",
}),
});
try {
/* XXX: This code results in 'Bad Request', which indicates something is wrong with our salesforce integration.
Needs further investigation ASAP */
const response = await fetch("https://login.salesforce.com/services/oauth2/token", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: new URLSearchParams({
grant_type: "refresh_token",
client_id: consumer_key,
client_secret: consumer_secret,
refresh_token: credentialKey.refresh_token,
}),
});
if (response.statusText !== "OK") throw new HttpError({ statusCode: 400, message: response.statusText });
if (!response.ok) {
const message = `${response.statusText}: ${JSON.stringify(await response.json())}`;
throw new Error(message);
}
const accessTokenJson = await response.json();
const accessTokenJson = await response.json();
const accessTokenParsed: ParseRefreshTokenResponse<typeof salesforceTokenSchema> =
parseRefreshTokenResponse(accessTokenJson, salesforceTokenSchema);
const accessTokenParsed: ParseRefreshTokenResponse<typeof salesforceTokenSchema> =
parseRefreshTokenResponse(accessTokenJson, salesforceTokenSchema);
await prisma.credential.update({
where: { id: credential.id },
data: { key: { ...accessTokenParsed, refresh_token: credentialKey.refresh_token } },
});
await prisma.credential.update({
where: { id: credential.id },
data: { key: { ...accessTokenParsed, refresh_token: credentialKey.refresh_token } },
});
} catch (err: unknown) {
console.error(err); // log but proceed
}
return new jsforce.Connection({
clientId: consumer_key,