Catching errors in Play framework. Best practice?

2,114 views
Skip to first unread message

Drew Hamlett

unread,
Jul 23, 2011, 11:33:35 PM7/23/11
to play-framework
What is the best way to catch errors in play framework. I'm just
wondering what everyone else does. Lately I've been surrounding the
entire block of each controller method with a try catch with a general
Exception and then using the error() method in the catch. Trying to
find best practice on this. Thanks.

Ivan Meredith

unread,
Jul 24, 2011, 12:00:12 AM7/24/11
to play-fr...@googlegroups.com
Catching errors is only useful if you know how to handle them usually. Anything else and I would probably just return a 500 error, which i think happens already?


--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.


Mark Piper

unread,
Jul 24, 2011, 9:35:43 AM7/24/11
to play-fr...@googlegroups.com
Play gives you a bit of magic to do this http://www.playframework.org/documentation/1.2.2/controllers#catch.  That at least gives you the opportunity to reduce duplication.

Nicolas Girardin

unread,
Jul 29, 2011, 5:47:03 PM7/29/11
to play-fr...@googlegroups.com
Usually, all my controller extends BaseController which is in charged of handling all the exceptions and rendering the HTTP 500 error. The MailUtils.sendErrorMail() send me an email when an exception occurs so I've not to skim trough the logs all day long.

public class BaseController extends Controller {
/**
* Catch any exception occurring into the controller
*/
@Util
@Catch(value = Exception.class)
public static void catchExceptions(Exception e) {
Logger.error(e, "BaseController catched an exception");
MailUtils.sendErrorMail(e);
error();
}
}

Hope it helps!

Nicolas
On Sun, Jul 24, 2011 at 3:35 PM, Mark Piper <revb...@gmail.com> wrote:
Play gives you a bit of magic to do this http://www.playframework.org/documentation/1.2.2/controllers#catch.  That at least gives you the opportunity to reduce duplication.

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/hl9tMIdOwtAJ.

Xuefeng Wu

unread,
Aug 1, 2011, 12:07:43 AM8/1/11
to play-fr...@googlegroups.com
Hi Nicolas,

It's useful, It's better it can send mail by a job.
Reply all
Reply to author
Forward
0 new messages