Forum Discussion

id-chalmers's avatar
11 years ago

freemarker error handling in rest api

I'm using the rest api (which is great, thanks!) to create a custom component, pulling recently solved messages from a large number of boards on my community. The rest call references boards by id, adds their messages to a giant list and then re-orders by date, serving up the 6 most recent. In testing I noticed that a typo in the board dispid would generate a freemarker template error that breaks the page:

 

code="101" ; message="No board with the specified dispid."

 

Everything is great and there are no errors now but I want to incorporate error handling in case somebody changes or deletes one of the (many) boards that i'm referencing in the rest call. I've tried the classic "<#if message?? && message?has_content >" test but that's too late in the process to prevent the freemarker template error. What can I do to test if the board id exists in the community before i use the rest api call?

 

Thanks!

  • Hi id-chalmers 

     

    have you tried with the attempt / recover freemarker directive?

     

    <#attempt>
    <#assign board = rest("boards/id/iDoNotExist").board />
    <#recover>
    Something went wrong
    </#attempt>
    

     

    Hope it helps

2 Replies

  • PaoloT's avatar
    PaoloT
    Lithium Alumni (Retired)
    11 years ago

    Hi id-chalmers 

     

    have you tried with the attempt / recover freemarker directive?

     

    <#attempt>
    <#assign board = rest("boards/id/iDoNotExist").board />
    <#recover>
    Something went wrong
    </#attempt>
    

     

    Hope it helps