Skip to content

Core\Script\ExitReport::Reason

Which ending ran your cleanup. A script can register work to do on the way out, and whatever that work is, its first question is usually how the script ended — you tidy up differently after a clean run than after a crash.

Core\Script\ExitReport::Reason is declared in the class Core\Script\ExitReport. The method reason returns it.

There are four answers. Normal means the last line ran and the script simply finished. ExitCall means exit ended it. UncaughtThrow means something was thrown and nothing caught it, and the report hands you the error itself. Finish means the script stopped itself early on purpose, which is what a request handler does once it has answered.

Good to know: there is no case for a crash or a cancellation, and that is not an oversight — when either of those ends a script, no cleanup runs at all, so there is nothing to be told. Every reason you can ever be handed is one of the four.

Examples

The four endings your cleanup can be told about

<?nvs
// Work registered on the way out is handed a short report, and the first thing
// in it is how the script ended. This one just says so. The script below ends
// by running out of lines, which is the ending called `Normal`.
class Ending
{
public static function words(Core\Script\ExitReport::Reason $reason): string
{
return match ($reason) {
Core\Script\ExitReport::Reason::Normal => 'the script ran to the end',
Core\Script\ExitReport::Reason::ExitCall => 'something called exit',
Core\Script\ExitReport::Reason::UncaughtThrow => 'something was thrown and nobody caught it',
Core\Script\ExitReport::Reason::Finish => 'the script stopped itself on purpose',
};
}
}
Core\Script::onExit(fn(Core\Script\ExitReport $report): void => {
echo 'on the way out: ', Ending::words($report->reason()), "\n";
echo 'status: ', $report->status(), "\n";
echo 'error: ', $report->error() == null ? 'none' : 'one', "\n";
});
echo "doing the work\n";
Output
doing the work
on the way out: the script ran to the end
status: 0
error: none

Stopping early is not the same as failing

<?nvs
// `Core\Script::finish()` stops a script that has done its work. The exit reason is `Finish`.
// It is not a failure. The status is `0` and every `finally` block still runs.
// It is not an error either, so no `catch` block runs.
Core\Script::onExit(fn(Core\Script\ExitReport $report): void => {
string $ending = match ($report->reason()) {
Core\Script\ExitReport::Reason::Normal => 'ran to the end',
Core\Script\ExitReport::Reason::ExitCall => 'called exit',
Core\Script\ExitReport::Reason::UncaughtThrow => 'threw something nobody caught',
Core\Script\ExitReport::Reason::Finish => 'stopped itself, having finished',
};
echo 'on the way out: ', $ending, ', status ', $report->status(), "\n";
});
try {
echo "the answer is ready\n";
Core\Script::finish();
echo "this line never runs\n";
} catch (Throwable $caught) {
echo "no catch arm sees a finish\n";
} finally {
echo "but every finally does\n";
}
Output
the answer is ready
but every finally does
on the way out: stopped itself, having finished, status 0

One audit line that says what really happened

<?nvs
// This program writes the last line of a nightly job's log from the exit reason.
// A job that stopped because there was no work gets a different line
// from a job that failed with an error.
class Audit
{
public static function line(Core\Script\ExitReport::Reason $reason): string
{
return match ($reason) {
Core\Script\ExitReport::Reason::Normal => 'nightly import: complete',
Core\Script\ExitReport::Reason::Finish => 'nightly import: complete',
Core\Script\ExitReport::Reason::ExitCall => 'nightly import: stopped early, on purpose',
Core\Script\ExitReport::Reason::UncaughtThrow => 'nightly import: FAILED, someone should look',
};
}
// Only `UncaughtThrow` needs a person to look at it.
// It is the only reason the program did not choose itself.
public static function wakeSomebody(Core\Script\ExitReport::Reason $reason): bool
{
return $reason == Core\Script\ExitReport::Reason::UncaughtThrow;
}
}
Core\Script::onExit(fn(Core\Script\ExitReport $report): void => {
echo Audit::line($report->reason()), "\n";
if (Audit::wakeSomebody($report->reason())) {
echo "paging the on-call engineer\n";
}
});
array<string> $waiting = [];
echo "looking for files to import\n";
// No files arrived tonight. That is normal, so `exit` stops the job here.
// The exit reason is `ExitCall`.
if (Core\Arr::count($waiting) == 0) {
echo "nothing arrived tonight\n";
exit;
}
echo 'importing ', Core\Arr::count($waiting), " file(s)\n";
Output
looking for files to import
nothing arrived tonight
nightly import: stopped early, on purpose