ExceptionListener
in package
Global exception listener.
Handles all unhandled exceptions, logs them, and provides user-friendly error messages with a unique error ID for tracking.
Table of Contents
Properties
- $environment : string
- $logger : LoggerInterface
- $mailer : MailerService
- $twig : Environment
Methods
- __construct() : mixed
- Constructor.
- onKernelException() : void
- Handles kernel exceptions.
- createApiErrorResponse() : JsonResponse
- Creates a JSON error response for API requests.
- createWebErrorResponse() : Response
- Creates an HTML error response for web requests.
- getHttpErrorMessage() : string
- Gets a user-friendly error message in French based on the HTTP status code.
- isApiRequest() : bool
- Checks if the request is an API request.
- sendErrorEmail() : void
- Sends an email notification with the error details.
Properties
$environment read-only
private
string
$environment
$logger read-only
private
LoggerInterface
$logger
$mailer read-only
private
MailerService
$mailer
$twig read-only
private
Environment
$twig
Methods
__construct()
Constructor.
public
__construct(LoggerInterface $logger, string $environment, Environment $twig, MailerService $mailer) : mixed
Parameters
- $logger : LoggerInterface
-
Logger for error tracking.
- $environment : string
-
Application environment (prod, dev, test).
- $twig : Environment
-
Twig environment for rendering templates.
- $mailer : MailerService
-
Mailer service for sending notifications.
onKernelException()
Handles kernel exceptions.
public
onKernelException(ExceptionEvent $event) : void
This method is triggered whenever an exception is not caught in the application. It logs the exception, generates a unique error ID, and returns a user-friendly response in either JSON (for API) or HTML format.
Parameters
- $event : ExceptionEvent
-
The exception event.
createApiErrorResponse()
Creates a JSON error response for API requests.
private
createApiErrorResponse(Throwable $exception, string $errorId) : JsonResponse
Parameters
- $exception : Throwable
-
The exception to handle.
- $errorId : string
-
The unique error ID for tracking.
Return values
JsonResponse —The JSON error response.
createWebErrorResponse()
Creates an HTML error response for web requests.
private
createWebErrorResponse(Throwable $exception, string $errorId) : Response
Parameters
- $exception : Throwable
-
The exception to handle.
- $errorId : string
-
The unique error ID for tracking.
Return values
Response —The HTML error response.
getHttpErrorMessage()
Gets a user-friendly error message in French based on the HTTP status code.
private
getHttpErrorMessage(int $statusCode) : string
Parameters
- $statusCode : int
-
The HTTP status code.
Return values
string —The user-friendly error message.
isApiRequest()
Checks if the request is an API request.
private
isApiRequest(Request $request) : bool
Parameters
- $request : Request
-
The HTTP request.
Return values
bool —True if it's an API request, false otherwise.
sendErrorEmail()
Sends an email notification with the error details.
private
sendErrorEmail(Throwable $exception, string $errorId, Request $request) : void
Parameters
- $exception : Throwable
-
The exception that was thrown.
- $errorId : string
-
The unique ID generated for this error.
- $request : Request
-
The request that caused the error.