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

Skipped Reporting of 302 and Request abort error #4881

Open
wants to merge 4 commits into
base: unstable
Choose a base branch
from

Conversation

SukhvirKooner
Copy link

@SukhvirKooner SukhvirKooner commented Jan 28, 2025

Fixes: #4873

Summary

Redirect requests (301/302)

Before
Screenshot 2025-01-27 at 10 08 48 PM
After
Screenshot 2025-01-27 at 10 09 16 PM

I have successfully resolved the 302 redirect request issue by updating the Axios error handling logic. Specifically, I added 302 to the list of status codes that should bypass error logging:
(error.response && [302, 403, 404, 405, 412].includes(error.response.status))
This ensures that when a 302 status is returned, Axios will handle it gracefully without triggering unnecessary error logs or forwarding it to Sentry.

Request aborted Error

Error ScreenShort
Screenshot 2025-01-28 at 12 39 33 PM
I have made the key changes to the original code to handle the Error particular during navigation:

  1. Page State Tracking:
let isNavigating = false;
let isReloading = false;
let pendingRequests = new Set();
  • Added variables to track navigation state and keep track of pending requests
  • This helps manage network requests during page transitions
  1. Abort Controller Integration:
    const abortController = new AbortController();
  • Added a central AbortController to cancel pending requests
  1. Navigation Event Handling:
window.addEventListener('navigate', () => {
  isNavigating = true;
  abortController.abort();
  setTimeout(() => {
    isNavigating = false;
  }, 100);
});

window.addEventListener('beforeunload', () => {
  abortController.abort();
});
  • Listens for navigation events and aborts pending requests
  • Uses a timeout to reset the navigation state
  • Handles page unload events
  1. Request Tracking in Interceptors:
client.interceptors.request.use(config => {
  config.signal = abortController.signal;
  pendingRequests.add(config);
  return config;
});
  • Adds abort signal to each request
  • Tracks pending requests in the Set
  1. Enhanced Error Handling:
if (
  isNavigating || 
  isReloading ||
  axios.isCancel(error) ||
  error.code === 'ERR_CANCELED' ||
  error.code === 'ECONNABORTED' ||
  (error.response && [302, 403, 404, 405, 412].includes(error.response.status))
) {
  return Promise.reject(error);
}
  • Added comprehensive error checking
  • Silently handles navigation-related cancellations and specific HTTP status codes
  • Added 302 status code to the suppressed errors list
  1. Cleanup Function:
export function cleanupRequests() {
  abortController.abort();
  pendingRequests.clear();
}
  • Added function to clean up pending requests
  • Useful when components unmount

Despite these suppression mechanisms, legitimate errors unrelated to Request aborted or Redirect requests (301/302) will still be logged and captured in Sentry for further monitoring and troubleshooting.

export function cleanupRequests() {
abortController.abort();
pendingRequests.clear();
}
Copy link
Member

Choose a reason for hiding this comment

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

Is this being used anywhere?

Copy link
Author

Choose a reason for hiding this comment

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

The exported cleanupRequests function is meant to be called when a component unmounts. It aborts any pending requests and clears the pendingRequests set to prevent memory leaks or unwanted side effects.

Copy link
Member

Choose a reason for hiding this comment

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

Right! Unfortunately I don't see any usages of this function. Makes me wonder whether we really need it, particularly within the scope of this task?

Copy link
Author

Choose a reason for hiding this comment

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

Got it! I'll go ahead and remove it.

// Reset the flag after navigation is likely complete
setTimeout(() => {
isNavigating = false;
}, 100);
Copy link
Member

Choose a reason for hiding this comment

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

I don't think the use of a delay here is a reliable way to set the navigation to false. I think it would be better to do this after an abort has been confirmed. You could consider adding an event listener to an controller.signal instance and do the setting there?

Copy link
Author

Choose a reason for hiding this comment

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

You're absolutely right—relying on a fixed delay is not a reliable way to reset the navigation flag. Using an event listener on the AbortController.signal makes a lot more sense since it ensures the state update happens precisely when the abort is confirmed. I'll update as you suggested.

if (
isNavigating ||
isReloading ||
axios.isCancel(error) ||
Copy link
Member

Choose a reason for hiding this comment

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

Let's import the isCancel instead so we can clear the linting error below ie;
import {isCancel} from 'axios' and this line changes to isCancel(error) ||

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for the suggestion! I'll make sure to update it accordingly.

const status = error.response?.status || 0;

// Suppress contentnode query errors
if (url.includes('contentnode')) {
Copy link
Member

Choose a reason for hiding this comment

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

Not sure about the motivation for this. The issue description doesn't seem to suggest this.

Copy link
Author

Choose a reason for hiding this comment

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

image

  • The primary goal here is to suppress errors specifically related to content nodes, as they are a significant source of the issues we need to address.
  • Since the URLs containing the keyword contentnode reliably identify these errors, this condition becomes essential for targeted error suppression.
  • I acknowledge that this detail should have been explicitly mentioned in the issue description.

@SukhvirKooner
Copy link
Author

Hi @akolson ,
I have removed the unused functions and updated the code as suggested.

Copy link
Member

@akolson akolson left a comment

Choose a reason for hiding this comment

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

Changes seem to make sense. Thanks @SukhvirKooner. I'll invite @nucleogenesis @AlexVelezLl @ozer550 incase of any further feedback before we proceed with the approval

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Skip reporting some client errors to Sentry
2 participants