XF 2.2 Returning error from repository

stromb0li

Active member
Is it ok practice to return an error from a function in a repository class? When trying to reference the error() method used by a controller, I noticed it not available for use.

Similarly, what is the proper way to reference the error method from the Repository (not sure why, but IntelliSense in VScode for me doesn't recognize the method, even in my controllers)?
 
You can't return a reply (including an Error) from a repository directly, this would have to be done through the controller.

What you can do is throw an exception with an error reply:

PHP:
throw new \XF\Mvc\Reply\Exception(new \XF\Mvc\Reply\Error($error, $code));

Only you can answer if that would be okay for your use case or not :)
 
Potentially better is to throw a printable/phrased exception, which will normally be caught by the dispatcher and returned as an error reply but is also potentially safer for callers outside of the dispatch loop:

PHP:
throw new \XF\PrintableException('Whoops...');
// or
throw \XF::phrasedException('some_oops_phrase');
 
Top Bottom