Skip to content

Commit d9b6368

Browse files
committed
Try to reproduce serialize issues
1 parent 291e9c9 commit d9b6368

File tree

2 files changed

+72
-2
lines changed

2 files changed

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

0 commit comments

Comments
 (0)