Skip to main content
Home
Drupal life hacks

Main navigation

  • Drupal
  • React
  • WP
  • Contact
  • About
User account menu
  • Log in

Breadcrumb

  1. Home

Handling Exceptions Gracefully in Drupal: A Custom Controller and Service Example

By admin, 20 May, 2024

In Drupal, checking if an exception attribute exists in a request object and returning an empty array if it does, might be used within a custom module or controller to handle errors gracefully. Here’s how you can incorporate this logic into your Drupal code.

Example: Custom Controller

Suppose you have a custom controller where you need to check for exceptions:

namespace Drupal\your_module\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
class YourController {
 
 public function yourMethod(Request $request) {
   // Check if the 'exception' attribute exists in the request.
   if ($request->attributes->has('exception')) {
     // Return an empty array if there is an exception.
     return new JsonResponse([]);
   }
  // Your logic here if there is no exception.
   $data = [
     'message' => 'No exceptions found',
     // Add more data as needed
   ];
  return new JsonResponse($data);
 }
}

Explanation:

1. Namespace and Use Statements:
  - The namespace should correspond to your module's structure.
  - Import necessary classes such as Request and JsonResponse.

2. Controller Class and Method:
  - Define a method in your controller where you need to handle the request.
  - Check if the exception attribute exists in the request using $request->attributes->has('exception').

3. Return an Empty Response:
  - If the exception attribute exists, return an empty JSON response.

4. Handling Valid Requests:
  - If there is no exception, proceed with your normal logic and return the appropriate response.

Example: Custom Module Service

If you want to encapsulate this logic in a service, you can create a service class in your module:

namespace Drupal\your_module\Service;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
class ExceptionCheckerService {
public function checkForException(Request $request) {
   // Check if the 'exception' attribute exists in the request.
   if ($request->attributes->has('exception')) {
     // Return an empty array if there is an exception.
     return new JsonResponse([]);
   }
  // Your logic here if there is no exception.
   $data = [
     'message' => 'No exceptions found',
     // Add more data as needed
   ];
  return new JsonResponse($data);
 }
}

Service Definition in your_module.services.yml:

yaml
services:
 your_module.exception_checker:
   class: Drupal\your_module\Service\ExceptionCheckerService

Using the Service in a Controller:

namespace Drupal\your_module\Controller;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\your_module\Service\ExceptionCheckerService;
use Symfony\Component\HttpFoundation\Request;
class YourController {
protected $exceptionChecker;
public function __construct(ExceptionCheckerService $exceptionChecker) {
   $this->exceptionChecker = $exceptionChecker;
 }
public static function create(ContainerInterface $container) {
   return new static(
     $container->get('your_module.exception_checker')
   );
 }
public function yourMethod(Request $request) {
   // Use the service to check for exceptions.
   return $this->exceptionChecker->checkForException($request);
 }
}

Explanation:

1. Service Class:
  - Create a service class that contains the logic to check for exceptions and return a response.

2. Service Definition:
  - Define the service in your module's your_module.services.yml file.

3. Dependency Injection:
  - Inject the service into your controller to use the exception-checking logic.

By organizing your code this way, you keep the exception-checking logic reusable and maintainable across different parts of your Drupal application.

Tags

  • #Drupal Planet
  • Handling Exceptions

Comments

About text formats

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
Powered by Drupal