Propagating errors from model to presenter
- miska
- Member | 3
Hi,
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.
- miska
- Member | 3
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!
- Marek Bartoš
- Nette Blogger | 1275
new Exception
used in namespace Example
resolves to
Example\Exception
. Absolute class name needs backslash prefix
\Exception
or use statement use Exception;
https://www.php.net/…mespaces.php
- Toanir
- Member | 57
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
- jiri.pudil
- Nette Blogger | 1032
Is there some special Nette way of doing exception?
Nope, there is not. However, there are some general good practices you can follow; this blog post is imo a particularly good resource on exceptions in PHP.