Search code examples
javascriptexceptiontapestry

Hiding javascript exceptions in tapestry


In Tapestry I have a situation where I redirect user from Page A to Page B, where sometimes he is redirected to page C before page B was shown. This results in following javascript exceptions shown as red pop-ups like this one appearing on the page:

Exception invoking function Tapestry.Initializer.activate with parameters ["example"]: TypeError: $(...) is null

All elements listed as null are from Page B, which I don't want or intent to show. I cannot directly redirect user from Page A to Page C in this scenario.

I need a way in tapestry to handle those javascript exception by myself or hide them from appearing, or proper tapestry technique to redirect between pages without getting those errors. Currently I'm simulating user moving trough pages by launching redirecting code from @AfterRender function.

Edit: I've added relevant parts of the code below

On page one after user clicks modify I redirect him to user page

@InjectPage
UserDataPage userDataPage;

@OnEvent(value = EventConstants.ACTION, component = "modify")
Object onModify(Long userToEdit) {
    userDataPage.setUserId(userToEdit);
    userDataPage.setEditDetails(true);
    return userDataPage;
}

Since I really need to edit details UserDataPage does this:

@InjectPage
UserDetailsPage userDetailsPage;

@AfterRender
Object setup() {
    if(editDetails) {
        userDetailsPage.setUserId(userToEdit);
        return userDetailsPage;
    }
}

When UserDetailsPage loads it shows errors about components from UserDataPage that are missing(obviously, because I'm not on UserDataPage anymore). In Firebug I see that scripts contain all elemenents from both UserDataPage and UserDetailsPage. For example I have grid element on both pages, so resulting tapestry scripts refer both to grid and automatically renamed grid_0. Then the errors complain about missing grid from UserDataPage.

I cannot go directly from list of users(first page) to UserDetailsPage, as UserDetailsPage requires that redirect comes from UserDataPage. I'm not allowed to get rid of that requirement.


Solution

  • Proper tapestry way to redirect between pages is by returning page class, page name or link from event handler.

    public Object onActionFromMyLink() {
      return MyPage.class;
    }
    

    To hide tapestry client-side errors you can override javascript function Tapestry.error.

    Tapestry.error = function() {
      console.log(...);
    }
    

    But I think the issue is in your code.

    UPD:

    The best way to do navigation in tapestry is by using page activation context