55use EasyCorp \Bundle \EasyAdminBundle \Contracts \Provider \AdminContextProviderInterface ;
66use EasyCorp \Bundle \EasyAdminBundle \Exception \BaseException ;
77use EasyCorp \Bundle \EasyAdminBundle \Exception \FlattenException ;
8+ use Psr \Log \LoggerInterface ;
89use Symfony \Component \HttpFoundation \Response ;
910use Symfony \Component \HttpKernel \Event \ExceptionEvent ;
1011use Twig \Environment ;
@@ -25,6 +26,7 @@ public function __construct(
2526 private readonly bool $ kernelDebug ,
2627 private readonly AdminContextProviderInterface $ adminContextProvider ,
2728 private readonly Environment $ twig ,
29+ private readonly ?LoggerInterface $ logger = null ,
2830 ) {
2931 }
3032
@@ -38,27 +40,37 @@ public function onKernelException(ExceptionEvent $event): void
3840 return ;
3941 }
4042
41- if ($ this ->kernelDebug || ! $ exception instanceof BaseException ) {
43+ if ($ this ->kernelDebug ) {
4244 return ;
4345 }
4446
45- if (null === $ this ->adminContextProvider ->getContext ()) {
47+ if (! $ exception instanceof BaseException && null === $ this ->adminContextProvider ->getContext ()) {
4648 return ;
4749 }
4850
49- // TODO: check why these custom error pages don't work
50- $ event ->setResponse ($ this ->createExceptionResponse (FlattenException::create ($ exception )));
51+ try {
52+ $ event ->setResponse ($ this ->createExceptionResponse (FlattenException::createFromThrowable ($ exception )));
53+ } catch (\Throwable $ renderingError ) {
54+ $ this ->logger ?->warning('EasyAdmin error page rendering failed, falling back to default error handling. ' , [
55+ 'rendering_error ' => $ renderingError ->getMessage (),
56+ 'original_exception ' => $ exception ::class,
57+ ]);
58+ }
5159 }
5260
5361 public function createExceptionResponse (FlattenException $ exception ): Response
5462 {
5563 $ context = $ this ->adminContextProvider ->getContext ();
56- $ exceptionTemplatePath = null === $ context ? '@EasyAdmin/exception.html.twig ' : $ context ->getTemplatePath ('exception ' );
57- $ layoutTemplatePath = null === $ context ? '@EasyAdmin/layout.html.twig ' : $ context ->getTemplatePath ('layout ' );
5864
59- return new Response ($ this ->twig ->render ($ exceptionTemplatePath , [
65+ if (null === $ context ) {
66+ return new Response ($ this ->twig ->render ('@EasyAdmin/exception_standalone.html.twig ' , [
67+ 'exception ' => $ exception ,
68+ ]), $ exception ->getStatusCode ());
69+ }
70+
71+ return new Response ($ this ->twig ->render ($ context ->getTemplatePath ('exception ' ), [
6072 'exception ' => $ exception ,
61- 'layout_template_path ' => $ layoutTemplatePath ,
73+ 'layout_template_path ' => $ context -> getTemplatePath ( ' layout ' ) ,
6274 ]), $ exception ->getStatusCode ());
6375 }
6476
0 commit comments