Undefined method getException() on RequestEvent class
mb3rnard opened this issue · comments
Maximilien BERNARD commented
I came across this error while trying to access http://api.codelytv.localhost:8030/
Attempted to call an undefined method named "getException" of class "Symfony\Component\HttpKernel\Event\ExceptionEvent".
To make it work, I changed a few things in this class:
- I used the ExceptionEvent class instead of RequestEvent in the method signature of onException() and changed the getException() call by getThrowable().
- I used \Throwable in the method signature of exceptionCodeFor() instead of Exception.
- I replaced class_basename() by get_class inside this same method
which results in the following:
final class ApiExceptionListener
{
private ApiExceptionsHttpStatusCodeMapping $exceptionHandler;
public function __construct(ApiExceptionsHttpStatusCodeMapping $exceptionHandler)
{
$this->exceptionHandler = $exceptionHandler;
}
public function onException(ExceptionEvent $event): void
{
$exception = $event->getThrowable();
$event->setResponse(
new JsonResponse(
[
'code' => $this->exceptionCodeFor($exception),
'message' => $exception->getMessage(),
],
$this->exceptionHandler->statusCodeFor(get_class($exception))
)
);
}
private function exceptionCodeFor(\Throwable $error): string
{
$domainErrorClass = DomainError::class;
return $error instanceof $domainErrorClass ? $error->errorCode() : Utils::toSnakeCase(get_class($error));
}
}
These are not optimal changes by any means, but they allowed me to display a fully functionnal error message on the homepage.
Rafa Gómez commented
Thanks to #163 this should be fixed now! :)