CkRequestException
What CkRequest throws — only when asked.
Raised by a CkRequest reader called with throwIfInvalid: true
when the entry under its key is missing or cannot be converted — a flat one, or one on a
Number, Date or
Uuid entry — then Key names the key, and Reason says why in
the words the owning reader uses, with that reader's own exception behind it as the previous
one: a CkDictionaryException, a CkNumberException, a CkDateException or a
CkUuidException. Under CkSettings::DefaultThrowIfInvalid, the development
switch, the first read throws too when the request holds nothing the readers can read; then
Key is null and the message names the class alone. Never thrown otherwise: by default a reader
answers its fallback, a request that cannot be read leaves the bags empty with
Explain saying why, and Bag with its two parts and the method, Has,
MissingKeys, Explain and every OrNull twin never throw at all. A final class, derived
from CkException.
The message names the method and the key, and says why:
CkRequest.String: cannot read key "email" (String) — no entry under the key.
CkRequest.Number.SmallInt: cannot read key "qty" (String) — not a whole number (exact: true).
CkRequest.Date.CarbonDay: cannot read key "birthday" (String) — no entry under the key.
CkRequest: cannot read the request — no body.
Properties
| Property | Type | What it holds |
|---|---|---|
Key |
?string |
The key that failed; null when the request itself could not be read. |
Input |
mixed |
From CkException: the value that failed, untouched. |
Reason |
string |
From CkException: why, in a few words. |
Quick look
use SafeCoder\CommonKit\CkRequest;
use SafeCoder\CommonKit\Exceptions\CkRequestException;
$_SERVER['REQUEST_METHOD'] = 'POST';
$_SERVER['CONTENT_TYPE'] = 'multipart/form-data';
$_POST = ['qty' => '3.7'];
try {
CkRequest::Number('qty')->Integer(exact: true, throwIfInvalid: true);
} catch (CkRequestException $error) {
echo $error->Key; // -> 'qty'
echo $error->Reason; // -> 'not a whole number (exact: true)'
echo $error->Input; // -> '3.7'
}