2

I have the following code in a velocity template theme:

#set($currLayout = $layoutLocalService.getFriendlyURLLayout($group_id, true, $currFriendlyUrl))

which throws a

com.liferay.portal.NoSuchLayoutException
    at com.liferay.portal.service.impl.LayoutLocalServiceImpl.getFriendlyURLLayout(LayoutLocalServiceImpl.java:959)

when matching layout not exist.

How could I catch this exception in my velocity template?

Maria Ioannidou
  • 1,544
  • 1
  • 15
  • 36
  • In Liferay you can rely on the fetch*() method, as it doesn't throw Exceptions and returns null if no result was found. – papgyo Nov 11 '14 at 10:36

1 Answers1

3

To my knowledge, it is not possible to catch exceptions inside a velocity template. Generally, there are two ways of dealing with such a situation:

  • handle the exception in the application which uses the template, such as a servlet
  • avoid throwing exceptions; instead, have the method always return a valid (default) value, or null if that is not possible

The velocity manual recommends to use the second option whenever possible.

wau
  • 830
  • 7
  • 20
  • You are right. After some research I found out it is not possible to catch exceptions inside a velocity template. On the other hand, the method I posted "$layoutLocalService.getFriendlyURLLayout" is a Liferay's method where I do not have control over its behavior, and what it does is throwing an exception if layout does not exist. So a solution would be to try to override somehow this method or create a hook overriding some servlet in liferay. The other approach, which I finally followed, was to try to ensure that all layouts exist, putting this code in conditional statements. – Maria Ioannidou Apr 23 '13 at 09:26
  • I just wondered whether a more straight-forward safe approach for such cases existed. Thanks a lot for your answer anyhow! – Maria Ioannidou Apr 23 '13 at 09:32
  • 2
    Your solution seems fine to me, but I am not familiar with Liferay. Maybe it is possible to define a default layout? – wau Apr 24 '13 at 08:43
  • Sounds a good idea in general, thank you. That could be another question for the liferay people, whether it is possible to set a default layout. Although from the liferay API it does not seem to be supported out of the box. http://docs.liferay.com/portal/6.1/javadocs-all/com/liferay/portal/service/LayoutLocalService.html#getFriendlyURLLayout%28long,%20boolean,%20java.lang.String%29 – Maria Ioannidou Apr 24 '13 at 09:24