Skip to content

Fix Zod errors in Outlook auth function#7931

Merged
emrysal merged 3 commits intomainfrom
hotpatch-outlook
Mar 25, 2023
Merged

Fix Zod errors in Outlook auth function#7931
emrysal merged 3 commits intomainfrom
hotpatch-outlook

Conversation

@joeauyeung
Copy link
Copy Markdown
Contributor

@joeauyeung joeauyeung commented Mar 24, 2023

What does this PR do?

This PR fixes Zod parsing errors in the Outlook auth function. By using safeParse we will not throw an error when the response from MS is not as expected. We also added increased logging of MS error responses to better handle future errors.

Fixes # (issue)

https://www.loom.com/share/3ca4e240e5864151854c50ed88431cc3

Environment: Staging(main branch) / Production

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How should this be tested?

  • Connect an Outlook account
  • In the DB change the expiry date to force a refresh
  • Refresh the listed calendars page
    • There should be a new token
  • In the DB remove a property so that the zod parse will fail
  • Refresh the listed calendars page
    • There should be a new token
  • Create a fake response in the Outlook auth function. The error log should contain the zod error & the response

Checklist

  • I haven't added tests that prove my fix is effective or that my feature works

@joeauyeung joeauyeung requested a review from a team March 24, 2023 14:43
@vercel
Copy link
Copy Markdown

vercel bot commented Mar 24, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated
cal ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Mar 24, 2023 at 3:49PM (UTC)
1 Ignored Deployment
Name Status Preview Comments Updated
ui ⬜️ Ignored (Inspect) Visit Preview Mar 24, 2023 at 3:49PM (UTC)

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 24, 2023

📦 Next.js Bundle Analysis

This analysis was generated by the next.js bundle analysis action 🤖

This PR introduced no changes to the javascript bundle 🙌

});
const o365AuthCredentials = refreshTokenResponseSchema.parse(await handleErrorsJson(response));
const responseJson = await handleErrorsJson(response);
const tokenResponse = refreshTokenResponseSchema.safeParse(responseJson);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to safeParse so we don't throw an error if MS sends a response we aren't expecting

const o365AuthCredentials = refreshTokenResponseSchema.parse(await handleErrorsJson(response));
const responseJson = await handleErrorsJson(response);
const tokenResponse = refreshTokenResponseSchema.safeParse(responseJson);
o365AuthCredentials = { ...o365AuthCredentials, ...(tokenResponse.success && tokenResponse.data) };
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the responses is what we were expecting, update the new keys. In the spread operator, if keys are present in both objects, the second object's keys will be written

Comment on lines +238 to +240
if (!tokenResponse.success) {
console.error("zodError:", tokenResponse.error, "MS response:", responseJson);
}
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capture the response on Axiom so we can better diagnose the problem

Comment on lines +260 to 261
refreshTokenResponseSchema.safeParse(o365AuthCredentials).success &&
!isExpired(o365AuthCredentials.expires_in)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refresh the tokens if the keys object is not what we expected or the access token is expired

@deploysentinel
Copy link
Copy Markdown

deploysentinel bot commented Mar 24, 2023

No failed tests 🎉

Copy link
Copy Markdown
Contributor

@emrysal emrysal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than that looking good 👍

@joeauyeung joeauyeung requested a review from emrysal March 24, 2023 15:40
@emrysal emrysal enabled auto-merge (squash) March 25, 2023 21:09
@emrysal emrysal merged commit 8dd9b8d into main Mar 25, 2023
@emrysal emrysal deleted the hotpatch-outlook branch March 25, 2023 21:19
@PeerRich PeerRich added the core area: core, team members only label Jul 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core area: core, team members only

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants