Forum Discussion

iftomkins's avatar
10 years ago

Get URL of user avatar, but get default image if user's account has been closed

We have a custom component that generates the author block of each topic/reply. The custom component pulls the avatar URL for the message author via the API. However, when the user has closed their a...
  • RobertT's avatar
    10 years ago

    Hi iftomkins

     

    Looks good, however I'd recommend wrapping in <attempt> tags to catch any errors should something go wrong. It is highly unlikely as this is fairly simple logic however this should cover most scenarios.

     

    <#attempt>
    <#if messageAuthorId == -1 >
        <#assign avatarUrl = "/html/assets/icon_anonymous_message.png" />
    <#else>
        <#assign avatarUrl = rest("/users/id/${messageAuthorId}/profiles/avatar/url").value />
    </#if>
    <#recover>
    <!-- what to do if the above logic fails or there is an error --> <#assign avatarUrl = "/html/assets/icon_anonymous_message.png" /> </#attempt>

    Wrapping in this around your code will catch any exceptions in the code should they occurr and instead of displaying the error it'll default to the anonymous user avatar you have specified.