Forum Discussion

Gursimrat's avatar
Gursimrat
Leader
11 years ago

Freemarker If/else not working, strange.

I am not able to handle the exception here

 

Below is the response XML which is giving the error:

 

<response status="error">
<error code="503">
<message>An object in the path is null.</message>
</error>
</response>

I check the status of the response from the status attribute and if the status is equals to succes, i pass the returned value, else a default value.

 

But the issue is, it is not working. Below is my FTL code for the same. The error block doesn't return anything. Why?

 

		<#assign check_status = rest("/users/id/${user.id}/profiles/avatar/size/profile")>
<#assign user_avatar = rest("/users/id/${user.id}/profiles/avatar/size/profile").image> <#if check_status.@status = "success"> SUCCESS: ${check_status.@status} <#else> ERROR: ${check_status.@status} </#if>

 

  • AdamN's avatar
    AdamN
    11 years ago

    I'm a little late to the party, but thought I'd share a work-around I've used in the past for this situation. In the code below, I check first to see whether the user has an avatar. I do this by seeing if the object returned via the REST API is not null. If it's not null (there's an image), then I make an additional REST API call to get the avatar in the desired size.

     

    <#attempt>
      <#assign avatarObj = restadmin("/users/id/${user.id?c}/profiles/avatar").image />
      <#if !((avatarObj.@null[0])?? && avatarObj.@null=="true") >
        <#assign avatar = restadmin("/users/id/${user.id?c}/profiles/avatar/size/profile").image />
      </#if>
    <#recover>
    </#attempt> 
    
    ${avatar.url!""}

     I hope this helps!

  • Line 2 will throw an error if the rest call returns null (e.g. there is not avatar or the user is not logged in), or does not contain a property called 'image'. Have you checked the value this returns?
  • NicoB's avatar
    NicoB
    Lithium Alumni (Retired)

    Hi ,
    I suspect that the firs assign is generating an expection and the whole code after it is being skipped.
    Why don't you try to wrap that #assign into an <#attempt><#recover> block? Something like this:

     

    <#attempt>
      <#assign check_status = rest("/users/id/${user.id}/profiles/avatar/size/profile")>
      <#assign test=check_status.@status/>
    <#recover>
      <#assign test = "error"/>
    </#attempt>

     

    <#if test=="success">
       SUCCESS: ${test}
    <#else>
      ERROR: ${test}
    </#if>

     

    I hope this helps.

     

    Nico

    • Gursimrat's avatar
      Gursimrat
      Leader
      Hi NicoB,

      This is also giving the Widget Could not be displayed error on page
      • PaoloT's avatar
        PaoloT
        Lithium Alumni (Retired)

        Hi Gursimrat 

         

        the "Widget could not be displayed" text is used to mask the full stack trace (so that it is more user friendly) - if you need to know the precise exception you can always ask support to look for that in your logs.

         

        I suspect the problem may be that in some scenarios the check_status variable may not be initialized correctly. Have you tried setting it to a value (such as an empty string) in the recover block, and then check for that before checking the status attribute?

         

        Thanks,

        Paolo

  • Hi Nico nathan 

     

    Below is my actual excerpt from the code which is giving issues. When the returned status is not success, it shoud print the text No Avatar Text Should be displayed on the screen, but instead, it shows Widget Could no be displayed 2 times. It is working fine for Success case.

     

     

     

    <!-- If user is logged in-->
    <#if user.registered == true>
    
    
    <#attempt>
    	<#assign check_status = rest("/users/id/${user.id}/profiles/avatar/size/profile")>
    <#recover>
    	<!-- Just checking the status of the REST call in the Attempt block -->
    </#attempt>
    	
    		<#if check_status.@status = "success">
    	           
    				<#assign user_avatar = rest("/users/id/${user.id}/profiles/avatar/size/profile").image>
    				<#assign user_profile = rest("/users/id/${user.id}?restapi.response_style=view").user>
    				<div class="avatar-mash-area">
    					<img src="${user_avatar.url}" alt="${user.login}" title="${user.login}" height="64" width="64"/>
    					<#assign pvt_msg_count = rest("/users/id/${user.id}/mailbox/notes/inbox/unread/count")>	
    						<div class="avatar-links-mash">
    				
    						<p>Hello, <a href="${user_profile.@view_href}" title="${user.login}"> ${user.login}</a></p>
    						<#if pvt_msg_count.value?number &gt; 0>
    						<p class="settings-links"><a href="/t5/user/myprofilepage/tab/personal-profile">SETTINGS</a><a href="/t5/notes/privatenotespage" title="${pvt_msg_count.value} Unread Messages"><span class="unread-pvt-msg">MESSAGES</a><a href="/t5/help/faqpage">HELP</a><a href="${webUi.getUserLogoutPageUrl("/")}">SIGN OUT</a></p>
    						<#else>
    						<p class="settings-links"><a href="/t5/user/myprofilepage/tab/personal-profile">SETTINGS</a><a href="/t5/notes/privatenotespage" title="No Unread Messages">MESSAGES</a><a href="/t5/help/faqpage">HELP</a><a href="${webUi.getUserLogoutPageUrl("/")}">SIGN OUT</a></p>
    						</#if>
    						</div>
    				</div>
    			<#else>
    				
    No Avatar Text Should be displayed
    </#if> <#else> <!-- If user is Not logged in--> <div id="buttonCol" class="signupCol clearfix"> <div class="link-btn black-btn -btn"> <@component id="users.action.login" /> </div> <div class="link-btn blue-btn -btn"> <@component id="users.action.registration" /> </div> </div> </#if>

     

     

     

  • Below is the exception that is getting printed, I am unable to figure it why it is giving error, any help?

     

    The following has evaluated to null or missing:
    ==> check_status  [in template "preview" at line 108, column 22]
    
    Tip: If the failing expression is known to be legally null/missing, either specify a default value with myOptionalVar!myDefault, or use &lt;#if myOptionalVar??>when-present&lt;#else>when-missing&lt;/#if>. (These only cover the last step of the expression; to cover the whole expression, use parenthessis: (myOptionVar.foo)!myDefault, (myOptionVar.foo)??
    
    The failing instruction (FTL stack trace):
    ----------
    ==> #if check_status.@status??  [in template "preview" at line 108, column 17]
        #if user.registered  [in template "preview" at line 101, column 1]
    ----------
  • Following code has worked, but strangely, if the user for which i am checking is not an admin, it always shows widget could not be displayed.

    If I assign Admin role to him, this works fine, even if I make rest calls as 

     

    <#assign xyz= restadmin("dummy_rest_url")>

     

    <#attempt>
    	<#assign check_status = restadmin("/users/id/${user.id}/profiles/avatar/size/profile")>
    	<#assign test = check_status.@status>
    <#recover>
    	<#assign test = "">
    </#attempt>

     

    • AdamN's avatar
      AdamN
      Khoros Oracle

      I'm a little late to the party, but thought I'd share a work-around I've used in the past for this situation. In the code below, I check first to see whether the user has an avatar. I do this by seeing if the object returned via the REST API is not null. If it's not null (there's an image), then I make an additional REST API call to get the avatar in the desired size.

       

      <#attempt>
        <#assign avatarObj = restadmin("/users/id/${user.id?c}/profiles/avatar").image />
        <#if !((avatarObj.@null[0])?? && avatarObj.@null=="true") >
          <#assign avatar = restadmin("/users/id/${user.id?c}/profiles/avatar/size/profile").image />
        </#if>
      <#recover>
      </#attempt> 
      
      ${avatar.url!""}

       I hope this helps!