Not sure if this can be done but worth a punt.
Brief explanation, we are adopting a modular monolith approach to our application development and everything is fine thus far. However, when implementing a custom exception handler for each of module this becomes quite tricky when attempting to register/inject this handler into Laravel similar to the way you would with the service providers.
File structure:
- Laravel
- app
- Exceptions
- Handler.php
- src
- module 1
- Http
- Client.php
- Exceptions
- Handler.php
- ExceptionOne.php
- Tests
- TestOne.php
- module 2
My understanding is because Laravel’s error handler is a singleton this is going to be quite tricky…not possible.
I have tried extending Laravels handler and then call the register method in my modules service provider but this didn’t work.
There were some other methods not worth mentioning that didn’t work either.
Ideal solution would be:
class Handler
{
protected $registeredHandlers = [
AppExceptions::class,
ModularMonolithExceptionsHandler::class
];
public function register(): void
{
foreach(this->registeredHandlers as $handler) {
$handler->register();
}
}
}
Something along these lines where we can have multiple handlers where we can register the exceptions, this would still allow the singleton to be there but also, allows for separation of the exceptions from the main apps handler.