Throwable
The root every error in Novis descends from, and the one a program's own errors extend.
Writing catch (Throwable $e) means anything at all: the two halves of the tree, every narrower name under them, and whatever the application declared itself. Every one of them carries the same four things — the message, the location it was thrown from, the backtrace of frames it passed through, and the previous error it was raised in answer to.
There is no Exception and no Error. A class of your own says extends Throwable, which puts it beside the built-in halves rather than inside either, so a library catching mistakes or catching the world's refusals never takes it by accident.
Good to know: the widest clause goes last. A catch list is read top to bottom and the first matching arm wins, so a Throwable arm above a narrower one would take everything.
The examples below show an error of your own, reading what a caught one carries, and a request handler whose last clause turns anything unexpected into one reply.
Examples
An error of your own
<?nvs// Your own error class extends `Throwable`. There is no other base class to choose.// It gets `message` from `Throwable` and can add its own properties.// Here `Overdue` adds the number of the unpaid invoice.
class Overdue extends Throwable{ public string $invoice = '';}
final class Ledger{ public static function charge(string $invoice, bool $paid): string { if (!$paid) { var $unpaid = new Overdue('invoice ' . $invoice . ' is still open'); $unpaid->invoice = $invoice; throw $unpaid; } return 'invoice ' . $invoice . ' charged'; }}
try { echo Ledger::charge('4021', true), "\n"; echo Ledger::charge('4022', false), "\n";} catch (Overdue $chase) { echo $chase->message, "\n"; echo 'chase up ', $chase->invoice, "\n";}invoice 4021 chargedinvoice 4022 is still openchase up 4022What a caught error carries
<?nvs// Every error, whatever its class, carries the same four things: the message,// the error it was raised in answer to, the list of frames it travelled// through, and the place it was thrown from. Reporting the second of those is// how a wrapped failure keeps the real cause.
class ReportFailed extends Throwable{}
final class Report{ public static function build(): string { try { var $figures = Core\Json::decode("{\"rows\": 3,"); return 'built'; } catch (Throwable $cause) { throw new ReportFailed('the monthly report could not be built', { previous: $cause }); } }}
try { echo Report::build(), "\n";} catch (Throwable $failed) { echo $failed->message, "\n";
// The cause may be absent, so it is read into a name and checked before // anything is asked of it. var $cause = $failed->previous; if ($cause != null) { echo 'because: ', $cause->message, "\n"; }
echo 'frames travelled: ', Core\Arr::count($failed->backtrace), "\n";}the monthly report could not be builtbecause: Core\Json::decode(): EOF while parsing a value at line 1 column 11frames travelled: 1One last clause for anything unexpected
<?nvs// This program turns each error in a request into a reply with a status code.// Each known error type gets its own `catch` block. A last `catch (Throwable ...)`// gives one safe reply for every other error. It must come last,// because the first `catch` block that matches is the one that runs.
final class Handler{ public static function reply(string $path): tainted string { try { if ($path == '/orders') { var $body = Core\Json::decode("{\"id\": 9,"); return '200 ok'; } if ($path == '/export') { throw new TimeoutError('the export service did not answer'); } if ($path == '/admin') { throw new RuntimeError('no session for this visitor'); } if ($path == '/legacy') { var $code = Core\BigInt::of(7)->format({ radix: 1 }); return '200 ok'; } return '200 ok'; } catch (ParseError $malformed) { return '400 the request body was not valid JSON'; } catch (TimeoutError $slow) { return '504 ' . $slow->message; } catch (RuntimeError $refused) { return '403 ' . $refused->message; } catch (Throwable $unexpected) { // Every error that no block above catches comes here. // The visitor gets one general reply, without the error's details. return '500 something went wrong on our side'; } }}
array<string> $paths = ['/', '/orders', '/export', '/admin', '/legacy'];foreach ($paths as string $path) { tainted string $status = Handler::reply($path); echo $path, ' -> ', $status, "\n";}/ -> 200 ok/orders -> 400 the request body was not valid JSON/export -> 504 the export service did not answer/admin -> 403 no session for this visitor/legacy -> 500 something went wrong on our sideChangelog
| Version | Change |
|---|---|
0.0.6 | Throwable::$message is tainted string, because some error messages contain input text. The text of a Core\Ldap\Filter and of a Core\Ldap\Dn is tainted string too. |
