Skip to content

Commit bcacc66

Browse files
committed
Add logging to error rendering failures
When error rendering fails it can be tedious to diagnose what the problem is. Having some logging when rendering fails could help triage the problem and get to a solution quicker. Refs #17603
1 parent 3f26c5b commit bcacc66

4 files changed

Lines changed: 24 additions & 3 deletions

File tree

src/Error/Renderer/WebExceptionRenderer.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
use Cake\Http\ResponseEmitter;
3636
use Cake\Http\ServerRequest;
3737
use Cake\Http\ServerRequestFactory;
38+
use Cake\Log\Log;
3839
use Cake\Routing\Exception\MissingRouteException;
3940
use Cake\Routing\Router;
4041
use Cake\Utility\Inflector;
@@ -161,6 +162,7 @@ protected function _getController(): Controller
161162
$request = $request->withAttribute('params', $routerRequest->getAttribute('params'));
162163
}
163164

165+
$class = "";
164166
try {
165167
$params = $request->getAttribute('params');
166168
$params['controller'] = 'Error';
@@ -185,6 +187,11 @@ protected function _getController(): Controller
185187
$controller = new $class($request);
186188
$controller->startupProcess();
187189
} catch (Throwable $e) {
190+
Log::warning(
191+
"Failed to construct or call startup() on the resolved controller class of `$class`. " .
192+
"Using Fallback Controller instead. Error {$e->getMessage()}",
193+
'cake.error'
194+
);
188195
$controller = null;
189196
}
190197

@@ -412,6 +419,10 @@ protected function _outputMessage(string $template): Response
412419

413420
return $this->_shutdown();
414421
} catch (MissingTemplateException $e) {
422+
Log::warning(
423+
"MissingTemplateException - Failed to render error template `{$template}`. Error: {$e->getMessage()}",
424+
'cake.error'
425+
);
415426
$attributes = $e->getAttributes();
416427
if (
417428
$e instanceof MissingLayoutException ||
@@ -422,13 +433,21 @@ protected function _outputMessage(string $template): Response
422433

423434
return $this->_outputMessage('error500');
424435
} catch (MissingPluginException $e) {
436+
Log::warning(
437+
"MissingPluginException - Failed to render error template `{$template}`. Error: {$e->getMessage()}",
438+
'cake.error'
439+
);
425440
$attributes = $e->getAttributes();
426441
if (isset($attributes['plugin']) && $attributes['plugin'] === $this->controller->getPlugin()) {
427442
$this->controller->setPlugin(null);
428443
}
429444

430445
return $this->_outputMessageSafe('error500');
431446
} catch (Throwable $outer) {
447+
Log::warning(
448+
"Throwable - Failed to render error template `{$template}`. Error: {$outer->getMessage()}",
449+
'cake.error'
450+
);
432451
try {
433452
return $this->_outputMessageSafe('error500');
434453
} catch (Throwable $inner) {

templates/Error/missing_controller.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
$namespace = Configure::read('App.namespace');
2323
$prefixNs = $prefixPath = '';
2424

25+
$controller = (string)$controller;
2526
$incompleteInflection = (str_contains($controller, '_') || str_contains($controller, '-'));
2627
$originalClass = $controller;
2728

tests/TestCase/Error/ExceptionTrapTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,8 @@ public function testSkipLogException(): void
260260
ob_get_clean();
261261

262262
$logs = Log::engine('test_error')->read();
263-
$this->assertEmpty($logs);
263+
$this->assertCount(1, $logs);
264+
$this->assertStringContainsString('MissingTemplateException - Failed to render', $logs[0]);
264265
$this->assertTrue($this->triggered, 'Should have triggered event when skipping logging.');
265266
}
266267

tests/TestCase/Error/Middleware/ErrorHandlerMiddlewareTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ public function testHandleExceptionLogAttributes(): void
318318
$request = ServerRequestFactory::fromGlobals();
319319
$middleware = new ErrorHandlerMiddleware(['log' => true]);
320320
$handler = new TestRequestHandler(function (): void {
321-
throw new MissingControllerException(['class' => 'Articles']);
321+
throw new MissingControllerException(['controller' => 'Articles']);
322322
});
323323
$result = $middleware->process($request, $handler);
324324
$this->assertSame(404, $result->getStatusCode());
@@ -329,7 +329,7 @@ public function testHandleExceptionLogAttributes(): void
329329
$logs[0]
330330
);
331331
$this->assertStringContainsString('Exception Attributes:', $logs[0]);
332-
$this->assertStringContainsString("'class' => 'Articles'", $logs[0]);
332+
$this->assertStringContainsString("'controller' => 'Articles'", $logs[0]);
333333
$this->assertStringContainsString('Request URL:', $logs[0]);
334334
}
335335

0 commit comments

Comments
 (0)