Core\Script\ExitReport::error
$exitReport->error(): ?ThrowableCore\Script\ExitReport::error() returns the error that ended the script. There is one only when the reason is UncaughtThrow: an error reached the top of the script and nothing caught it. For every other ending, the result is null. An error that a catch block caught does not end the script, so it is not in the report.
The result is the same object that the script threw. It has its own class, its message and its backtrace, and you can test its class with is.
The examples below show a script with no error, an error that nothing caught, and a job that writes an alert with the message of the error.
Return value
Returns ?Throwable. When the reason is UncaughtThrow, the Throwable that nothing caught, with its own class, message and backtrace. For every other ending, null.
Examples
An error that was caught
<?nvs// Shows that `error()` returns `null` when the script caught its error.
Core\Script::onExit(fn(Core\Script\ExitReport $report): void => { ?Throwable $error = $report->error(); // This prints "no error". if ($error == null) { echo "no error\n"; }});
try { throw new RuntimeError('the cache is empty');} catch (RuntimeError $caught) { // The error is caught here, so it does not end the script. echo 'caught: ', $caught->message, "\n";}caught: the cache is emptyno errorAn error that nothing catches
<?nvs// Shows the error that ended the script. It is the same object that the script threw.// proof: exit 1
Core\Script::onExit(fn(Core\Script\ExitReport $report): void => { ?Throwable $error = $report->error(); if ($error != null) { // This prints "error: the disk is full". echo 'error: ', $error->message, "\n"; }});
echo "saving the upload\n";throw new RuntimeError('the disk is full');saving the uploaderror: the disk is fullAn alert that says what to do next
<?nvs// Shows a job that writes an alert when an error ends it. The alert also says// if the job can try again.// proof: exit 1
Core\Script::onExit(fn(Core\Script\ExitReport $report): void => { ?Throwable $error = $report->error(); if ($error == null) { return; } // `is IOError` tests the class of the error. An `IOError` is often // temporary, so the job can try again later. string $next = $error is IOError ? 'retry in 5 minutes' : 'needs a person'; // This prints "ALERT sync failed: the server did not answer (retry in 5 minutes)". echo 'ALERT sync failed: ', $error->message, ' (', $next, ")\n";});
echo "syncing the customer list\n";throw new IOError('the server did not answer');syncing the customer listALERT sync failed: the server did not answer (retry in 5 minutes)