How to set Other Exceptions when using Table::get($id)?


#1

Simple question:

How to set GoneExceptions when using Table::get($id)?

We now use like:

$post = $this->Posts->find()
            ->where(['Posts.id' => $id])
            ->first();
        if (!$post) {
            throw new GoneException('Custom error message');
        }

But how to use with $this->Posts->get($id);

Books says:

If the get operation does not find any results a Cake\Datasource\Exception\RecordNotFoundException will be raised. You can either catch this exception yourself, or allow CakePHP to convert it into a 404 error.

scrutinizer report bug:

Accessingcontact_idon the interfaceCake\Datasource\EntityInterfacesuggest that you code against a concrete implementation. How about adding aninstanceofcheck?


#2

Just catch the RecordNotFoundException that get throws, and throw your own GoneException?