JSON serialized error output from bitcoin-cli – Bitcoin Stack Exchange

Is there any way to do bitcoin-cli JSON serialized output errors? For example, if I do

bitcoin-cli getblock foo

the output is

Error code: -5
Error message:
Block not found

I want the output to be something more like:

{
"error": {
"code": -5,
"message": "Block not found"
}
}

Behind the scenes, this is what the RPC response body looks like, and I want to tell bitcoin-cli, "Do not print the error if there is one, just give me the JSON response."