3

I am overriding the addArticle and updateArticle methods of JournalArticleServiceImpl using a Hook. I am checking for all articles with a particular ddmStructureKey and that the current article has a unique value in a particular field.

I am throwing DuplicateEntryException exception when I find non-uniqueness. Within the curresponding catch method, I gave return null;. But it threw a NullPointerException. Then I tried to throw SystemException like follows.

try {
// logic
} catch (DuplicateEntryException e) {
    LOG.error("Value already present", e);
    throw new SystemException("Value already present", e);
}

But the result for the end users was as shown below. Even though in the logs, it displayed the actual error, it is not possible for users to understand what exactly happened in the background from this message.

enter image description here

I do not know how to display a custom error message to the end users from a Hook. Also to return to the same page to edit the same article.

Philip John
  • 5,275
  • 10
  • 43
  • 68
  • You shouldn't use hook for that. You should use ext or ext-plugin (it depends on version of your liferay) – Marcin Apr 06 '16 at 12:18
  • 1
    There's nothing in here that asks for an ext - it's totally unrelated. – Olaf Kock Apr 06 '16 at 12:56
  • Someone is crossposting this question(https://www.liferay.com/en/community/forums/-/message_boards/message/72660775) under your name. You'd certainly not do this yourself (http://meta.stackexchange.com/questions/141823/why-is-cross-posting-wrong-on-an-external-site), right? – Olaf Kock Apr 07 '16 at 10:21
  • @OlafKock I did not know that cross posting is not welcomed. Will make sure not to repeat it again. – Philip John Apr 07 '16 at 10:39
  • well, at least link those crossposts yourself so that those answering have a clue that the question might already be answered or discussed elsewhere – Olaf Kock Apr 07 '16 at 10:41

2 Answers2

2

Display error messages in Liferay:

You may use session messages, like <liferay-ui:error> tag.

For example in the jsp page:

<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
...
<liferay-ui:error key="err1" message="Third message" translateMessage="false"/>

or with exception, like in edit_article.jsp:

<liferay-ui:error exception="<%= ArticleContentSizeException.class %>" message="you-have-exceeded-the-maximum-web-content-size-allowed" />

You can define your own exception class and your own message-key and the value for the key in Language.properties.

And in the render method:

SessionErrors.add(renderRequest, "err1");

or when catching an exception (e) use this:

SessionErrors.add(renderRequest, e.getClass());

A complete example on github - portlet, github - hook

Peter B
  • 257
  • 2
  • 3
  • No no.. you have got it completely wrong. I am talking about Hook. Not a portlet. I know that we can implement SessionErrors in portlet. I want to know how to implement the same in a hook service. – Philip John Apr 07 '16 at 06:46
  • SessionErrors works the same way in a Service Wrapper Hook than in a portlet. I tried it in your scenario and it worked. I threw a ArticleContentSizeException in the addArticle method and the web content was not saved, moreover, on the screen there is a red error message: "You have exceeded the maximum web content size allowed.". [example on github](https://github.com/peterborkuti/hook-tutorial/tree/010_service-wrapper/service-wrapper00-hook) – Peter B Apr 07 '16 at 19:53
  • I replaced the `SystemException` with `ArticleContentException`. It would display error message on the screen. ` try { // logic } catch (DuplicateEntryException e) { LOG.error("Value already present", e); throw new ArticleContentException("Value already present", e); } ` But it displays `Please enter valid content.` always . Not able to update this error message with another custom message. But that should be fine. Do you know if we can update this message? Plus could you update the answer so that the post could be useful for others. – Philip John Apr 08 '16 at 07:43
  • OK, I updated my answer. I have to tell you that I am new in this site, but it is a bit peculiar to me, that there is no thank you in this site but only "No no.." And if turned out that "No no" is not only a bit rude, but also false, there is no "Sorry", moreover it is attached with another question and with a push to me to make more effort, instead of a light look into Liferay's code. I think, you only had to search for "Please enter valid content" than after "please-enter-valid-content" and it would point to the jsps where the errors are displayed with tag. – Peter B Apr 09 '16 at 15:36
1

As the UI layer seems to not expect any exception from these methods, this might call for changes in the UI- or Action-layer as well. When you do that, you might even get well along without service changes (because you can check upfront).

A hacky idea might be to not return null, but the duplicated value - simulating a successful update but returning the already existing article. Note that this is hacky, I'm not sure if it works always. If it breaks, please let me know how it breaks.

Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
  • I tried that for `updateArticle`. I used the same `articleId` and returned the original article the user is updating. But here, the message shown to the user is a successful update message instead of letting the user know that the update failed as he entered a duplicate value. – Philip John Apr 06 '16 at 13:03
  • Consider this solution is acceptable. I had tried this and I could not continue this because for `addArticle`, there is no article in database to return. I am finding it difficult to create an empty JournalArticle object and return back. – Philip John Apr 06 '16 at 13:04