Skip to content

Commit 3702de8

Browse files
committed
Merge branch '5.x' into 5.next
# Conflicts: # composer.json
2 parents ec8e029 + bd06e5f commit 3702de8

File tree

3 files changed

+80
-2
lines changed

3 files changed

+80
-2
lines changed

src/ToolbarService.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,31 @@ public function saveData(ServerRequest $request, ResponseInterface $response): R
291291
foreach ($this->registry->loaded() as $name) {
292292
$panel = $this->registry->{$name};
293293
try {
294-
$content = serialize($panel->data());
294+
$data = $panel->data();
295+
296+
// Set error handler to catch warnings/errors during serialization
297+
set_error_handler(function ($errno, $errstr) use ($name): void {
298+
throw new Exception("Serialization error in panel '{$name}': {$errstr}");
299+
});
300+
301+
$content = serialize($data);
302+
303+
restore_error_handler();
295304
} catch (Exception $e) {
305+
restore_error_handler();
306+
307+
$errorMessage = sprintf(
308+
'Failed to serialize data for panel "%s": %s',
309+
$name,
310+
$e->getMessage(),
311+
);
312+
313+
Log::warning($errorMessage);
314+
Log::debug('Panel data type: ' . gettype($data ?? null));
315+
296316
$content = serialize([
297-
'error' => $e->getMessage(),
317+
'error' => $errorMessage,
318+
'panel' => $name,
298319
]);
299320
}
300321
$row->panels[] = $requests->Panels->newEntity([

tests/TestCase/ToolbarServiceTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use Cake\TestSuite\TestCase;
2727
use DebugKit\Model\Entity\Request as RequestEntity;
2828
use DebugKit\Panel\SqlLogPanel;
29+
use DebugKit\TestApp\Panel\SimplePanel;
2930
use DebugKit\ToolbarService;
3031
use PHPUnit\Framework\Attributes\DataProvider;
3132

@@ -506,4 +507,56 @@ public function testIsEnabledForceEnableCallable()
506507
]);
507508
$this->assertTrue($bar->isEnabled(), 'debug is off, panel is forced on');
508509
}
510+
511+
/**
512+
* Test that saveData handles serialization errors gracefully
513+
*
514+
* @return void
515+
*/
516+
public function testSaveDataSerializationError()
517+
{
518+
$request = new Request([
519+
'url' => '/articles',
520+
'environment' => ['REQUEST_METHOD' => 'GET'],
521+
]);
522+
$response = new Response([
523+
'statusCode' => 200,
524+
'type' => 'text/html',
525+
'body' => '<html><title>test</title><body><p>some text</p></body>',
526+
]);
527+
528+
$bar = new ToolbarService($this->events, []);
529+
$bar->loadPanels();
530+
531+
// Create a panel with unserializable data
532+
/** @var SimplePanel $panel */
533+
$panel = $bar->registry()->load('DebugKit.TestApp\Panel\SimplePanel', [
534+
'className' => SimplePanel::class,
535+
]);
536+
// Mock the data() method to return something problematic
537+
$panel->setData(['closure' => fn() => 'test']);
538+
539+
$row = $bar->saveData($request, $response);
540+
$this->assertNotEmpty($row, 'Should save data even with serialization errors');
541+
542+
$requests = $this->getTableLocator()->get('DebugKit.Requests');
543+
$result = $requests->find()
544+
->orderBy(['Requests.requested_at' => 'DESC'])
545+
->contain('Panels')
546+
->first();
547+
548+
// Find the SimplePanel in the results
549+
$simplePanel = null;
550+
foreach ($result->panels as $p) {
551+
if ($p->panel === 'DebugKit.TestApp\Panel\SimplePanel') {
552+
$simplePanel = $p;
553+
break;
554+
}
555+
}
556+
557+
$this->assertNotNull($simplePanel, 'SimplePanel should be present');
558+
$content = unserialize($simplePanel->content);
559+
$this->assertArrayHasKey('error', $content, 'Should have error key');
560+
$this->assertStringContainsString('SimplePanel', $content['error'], 'Error should mention panel name');
561+
}
509562
}

tests/test_app/Panel/SimplePanel.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,8 @@
2222
*/
2323
class SimplePanel extends DebugPanel
2424
{
25+
public function setData(array $data): void
26+
{
27+
$this->_data = $data;
28+
}
2529
}

0 commit comments

Comments
 (0)