logo

एक्स्ट्रा ब्लॉक टाइप्स (EBT) - नया लेआउट बिल्डर अनुभव❗

एक्स्ट्रा ब्लॉक टाइप्स (EBT) - स्टाइलिश, कस्टमाइज़ेबल ब्लॉक टाइप्स: स्लाइडशो, टैब्स, कार्ड्स, एकॉर्डियन्स और कई अन्य। बैकग्राउंड, DOM बॉक्स, जावास्क्रिप्ट प्लगइन्स के लिए बिल्ट-इन सेटिंग्स। आज ही लेआउट बिल्डिंग का भविष्य अनुभव करें।

डेमो EBT मॉड्यूल्स EBT मॉड्यूल्स डाउनलोड करें

❗एक्स्ट्रा पैराग्राफ टाइप्स (EPT) - नया पैराग्राफ्स अनुभव

एक्स्ट्रा पैराग्राफ टाइप्स (EPT) - एनालॉजिकल पैराग्राफ आधारित मॉड्यूल्स का सेट।

डेमो EPT मॉड्यूल्स EPT मॉड्यूल्स डाउनलोड करें

स्क्रॉल
02/09/2025, by Ivan

Menu

अक्सर हम 500 त्रुटि पृष्ठ का सामना करते हैं जब Drupal, उसकी सेवाएं, या कोई अन्य साइट उपलब्ध नहीं होती है। जब हम 500 (या 501–504) त्रुटि पृष्ठ देखते हैं, तो Drupal Exceptions का उपयोग यह जांचने के लिए करता है कि कोई महत्वपूर्ण कोड निष्पादित हुआ या नहीं। यदि कोई त्रुटि आती है, उदाहरण के लिए किसी अन्य साइट को HTTP अनुरोध भेजते समय, तो Drupal यह त्रुटि संदेश दिखाता है: "The website encountered an unexpected error. Please try again later":

Drupal default 500 error page
Drupal default 500 error page

आपकी साइट पर WSOD (white screen of death) होना अच्छा नहीं है, इसलिए हम इसे बेहतर बनाएँगे और इसके स्थान पर एक स्टाइल किया गया HTML पृष्ठ दिखाएँगे।

मेरे पास साइट की रूट डायरेक्टरी में एक स्टाइल किया गया 500.html पृष्ठ है, प्रदर्शन कारणों से। हम 500 त्रुटि के लिए एक स्टाइल किया गया Drupal पृष्ठ उपयोग कर सकते हैं, लेकिन मैं उसी पृष्ठ को 503/504 त्रुटियों के लिए Apache/Nginx में भी उपयोग करूँगा ताकि इसे एक स्थान पर रखना आसान हो।

500 HTML page
500 HTML error page

अब हमें अपने कस्टम मॉड्यूल DrupalBook Custom (drupalbook_custom) में कोड जोड़ने की आवश्यकता है। drupalbook_custom.services.yml में हम Event Subscriber जोड़ते हैं:

services:
  drupalbook_custom.exception_subscriber:
    class: Drupal\drupalbook_custom\EventSubscriber\SeoExceptionSubscriber
    arguments: ['@config.factory']
    tags:
      - { name: event_subscriber, priority: -250 }

और यह है drupalbook_custom/src/EventSubscriber/SeoExceptionSubscriber के लिए कोड:

<?php

namespace Drupal\drupalbook_custom\EventSubscriber;

use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Render\Markup;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Utility\Error;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\HttpKernel\KernelEvents;

/**
 * Drupal\Core\EventSubscriber\FinalExceptionSubscriber की 500 त्रुटि को बदलता है।
 */
class SeoExceptionSubscriber implements EventSubscriberInterface {

  use StringTranslationTrait;

  protected ConfigFactoryInterface $configFactory;

  public function __construct(ConfigFactoryInterface $config_factory) {
    $this->configFactory = $config_factory;
  }

  public function onException(ExceptionEvent $event): void {
    if ($this->isErrorLevelVerbose()) {
      return;
    }

    $exception = $event->getThrowable();
    $error = Error::decodeException($exception);
    $message = new FormattableMarkup('@message', [
      '@message' => $error['!message'] ?? $this->t('The website encountered an unexpected error.'),
    ]);

    $html = $this->buildHtml((string) $message);
    $status = $exception instanceof HttpExceptionInterface
      ? $exception->getStatusCode()
      : Response::HTTP_INTERNAL_SERVER_ERROR;

    $response = new Response($html, $status, ['Content-Type' => 'text/html']);

    if ($exception instanceof HttpExceptionInterface) {
      $response->headers->add($exception->getHeaders());
    }

    $event->setResponse($response);
    $event->stopPropagation();
  }

  protected function buildHtml(string $message): string {
    $template = DRUPAL_ROOT . '/500.html';

    if (is_readable($template)) {
      $html = file_get_contents($template);
      return str_replace('{{ message }}', Markup::create($message), $html);
    }

    return '<html><head><title>500</title></head><body>'
      . Markup::create($message)
      . '</body></html>';
  }

  protected function isErrorLevelVerbose(): bool {
    return $this->configFactory
      ->get('system.logging')
      ->get('error_level') === ERROR_REPORTING_DISPLAY_VERBOSE;
  }

  public static function getSubscribedEvents(): array {
    $events[KernelEvents::EXCEPTION][] = ['onException', -250];
    return $events;
  }

}

यह SeoExceptionSubscriber क्लास Drupal में सभी अप्रत्याशित Exceptions को इंटरसेप्ट करती है। यह जांचती है कि साइट “verbose” मोड में है या नहीं। यदि है, तो डिफ़ॉल्ट Drupal त्रुटि संदेश दिखाए जाते हैं; अन्यथा, यह कस्टम HTML त्रुटि पृष्ठ प्रस्तुत करती है।

यह क्लास 500.html पृष्ठ को पढ़ती है और उसमें मौजूद {{ message }} प्लेसहोल्डर को वास्तविक त्रुटि संदेश से बदल देती है। साथ ही, यह Drupal के डिफ़ॉल्ट error handler को ओवरराइड करने के लिए event propagation को रोकती है।

स्थानीय वातावरण में विस्तृत त्रुटियाँ दिखाना

स्थानीय वातावरण में 500 त्रुटि पृष्ठ की बजाय विस्तृत त्रुटियाँ देखने के लिए settings.php में निम्नलिखित सेटिंग जोड़ें:

$config['system.logging']['error_level'] = 'verbose';

error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

यदि Drupal अनुपलब्ध हो, तो आपको अपने वेब सर्वर या क्लाउड कॉन्फ़िग में अतिरिक्त सेटिंग करनी होगी।

Apache में 500 त्रुटि पृष्ठ जोड़ना

यदि आप HTTP 500–504 त्रुटियों के लिए 500.html दिखाना चाहते हैं, तो Apache को इस प्रकार कॉन्फ़िग करें:

1. Apache Virtual Host Configuration (सुझावित)

/etc/apache2/sites-available/your-site.conf में <VirtualHost> ब्लॉक के भीतर जोड़ें:

ErrorDocument 500 /500.html
ErrorDocument 501 /500.html
ErrorDocument 502 /500.html
ErrorDocument 503 /500.html
ErrorDocument 504 /500.html

फिर Apache को पुनः लोड करें:

sudo systemctl reload apache2

2. .htaccess फ़ाइल का उपयोग करना

साइट की रूट में स्थित .htaccess फ़ाइल में यह पंक्तियाँ जोड़ें:

ErrorDocument 500 /500.html
ErrorDocument 501 /500.html
ErrorDocument 502 /500.html
ErrorDocument 503 /500.html
ErrorDocument 504 /500.html

सुनिश्चित करें कि 500.html फाइल साइट की रूट डायरेक्टरी में उपलब्ध और Apache द्वारा पढ़ने योग्य हो।

Nginx में 500 त्रुटि पृष्ठ जोड़ना

Nginx में 500.html त्रुटि पृष्ठ दिखाने के लिए /etc/nginx/sites-available/your-site.conf में अपने server {} ब्लॉक में जोड़ें:

error_page 500 501 502 503 504 /500.html;
location = /500.html {
    root /var/www/html;
    internal;
}

/var/www/html उस पथ की ओर इंगित करना चाहिए जहाँ आपकी 500.html फ़ाइल रखी है। फिर Nginx को रीलोड करें:

sudo nginx -s reload

अब 500 से 504 तक की सभी HTTP त्रुटियों के लिए Nginx आपकी कस्टम HTML त्रुटि पृष्ठ दिखाएगा।