Skip to content

ParseError

The error a program throws when input does not match the format it should have: text that is not valid JSON, a field with the wrong type, or a document that is nested too deeply.

ParseError is a kind of RuntimeError, because bad input comes from outside the program.

A ParseError has a message like every error, and it also has issues. issues is a list with one entry for each problem that was found. Each entry has a path, which is the name of the field, and a message, which says what is wrong with that field. A form with three bad fields throws one ParseError with three entries. Your reply can then tell the person about all three fields at once.

Good to know: when the whole document is broken, issues has one entry and its path is an empty string.

The examples below show a form with two bad fields that are reported together, a document that is not valid JSON, and a JSON reply that names each wrong field of a signup.

Examples

Every bad field at once

<?nvs
// A form arrives as text, whatever the fields are meant to hold. Reading it
// into the shape the rest of the program wants reports every field that did
// not fit, not just the first one.
try {
Core\Arr::shapeAs<{ name: string, age: int, subscribed: bool }>([
'name' => 'Ada',
'age' => 'thirty',
'subscribed' => 'yes',
]);
} catch (ParseError $bad) {
echo 'this form has ', Core\Arr::count($bad->issues), " problem(s):\n";
// Each entry says where the trouble was and what it was.
foreach ($bad->issues as ({ path: string, message: string }) $issue) {
echo ' ', $issue->path, ': ', $issue->message, "\n";
}
}
Output
this form has 2 problem(s):
age: expected int, found a string
subscribed: expected bool, found a string

A document that is not json

<?nvs
// Some errors are not about one field. A broken document throws a `ParseError` with one entry.
// The entry's path is empty, because no single field caused the error.
try {
Core\Json::decode("{\"total\": }");
} catch (ParseError $broken) {
echo 'entries=', Core\Arr::count($broken->issues), "\n";
echo 'path=[', $broken->issues['0']->path, "]\n";
echo $broken->message, "\n";
}
// `maxDepth` limits how deeply a document may be nested. A deeper document
// throws a `ParseError`, so a hostile document cannot use up the server.
try {
Core\Json::decode('[[[1]]]', { maxDepth: 2 });
} catch (ParseError $deep) {
echo $deep->message, "\n";
}
// A valid document throws no error.
echo Core\Json::encode(Core\Json::decode("{\"total\": 3}")), "\n";
Output
entries=1
path=[]
Core\Json::decode(): expected value at line 1 column 11
Core\Json::decode(): nesting is deeper than the `maxDepth` of 2 at line 1 column 2
{"total":3}

A reply that names every bad field

<?nvs
// The everyday use: a signup arrives as a document, and the reply names every
// field that was wrong. Somebody filling in a form sees all of it at once
// rather than fixing one field per attempt.
#[Core\Json\Derive]
class Signup
{
public string $email;
public int $age;
public bool $terms;
public function constructor(string $email, int $age, bool $terms)
{
$this->email = $email;
$this->age = $age;
$this->terms = $terms;
}
}
string $body = "{\"email\":\"ada@example.com\",\"age\":\"thirty six\",\"terms\":\"on\"}";
try {
var $signup = Core\Json::decodeAs<Signup>($body);
echo 'welcome ', $signup->email, "\n";
} catch (ParseError $bad) {
// One entry becomes one field of the answer, keyed by the field it is about.
array<string> $fields = [];
foreach ($bad->issues as ({ path: string, message: string }) $issue) {
$fields[$issue->path] = $issue->message;
}
echo Core\Json::encode({ status: 'invalid', fields: $fields }), "\n";
}
Output
{"fields":{"age":"expected int, found a string","terms":"expected bool, found a string"},"status":"invalid"}