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

Centralise frontend network error reporting #3721

Closed
wants to merge 6 commits into from

Conversation

sarayourfriend
Copy link
Collaborator

Fixes

Fixes #3468 by @krysal

Description

I've kept this PR as a draft because:

  • it is just a proposal for an idea for how to solve this problem, but is not necessarily the simplest (but I think it's worth it)
  • it still needs additional testing and probably some small refactoring, but I don't want to invest time into that until someone else has looked at this and given the general approach/idea a 👍

Testing Instructions

Force an error to happen in a network request on the frontend. For example, append a bogus string to the end of media IDs so that they result in 404s. Confirm that those don't get sent to Plausible or Sentry anymore (try it on main as well to see that it goes to Sentry).

I don't know how to force a network error. You may be able to do it using your browser's network debugging tab and explicitly blocking a request.

Checklist

  • My pull request has a descriptive title (not a vague title likeUpdate index.md).
  • My pull request targets the default branch of the repository (main) or a parent feature branch.
  • My commit messages follow best practices.
  • My code follows the established code style of the repository.
  • I added or updated tests for the changes I made (if applicable).
  • I added or updated documentation (if applicable).
  • I tried running the project locally and verified that there are no visible errors.
  • [N/A] I ran the DAG documentation generator (if applicable).

Developer Certificate of Origin

Developer Certificate of Origin
Developer Certificate of Origin
Version 1.1

Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
1 Letterman Drive
Suite D4700
San Francisco, CA, 94129

Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.


Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.

@sarayourfriend sarayourfriend added 🛠 goal: fix Bug fix 💻 aspect: code Concerns the software code in the repository 🧱 stack: frontend Related to the Nuxt frontend labels Jan 30, 2024
@sarayourfriend sarayourfriend requested a review from a team as a code owner January 30, 2024 01:37
@sarayourfriend sarayourfriend marked this pull request as draft January 30, 2024 01:37
@openverse-bot openverse-bot added the 🟨 priority: medium Not blocking but should be addressed soon label Jan 30, 2024
@sarayourfriend
Copy link
Collaborator Author

@krysal interesting in your view on this, in particular, if you have any opinions about this approach.

The simpler alternative I can think of was to either copy/paste the conditionals of error handling to the appropriate areas, or to create a similar unified fetch error processing function that just takes context as an argument, rather than creating a plugin. The plugin feels like a convenient approach to me, but it could cause additional pains with Nuxt 3, I'm not sure.

Curious for your thoughts regardless!

Copy link

Full-stack documentation: https://docs.openverse.org/_preview/3721

Please note that GitHub pages takes a little time to deploy newly pushed code, if the links above don't work or you see old versions, wait 5 minutes and try again.

You can check the GitHub pages deployment action list to see the current status of the deployments.

Copy link
Collaborator

@AetherUnbound AetherUnbound left a comment

Choose a reason for hiding this comment

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

I think this approach makes sense! I like that we can differentiate the tool the error is sent to for certain kinds of failures.

@sarayourfriend sarayourfriend force-pushed the fix/prevent-axios-error-sentry branch from 83c156d to e949617 Compare February 5, 2024 06:05
Copy link
Contributor

@obulat obulat left a comment

Choose a reason for hiding this comment

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

I love centralized recording of errors, and separation of the actionable ones from the non-actionable, @sarayourfriend!
I was thinking whether we should create an Error class for our custom fetchingError instead of an object, in case we need to throw this error somewhere (based on some work in the Nuxt 3 branch). What do you think?

const mediaStore = useMediaStore()
const searchStore = useSearchStore()
const router = useRouter()
const { sendCustomEvent } = useAnalytics()
Copy link
Contributor

Choose a reason for hiding this comment

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

Have you checked that this always works?

The reason that sendCustomEvent was passed as a parameter here was that you cannot use a composable that requires Nuxt instance outside of setup. There is an edge case useSearch is called, if I remember correctly, in asyncData, and Nuxt instance is not available, so a call to useAnalytics inside useSearch produces a warning.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I see. But then should useSearch return a product based on calling useAnalytics? I'll revert this change, but that to me sounds like a bit of code smell, if the composable isn't able to call other composables at the top level. Maybe I am misunderstanding the edge case. Either way, I'll remove this from the PR, it definitely doesn't need to be here.

Copy link
Member

@krysal krysal left a comment

Choose a reason for hiding this comment

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

I love the centralization of error management, too! 💯 I think the plugin approach is right; not so sure about the high coupling with Axios since it may go away with Nuxt 3, but it is what it is right now. If we also move the API calls to a plugin, I think the error management will probably merge with it.

frontend/src/plugins/errors.ts Show resolved Hide resolved
@sarayourfriend
Copy link
Collaborator Author

should create an Error class for our custom fetchingError instead of an object, in case we need to throw this error somewhere

In JS you can throw anything. We can wrap it in a class, no problem. But is the idea to be able to check it with instanceof? We can do this in a separate issue as well, now it will be easy to switch to a different format for the error if needed.

@sarayourfriend
Copy link
Collaborator Author

Thanks for the preliminary reviews, Krystle and Olga. I'll fix up this PR and get it ready for a final review.

@sarayourfriend sarayourfriend force-pushed the fix/prevent-axios-error-sentry branch from e949617 to f9c8496 Compare February 7, 2024 00:59
@sarayourfriend sarayourfriend marked this pull request as ready for review February 7, 2024 02:29
Copy link
Contributor

@obulat obulat left a comment

Choose a reason for hiding this comment

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

I tried this locally, and sent the following error to Sentry:
Screenshot 2024-02-08 at 11 37 49 AM

Do you know where Sentry gets the "recordError" title from, and whether we can change it?
I also left 2 non-blocking comments inline.

sendCustomEvent("NETWORK_ERROR", {
requestKind: fetchingError.requestKind,
searchType: fetchingError.searchType,
})
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we should treat the 429 errors the same as NETWORK_ERRORs? There's nothing we can do about them in Sentry, too.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good question! Those we can see in Cloudflare, so I don't know how much value we get from sending them to Plausible, but I agree that we can skip them in Sentry 👍

Copy link
Contributor

Choose a reason for hiding this comment

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

Then it's even better, we can completely ignore 429s here :)

@@ -0,0 +1,148 @@
import { Context } from "@nuxt/types"
Copy link
Contributor

Choose a reason for hiding this comment

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

What if we test the$processFetchingError instead of the plugin itself? This way, the setup and mocking would be simpler.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I did start with that, but the function the plugin puts into $processFetchingError still requires $sentry and useAnalytics. It also would require further exporting things from the plugin module that aren't really needed anywhere other to make them testable, and would be testing such small units that it would kind of miss the bigger picture (is how I felt).

Happy to change this if you see a way to make the tests much simpler, and not require mocking $sentry and useAnalytics still, but if they still require that, then the only change is one that also additionally tests that the plugin is configured correctly (calls inject with the right parameters).

Copy link
Contributor

Choose a reason for hiding this comment

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

Not directly related to this comment, but after spending some time this morning investigating the problems with "search time" event, I realized that this change would also require the changes that would make useAnalytics usable on the server (see #3775)

@sarayourfriend
Copy link
Collaborator Author

Do you know where Sentry gets the "recordError" title from, and whether we can change it?

I suppose from the function name, but I don't know how to change it. Maybe in breadcrumbs? I'll have to look at the sentry JS docs, but it's a good one to change, absolutely.

Copy link
Contributor

@obulat obulat left a comment

Choose a reason for hiding this comment

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

I'm requesting changes because I realized that our plausible tracker breaks on the server (it's basically only loaded on the client side). We would need to make sure that the server-side error reporting does not cause new errors. And related to that, we should probably add e2e tests for errors because the unit tests do not have server/client distinction.

@sarayourfriend
Copy link
Collaborator Author

Yes! I actually have staged changes for that I forgot to push, thanks for the reminder!

@sarayourfriend
Copy link
Collaborator Author

sarayourfriend commented Feb 13, 2024

I remember what I got stuck on, with Playwright. I don't know how to trigger a network error as Axios identifies them. Maybe using page.route and route.abort will work, I will try it. Just wanted to mention in case you have ideas before I'm able to get back to this, @obulat

/**
* Record network errors using the appropriate tool, as needed,
* based on response code, status, and request kind.
* @param error - The error to record
Copy link
Contributor

Choose a reason for hiding this comment

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

WebStorm is complaining that JSDoc does not describe the parameters correctly (error instead of context, originalError and fetchingError.

@obulat
Copy link
Contributor

obulat commented Feb 14, 2024

I remember what I got stuck on, with Playwright. I don't know how to trigger a network error as Axios identifies them. Maybe using page.route and route.abort will work, I will try it. Just wanted to mention in case you have ideas before I'm able to get back to this, @obulat

You can use Playwright's route.abort like this to trigger a network error:

await page.route(new RegExp(`v1/(images|audio)/`), async (route) => {
      route.abort("internetdisconnected")
    })

I tried adding e2e error tests to this PR, and found that useAnalytics does not work when we need to call it from the store and not from the setup function. The context is not passed to this composable, even if the store's fetchMedia function is called from within the setup function. The context is not available, and the onMounted also throws an error. We could move the onMounted to sync analytics to the layout files to prevent it from running for every mounted component that uses it. However, the solution for the context not being available is more complex. Maybe we should move sendEvent to a plugin?

@sarayourfriend
Copy link
Collaborator Author

Maybe we should move sendEvent to a plugin?

I was thinking about this as well. There are a few composables like this that might be better suited for plugins instead of composables.

@sarayourfriend
Copy link
Collaborator Author

This PR doesn't work due to the analytics composable, so I'm going to close it for now, but leave the branch up. I'll open another PR to move analytics composable into a plugin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💻 aspect: code Concerns the software code in the repository 🛠 goal: fix Bug fix 🟨 priority: medium Not blocking but should be addressed soon 🧱 stack: frontend Related to the Nuxt frontend
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Avoid AxiosError when requesting bad image links
5 participants