Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Theme] Handle async errors #4599

Merged
merged 12 commits into from
Oct 16, 2024
Merged

[Theme] Handle async errors #4599

merged 12 commits into from
Oct 16, 2024

Conversation

frandiox
Copy link
Contributor

@frandiox frandiox commented Oct 8, 2024

WHY are these changes introduced?

Related to #4576 and #4598

A proper request retry + token refresh is still needed but this PR tries to handle async errors more gracefully.

WHAT is this pull request doing?

Ensure we print async errors properly with a red banner, and avoid process exit in some situations:

  • On file delete / upload error in the remote theme, we show the error but the process continues. This is because the local development can continue working with local files in most cases, and is up to the developer to halt the process after seeing the error.
  • On polling errors, wait for 5 errors before exiting the process. This is because it might recover from one or two failed attempts since it still compares local checksums with latest.

For example, when you lose your internet connection while polling:

image

During the initial sync, if there are too many delete errors or a single upload error:

image

How to test your changes?

Create fake errors around the modified code or directly disconnect your internet + reconnect it within 15 seconds.

Post-release steps

Measuring impact

How do we know this change was effective? Please choose one:

  • n/a - this doesn't need measurement, e.g. a linting rule or a bug-fix
  • Existing analytics will cater for this addition
  • PR includes analytics changes to measure impact

Checklist

  • I've considered possible cross-platform impacts (Mac, Linux, Windows)
  • I've considered possible documentation changes

Copy link
Contributor

github-actions bot commented Oct 8, 2024

Thanks for your contribution!

Depending on what you are working on, you may want to request a review from a Shopify team:

  • Themes: @shopify/advanced-edits
  • UI extensions: @shopify/ui-extensions-cli
    • Checkout UI extensions: @shopify/checkout-ui-extensions-api-stewardship
  • Hydrogen: @shopify/hydrogen
  • Other: @shopify/app-inner-loop

Copy link
Contributor

github-actions bot commented Oct 8, 2024

Coverage report

St.
Category Percentage Covered / Total
🟡 Statements
72.73% (+0.09% 🔼)
8563/11774
🟡 Branches
69.72% (+0.11% 🔼)
4205/6031
🟡 Functions
71.69% (-0.03% 🔻)
2211/3084
🟡 Lines
73.06% (+0.09% 🔼)
8105/11094
Show new covered files 🐣
St.
File Statements Branches Functions Lines
🟢
... / function-upload-url-generate.ts
100% 100% 100% 100%
Show files with reduced coverage 🔻
St.
File Statements Branches Functions Lines
🟢
... / app.test-data.ts
91.4% (-0.54% 🔻)
91.09% (-0.09% 🔻)
81.01% (-1.27% 🔻)
90.8% (-0.57% 🔻)
🟢
... / app.ts
87.07%
70.89% (-0.72% 🔻)
92% 88.37%
🟢
... / specification.ts
93.1% (-1.81% 🔻)
90.48%
87.5% (-0.5% 🔻)
92% (-2.12% 🔻)
🟢
... / function.ts
86.36% (-0.59% 🔻)
86.36% 83.33%
86.36% (-0.59% 🔻)
🔴
... / extension.ts
55.26% (-0.29% 🔻)
50% 57.14%
56.76% (-0.06% 🔻)
🟡
... / update-extension.ts
64.86% (-5.14% 🔻)
54.55% (-3.79% 🔻)
60%
68.75% (-5.54% 🔻)
🟡
... / build.ts
74.49%
59.09% (-2.27% 🔻)
75.76% 72.22%
🟢
... / extension.ts
91.4% (+0.09% 🔼)
73.58%
91.3% (-0.36% 🔻)
91.21% (+0.1% 🔼)
🔴
... / app-management-client.ts
20.75% (-0.09% 🔻)
10.26%
22.58% (-0.25% 🔻)
19% (-0.09% 🔻)
🔴
... / partners-client.ts
26.87% (-0.2% 🔻)
40%
18.18% (-0.34% 🔻)
26.56% (-0.21% 🔻)
🟡
... / fs.ts
62.5%
84.62% (-7.05% 🔻)
58.97% 62.5%

Test suite run success

1947 tests passing in 876 suites.

Report generated by 🧪jest coverage report action from 8e8fef2

@frandiox frandiox requested review from karreiro, jamesmengo and a team and removed request for karreiro October 9, 2024 06:27
@frandiox frandiox marked this pull request as ready for review October 9, 2024 06:27

This comment has been minimized.

Copy link
Contributor

@graygilmore graygilmore left a comment

Choose a reason for hiding this comment

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

Looks good! Couple of questions but I think it all makes sense.

packages/theme/src/cli/utilities/errors.ts Outdated Show resolved Hide resolved
})
})
.catch(abort)
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of .catch()ing on each of these could we wrap the whole thing in a try/catch? Maybe not with the promises?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It would be much more verbose with async promises than .catch, though. We could also pass it as the second param in .then(() => {}, abort) but I think most people are more familiar with .catch 🤔

const [result] = await bulkUploadThemeAssets(Number(themeId), [{key: fileKey, value: content}], adminSession)
const [result] = await bulkUploadThemeAssets(Number(themeId), [{key: fileKey, value: content}], adminSession)

if (!result?.success) {
Copy link
Contributor

Choose a reason for hiding this comment

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

(nit) Would prefer result.errors here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not familiar with the underlying API this calls in cli-kit, so not sure if something can have a "warning" in errors or something like that. It looks like success is just response.status === 200, and errors are at least {} instead of undefined.
cc @jamesmengo thoughts?

Promise.all([
themeCreationPromise,
uploadJobPromise.then((result) => result.promise),
deleteJobPromise.then((result) => result.promise),
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this would have already run on L51? I might be misreading this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Afaik, the .then creates a new Promise, so in line 51 we create "promise A" that resolves to X, and here we create "promise B" that also resolves to X. But they are still different promises and, since "promise A" is conditional, we can't use it to add the catch (because we might be adding it to something else than the delete promise).

I think? 🤔

Copy link
Contributor

@karreiro karreiro left a comment

Choose a reason for hiding this comment

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

Thank you, @frandiox! LGTM and works as expected as well 🚀

Copy link
Contributor

@jamesmengo jamesmengo left a comment

Choose a reason for hiding this comment

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

Very cool! I learned lots from reading this PR - thanks Fran!

: deleteJobPromise.then((result) => result.promise)

if (options?.backgroundWorkCatch) {
// Aggregate all backgorund work in a single promise and handle errors
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// Aggregate all backgorund work in a single promise and handle errors
// Aggregate all background work in a single promise and handle errors

@frandiox frandiox requested a review from a team as a code owner October 11, 2024 03:37
@frandiox frandiox enabled auto-merge October 11, 2024 04:11
@karreiro
Copy link
Contributor

@shopify/app-inner-loop, Could you please take a look at this PR? It seems like the merge is blocked by that requirement.

@frandiox frandiox added this pull request to the merge queue Oct 16, 2024
Merged via the queue into main with commit 74f8888 Oct 16, 2024
@frandiox frandiox deleted the fd-async-errors branch October 16, 2024 08:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants