I'm new and I'm struggling a little how to propagate errors from model where they occur – data validation, database connections, fetches and stuff to a presentation layer. I can drop model and do everything in presenter or I can pass the presenter object to model which is only slightly better. What is the correct way to handle this? Would expect some kind of exceptions, but can't find anything relevant anywhere.
Thanks for any pointers.
My bad, I see the problem now, missing
\, should be
throw new \Exception('Something bad happened.'); definitely tried
it but as Tracy caught it and the error page seemed same at first glance didn't
noticed it worked. Sorry for the confusion. So solved for me, now I can try
playing with Exception types and such and error handling in general. Thanks!
- Member | 52
As CZechBoY suggests, do use exceptions for exceptional situations – edge
cases that don't make sense to be handled at the point of code. For example if
you're designing a
Model::getRow() service which communicates with
DB and an error occurs, you can choose typically between these approaches:
- catch possible exceptions in the
Model::getRow()(, log it) and return null
- do not catch the exception (ideally describe the throw behavior by phpDoc annotation) and catch it where it makes sense, usually a Presenter
As far as handling the exception is concerned, you might be interested in the catchExceptions parameter . In short, Tracy catches and displays exceptions while in debug mode but in production mode, they end up in the errorPresenter, where you can decide what to display to the user