You’re just about there. Take a look at get_object_vars in combination with json_encode and you’ll have everything you need. Doing:
json_encode(get_object_vars($error));
should return exactly what you’re looking for.
The comments brought up get_object_vars respect for visibility, so consider doing something like the following in your class:
public function expose() {
return get_object_vars($this);
}
And then changing the previous suggestion to:
json_encode($error->expose());
That should take care of visibility issues.