next-drupal icon indicating copy to clipboard operation
next-drupal copied to clipboard

translatePath: Throw error when server responds Internal Server Error

Open marcorcau opened this issue 2 years ago • 0 comments

Package

next-drupal (NPM package)

Describe the feature request

DrupalClient.translatePath currently returns the same value (null) in multiple situations:

  • Path is "Not Found".
  • A server error is returned by backend.

It would be great to differentiate legitimate "Not Found" from any server errors.

Describe the solution you'd like

When a page re-generation is requested but an error happens during page build, Next.js holds to the previous version of the page if it exists, and doesn't update the content of a page with "Server error".

But if the error is in /router/translate-path backend callback, the page is currently updated to a 404 Not Found, at least in my setup. In this backend failure situation it would be great to also not re-generate the page, and just stick to the previous version.

Describe alternatives you've considered

The issue is solved with some cache in backend so the correct response is delivered.

Additional context

The issue can be forced for the /internal-server-error path with this PathTranslatorSubscriber:

<?php

namespace Drupal\decoupled_router\EventSubscriber;

use Drupal\decoupled_router\PathTranslatorEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Decoupled Router event subscriber.
 */
class InternalServerErrorPathTranslatorSubscriber implements EventSubscriberInterface {

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[PathTranslatorEvent::TRANSLATE][] = ['onPathTranslation', 20];
    return $events;
  }

  /**
   * Set 500 internal error for a specific test path.
   *
   * @param \Drupal\decoupled_router\PathTranslatorEvent $event
   *   The path translator event.
   */
  public function onPathTranslation(PathTranslatorEvent $event) {
    $response = $event->getResponse();

    if ($event->getPath() == '/internal-server-error') {
      $response->setData([
        'message' => 'Internal server error.',
        'details' => 'A forced internal server error.',
      ]);
      $response->setStatusCode(500);
      $event->stopPropagation();
    }
  }

}

```

marcorcau avatar Feb 19 '24 21:02 marcorcau