Skip to content

Commit

Permalink
Handle exceptions in data search method
Browse files Browse the repository at this point in the history
Added error handling to the search method in Table class. Now every iteration of the input data is wrapped in a try-catch block to handle and log any potential exceptions and keep the application from crashing. The Throwable exception has also been imported to be used in this class.
  • Loading branch information
krzysztofzylka committed Jan 24, 2024
1 parent f1aa5dd commit 51d531f
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/Libs/Table/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Krzysztofzylka\MicroFramework\Model;
use Krzysztofzylka\MicroFramework\View;
use Krzysztofzylka\Request\Request;
use Throwable;

/**
* Table
Expand Down Expand Up @@ -161,6 +162,7 @@ public function setData(array $data): void
* @param array $data
* @param string $search
* @return array
* @throws Throwable
*/
protected function search(array $data, string $search): array
{
Expand All @@ -179,13 +181,23 @@ protected function search(array $data, string $search): array

foreach ($data as $row) {
foreach ($row as $value) {
if (preg_match($regex, $value)) {
$matches[] = $row;
break; // Przerywamy wewnętrzną pętlę po pierwszym dopasowaniu
try {
if (is_string($value) && preg_match($regex, $value)) {
$matches[] = $row;

break;
}
} catch (Throwable $throwable) {
DebugBar::addThrowable($throwable);
Log::log('Table search fail', 'INFO', ['value' => $value, 'regex' => $regex, 'search' => $search]);
TableReminder::clear($this);

throw $throwable;
}
}
}


return $matches;
}

Expand Down Expand Up @@ -401,7 +413,7 @@ public function render(): string
], 'Render table');
DebugBar::timeStop($debugId);
return $tableContent;
} catch (\Throwable $throwable) {
} catch (Throwable $throwable) {
DebugBar::addThrowable($throwable);
Log::log('Failed table render', 'ERR', ['exception' => $throwable->getMessage()]);

Expand Down

0 comments on commit 51d531f

Please sign in to comment.