K
K
kaxa32012019-08-20 09:33:29
symfony
kaxa3201, 2019-08-20 09:33:29

How to write your own ExceptionHandler?

I am writing my own small application, using components

"symfony/routing": "^4.3",
    "symfony/http-foundation": "^4.3",
    "symfony/yaml": "^4.3",
    "symfony/dependency-injection": "^4.3",
    "monolog/monolog": "^1.24",
    "sentry/sdk": "2.0",
    "symfony/config": "^4.3",
    "symfony/http-kernel": "^4.3",

I have an entry point to my application in bootsrap/app.php
<?php

use App\App;
use App\Services\Log;

define("BASEPATH", dirname(__DIR__));

\App\Exceptions\Handle::register(true);

$app = App::getInstance(BASEPATH);

Log::init(App::getConfig('logging'));

return $app;

as I understand it, using \App\Exceptions\Handle::register(true); I can catch all exceptions.
Please tell me how can I write a class, let's say the Handler class that will be inherited from ExceptionHandler. Where can I handle all my exceptions.
I want all my exceptions to be handled in one place.
I would appreciate an example

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaly Khomenko, 2019-08-20
@iiifx

I won't tell you about this handler, I didn't look inside. I suspect that it is only for development mode, not sure.
But I can tell you where to look at exactly how exceptions are intercepted, their type is determined and status codes are returned.
Here is a list of http-exclusions in Symfony, they are used for the purposes you need (remember your previous question): https://github.com/symfony/http-kernel/tree/master...
Here is the main process handler in the kernel, this when a controller action method is executed: https://github.com/symfony/symfony/blob/4.3/src/Sy...
Pay attention to try-catch catch all exceptions
And here comes the exception handler, which understands what to do with different types of exceptions, it handles the http exceptions you need: https://github.com/symfony/symfony/blob/4.3/src/Sy...
Study carefully , there is nothing super complicated

M
Maxim Colesnic, 2019-08-31
@colesnic89

You can hook a handler to the exception event
https://symfony.com/doc/current/reference/events.html
In services, define a handler, give it the necessary tag, and that's it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question