JavaScript HTML

Graceful Error Handling: Improving the LinkedIn Share Modal

When building web applications, it's crucial to handle errors gracefully, providing users with helpful feedback instead of cryptic messages. A recent update to the devlog-ist/landing project focused on improving the error handling within the LinkedIn share modal, specifically addressing scenarios where the server returns non-JSON responses.

The Problem: Cryptic Error Messages

Previously, when the server encountered an issue (like a session expiry or a CSRF mismatch) while attempting to process a LinkedIn share request, it would sometimes return an HTML error page instead of the expected JSON response. This resulted in a JSON parsing error in the modal, leaving the user with an unhelpful and confusing message.

The Solution: Handling Non-JSON Responses

To address this, the update implemented a mechanism to detect and handle non-JSON responses from the server. Now, when the modal receives an HTML error page, it displays a user-friendly error message, informing the user that something went wrong and suggesting possible causes (e.g., session expired).

Here's a simplified example of how this might be implemented in JavaScript:

fetch('/linkedin/share', { /* options */ })
  .then(response => {
    const contentType = response.headers.get("content-type");
    if (!contentType || !contentType.includes("application/json")) {
      throw new Error("Received non-JSON response");
    }
    return response.json();
  })
  .then(data => {
    // Process the JSON data
  })
  .catch(error => {
    console.error("Error sharing to LinkedIn:", error);
    // Display user-friendly error message in the modal
    displayErrorMessage("Something went wrong. Please try again or check your session.");
  });

The Benefits: Improved User Experience

This update significantly improves the user experience by:

  • Providing clear and informative error messages.
  • Guiding users towards potential solutions.
  • Preventing confusion and frustration caused by cryptic JSON parsing errors.

By handling non-JSON responses gracefully, the devlog-ist/landing project ensures a smoother and more user-friendly experience when sharing content on LinkedIn.

Graceful Error Handling: Improving the LinkedIn Share Modal
Gerardo Ruiz

Gerardo Ruiz

Author

Share: