Core\Script\ExitReport::reason
$exitReport->reason(): Core\Script\ExitReport::ReasonCore\Script\ExitReport::reason() returns how the script ended. The result is a case of the Core\Script\ExitReport::Reason enum. Normal means the last line of the script ran. ExitCall means the script called exit. UncaughtThrow means an error reached the top of the script and nothing caught it. Finish means the script called Core\Script::finish().
You read the report inside a function that you added with Core\Script::onExit(). Each call to reason() returns the same case. A FATAL error and a cancelled script have no case, because they run no exit function.
The examples below show a script that ends after its last line, a script that ends early with Core\Script::finish(), and a job that writes one log line that says how it ended.
Return value
Returns Core\Script\ExitReport::Reason. Normal when the last statement ran, ExitCall after exit, UncaughtThrow after an exception that nothing caught, and Finish after Core\Script::finish().
Examples
The last line ran
<?nvs// Shows the reason after the last line of the script runs.
Core\Script::onExit(fn(Core\Script\ExitReport $report): void => { // The reason is `Normal`, so this prints "normal end". if ($report->reason() == Core\Script\ExitReport::Reason::Normal) { echo "normal end\n"; }});
echo "the script runs\n";the script runsnormal endA script that finishes early
<?nvs// Shows the reason when a script ends early with `Core\Script::finish()`.
Core\Script::onExit(fn(Core\Script\ExitReport $report): void => { // The reason is `Finish`, so this prints "finished early". if ($report->reason() == Core\Script\ExitReport::Reason::Finish) { echo "finished early\n"; }});
int $waiting = 0;if ($waiting == 0) { echo "no orders are waiting\n"; Core\Script::finish();}
// This line does not print.echo "sending the orders\n";no orders are waitingfinished earlyA job writes how it ended
<?nvs// Shows a job that writes one log line that says how it ended.// proof: exit 3
final class Job{ // Returns a short word for each way the job can end. public static function outcome(Core\Script\ExitReport::Reason $reason): string { return match ($reason) { Core\Script\ExitReport::Reason::Normal => 'done', Core\Script\ExitReport::Reason::ExitCall => 'stopped', Core\Script\ExitReport::Reason::UncaughtThrow => 'failed', Core\Script\ExitReport::Reason::Finish => 'nothing to do', }; }}
Core\Script::onExit(fn(Core\Script\ExitReport $report): void => { // The job called `exit`, so this prints "import: stopped". echo 'import: ', Job::outcome($report->reason()), "\n";});
echo "the input folder is missing\n";exit(3);the input folder is missingimport: stopped