Core\Fatal::onLimit
Core\Fatal::onLimit(callable(array<string>): mixed $handler): voidCore\Fatal::onLimit( callable(array<string>): mixed $handler, ): void
Registers a function that runs when a resource limit stops your program.
A limit stops the program when it uses too much memory or too much CPU time. A catch block cannot catch this error, so this function is the only code that still runs. It gets an array whose limit key names the limit, for example memory or cpu_time. It runs once. After it returns, the program ends.
Novis keeps back a small amount of memory and time for this function, so it can still print or log a message. Keep it short. If it throws an error or uses up that small amount too, it is stopped.
Good to know: only one function is registered. A second call replaces the first one.
The examples below show the name of the limit, a function that ignores the array, and a short message for the person who started a big job.
Parameters
$handlercallable(array<string>): mixedThe function to run. It gets one array. Itslimitkey is the name of the limit that stopped the request, as the setting writes it, such asmemoryorcpu_time. The function returns nothing, and it may have no parameter. If it throws, or goes over the memory and time kept aside for it, it stops at once.
Return value
Returns void. Nothing. The function belongs to this request only, and a second call replaces the first. It is deleted when the request ends, and no other request can see it.
Examples
Print which limit stopped the program
<?nvs// Shows how to print which limit stopped the program.// proof: exit 1
// The program asks for a memory limit of 16 MB for itself.Core\Config::set('limits.memory', '16M');
// This function runs when a limit stops the program.// The array has one key, `limit`. Here its value is `memory`.Core\Fatal::onLimit(fn(array<string> $report): void => { echo 'stopped by the ', $report['limit'], " limit\n";});
// Each string is 4 MB, so the fourth one goes over the limit.array<string> $blocks = [];int $count = 0;while ($count < 10) { $blocks[] = Core\Str::repeat('x', 4194304); $count = $count + 1; echo 'blocks: ', $count, "\n";}
// The program stops inside the loop, so this line never runs.echo "done\n";blocks: 1blocks: 2blocks: 3stopped by the memory limitA function that ignores the report
<?nvs// Shows a limit function with no parameter. You can leave the parameter out// when you do not need to know which limit it was.// proof: exit 1
Core\Config::set('limits.memory', '16M');
Core\Fatal::onLimit(fn(): void => { echo "the page cache is full\n";});
// Each page in this cache is 2 MB. The cache has no size limit of its own,// so the memory limit stops it, and then the function above runs.array<string> $cache = [];int $page = 1;while ($page <= 100) { $cache['/page/' . $page] = Core\Str::repeat('<p>text</p>', 190650); $page = $page + 1;}
echo "all pages are cached\n";the page cache is fullA short message when an export is too big
<?nvs// Shows how to give the user a clear message when an export is too big// to build in memory.// proof: exit 1
final class Export{ // The limit function reads this to say how far the export got. public static int $rows = 0;}
Core\Config::set('limits.memory', '16M');
Core\Fatal::onLimit(fn(): void => { echo 'The export stopped after ', Export::$rows, " rows because it is too big.\n"; echo "Please choose a shorter date range and try again.\n";});
// Each row here is 1 MB of text, so the export is soon too big.array<string> $lines = [];while (Export::$rows < 1000) { $lines[] = Core\Str::repeat('2026-09-22;order;', 61681); Export::$rows = Export::$rows + 1;}
echo "The export is ready.\n";The export stopped after 14 rows because it is too big.Please choose a shorter date range and try again.