i18next / i18next-http-backend

i18next-http-backend is a backend layer for i18next using in Node.js, in the browser and for Deno.
MIT License
452 stars 70 forks source link

Inconsistent Error Handling Across Browsers for Network Failures #151

Closed nycruslan closed 1 month ago

nycruslan commented 1 month ago

Description: There is an inconsistency in how the i18next-http-backend plugin handles network errors across different browsers. The current implementation checks for specific error messages to determine whether to retry a failed request. However, different browsers return different error messages, leading to inconsistent behavior.

Steps to Reproduce:

  1. Use the i18next-http-backend plugin in a project.
  2. Simulate a network failure (e.g., by disconnecting from the internet or blocking the request).
  3. Observe the error messages in different browsers:
    • Chrome: "Failed to fetch"
    • Firefox: "NetworkError when attempting to fetch resource."
    • Safari: "Load failed"

Expected Behavior: The plugin should consistently identify network-related errors and decide whether to retry the request, regardless of the browser being used.

Actual Behavior: The plugin's error handling logic is inconsistent due to different error messages returned by various browsers.

Proposed Solution: Modify the error-checking logic to account for the different error messages across browsers. Here is a suggested revision:

if (!res && err && err.message) {
  const errorMessage = err.message.toLowerCase();
  const isNetworkError = errorMessage.includes('failed') || errorMessage.includes('fetch') || errorMessage.includes('network') || errorMessage.includes('load');
  if (isNetworkError) {
    return callback('failed loading ' + url + ': ' + err.message, true /* retry */);
  }
}

Reference to the code: https://github.com/i18next/i18next-http-backend/blob/master/lib/index.js#L77

Environment:

Additional Context: This issue affects the reliability of the plugin in handling network errors, which can impact the user experience by either failing to retry requests when appropriate or retrying unnecessarily.

adrai commented 1 month ago

Can you please provide a PR?

adrai commented 1 month ago

Thank you @nycruslan

nycruslan commented 1 month ago

Thank you @nycruslan

Thank you for quick reply.