2525use Exception ;
2626use Psr \Log \LoggerInterface ;
2727use Symfony \Component \HttpFoundation \RequestStack ;
28- use Symfony \Component \HttpFoundation \Session \SessionInterface ;
2928
3029class ErrorReporter
3130{
@@ -38,9 +37,9 @@ class ErrorReporter
3837 */
3938 private $ engineBlockApplicationSingleton ;
4039 /**
41- * @var SessionInterface
40+ * @var RequestStack
4241 */
43- private $ session ;
42+ private $ requestStack ;
4443
4544 public function __construct (
4645 EngineBlock_ApplicationSingleton $ engineBlockApplicationSingleton ,
@@ -49,63 +48,64 @@ public function __construct(
4948 ) {
5049 $ this ->engineBlockApplicationSingleton = $ engineBlockApplicationSingleton ;
5150 $ this ->logger = $ logger ;
52- $ this ->session = $ requestStack-> getSession () ;
51+ $ this ->requestStack = $ requestStack ;
5352 }
5453
55- /**
56- * @param Exception $exception
57- * @param string $messageSuffix
58- */
59- public function reportError (Exception $ exception , $ messageSuffix )
54+ public function reportError (Exception $ exception , string $ messageSuffix ): void
6055 {
61- $ logContext = ['exception ' => $ exception ];
56+ $ logContext = $ this ->buildLogContext ($ exception );
57+ $ severity = $ exception instanceof EngineBlock_Exception
58+ ? $ exception ->getSeverity ()
59+ : EngineBlock_Exception::CODE_ERROR ;
6260
63- if ($ exception instanceof EngineBlock_Exception) {
64- $ severity = $ exception ->getSeverity ();
65- } else {
66- $ severity = EngineBlock_Exception::CODE_ERROR ;
61+ $ message = $ exception ->getMessage () ?: 'Exception without message " ' . get_class ($ exception ) . '" ' ;
62+ if ($ messageSuffix ) {
63+ $ message .= ' | ' . $ messageSuffix ;
6764 }
6865
69- // unwrap the exception stack
70- $ prevException = $ exception ;
71- while ($ prevException = $ prevException ->getPrevious ()) {
72- if (!isset ($ logContext ['previous_exceptions ' ])) {
73- $ logContext ['previous_exceptions ' ] = [];
74- }
66+ $ this ->logger ->log ($ severity , $ message , $ logContext );
7567
76- $ logContext ['previous_exceptions ' ][] = (string )$ prevException ;
68+ try {
69+ $ this ->storeSessionFeedback ($ exception );
70+ } finally {
71+ // flush all messages in queue, something went wrong!
72+ $ this ->engineBlockApplicationSingleton ->flushLog ('An error was caught ' );
7773 }
74+ }
7875
79- // message building
80- $ message = $ exception ->getMessage ();
81- if (empty ($ message )) {
82- $ message = 'Exception without message " ' . get_class ($ exception ) . '" ' ;
83- }
76+ private function buildLogContext (Exception $ exception ): array
77+ {
78+ $ logContext = ['exception ' => $ exception ];
79+ $ prevException = $ exception ;
8480
85- if ($ messageSuffix ) {
86- $ message .= ' | ' . $ messageSuffix ;
81+ // unwrap the exception stack
82+ while ($ prevException = $ prevException ->getPrevious ()) {
83+ $ logContext ['previous_exceptions ' ][] = (string ) $ prevException ;
8784 }
8885
89- $ this ->logger ->log ($ severity , $ message , $ logContext );
86+ return $ logContext ;
87+ }
9088
91- // Store some valuable debug info in session so it can be displayed on feedback pages
92- $ feedback = $ this ->session ->get ('feedbackInfo ' );
93- if (empty ($ feedback )) {
94- $ feedback = [];
89+ private function storeSessionFeedback (Exception $ exception ): void
90+ {
91+ // Store some valuable debug info in session so it can be displayed on feedback pages.
92+ // This is only applicable to web requests; skip entirely in CLI context or when there is no active request.
93+ if ($ this ->requestStack ->getCurrentRequest () === null ) {
94+ return ;
9595 }
9696
97+ $ session = $ this ->requestStack ->getSession ();
98+ $ feedback = $ session ->get ('feedbackInfo ' ) ?: [];
99+
97100 if ($ exception instanceof EngineBlock_Corto_Exception_HasFeedbackInfoInterface) {
98101 $ feedback = array_merge ($ feedback , $ exception ->getFeedbackInfo ());
99102 } elseif ($ exception instanceof EngineBlock_Corto_Exception_PEPNoAccess) {
100- $ this -> session ->set ('error_authorization_policy_decision ' , $ exception ->getPolicyDecision ());
103+ $ session ->set ('error_authorization_policy_decision ' , $ exception ->getPolicyDecision ());
101104 }
102105
103- $ this -> session ->set ('feedbackInfo ' , array_merge (
106+ $ session ->set ('feedbackInfo ' , array_merge (
104107 $ feedback ,
105108 $ this ->engineBlockApplicationSingleton ->collectFeedbackInfo ($ exception )
106109 ));
107-
108- // flush all messages in queue, something went wrong!
109- $ this ->engineBlockApplicationSingleton ->flushLog ('An error was caught ' );
110110 }
111111}
0 commit comments