I think the logger injection in CustomException is wrong because (as you pointed out) this interrupts SRP and increases the complexity of your exception classes.
I suggest you separate Exception from ExceptionHandler. The exception class should contain only information about "what (and where) went wrong." ExceptionHandler is responsible for the exception of registration (and if necessary, does other work with the exception).
, ExceptionHandler ( set_exception_handler set_error_handler - , symfony ExceptionListener), .
<?php
class ExceptionHandler {
private $logger;
public function __construct(Logger $logger)
{
$this->logger = $logger;
}
public function handle(Throwable $e)
{
$this->logger->logException($e);
}
}
. , 4 .
- , , - , .
<?php
try {
$methodThatThrowsException();
}
catch (DoesNotMatterException $e) {
}
, .
<?php
try {
$methodThatThrowsException();
}
catch (NonCriticalExceptionThatShouldBeLogged $e) {
$this->exceptionHandler->handle($e);
}
""
-, . , , . .
<?php
try {
$methodThatThrowsException();
}
catch (CriticalException $e) {
throw $e;
}
, , .
<?php
$methodThatThrowsException();