Question

I have pojo classes that handle my backend connections. I want to encapsulate my (backend) error handling in these classes by catching backend exceptions inside.

Is there any way to access the current wicket page (or any component for that matter) to enable me to give feedback to the user from outside the wicket component hierarchy?

class MyService {
...
public void doBackEndThing(){
    try {
        backEndService.doRemoteCall();
    } catch (BackendException e) {
        //we're not inside the component hierarchy! so no getPage() available
        WebPage page = getCurrentPage(); 
        page.error("Backend is currently not available");
    }
}

I've tried the PageManager, but I have no idea how to retrieve the correct version and so I do not know if would work at all:

int version = ?;
WebPage page = (WebPage )Session.get().getPageManager().getPage(version);
Was it helpful?

Solution

There isn't a nice way and it doesn't seem to be a good idea to do this. Your frontend should call your backend not the other way. So the easiest way to do this would be to store the errors inside your service and have your page get these.

class MyService {

    private String error;

    public void doBackEndThing(){
        try {
            backEndService.doRemoteCall();
        } catch (BackendException e) {
           error ="Backend is currently not available";
        }
    }
}

and

class MyPage extends WebPage {

    private MySerivce service;

    public void doSomethingFrontendy() {

        error = service.getError();
    }
}

or you could return an error from your backend method or throw an Exception and handle this in your WebPage or use IRequestCycleListener#onException() like @svenmeier pointed out.

OTHER TIPS

IRequestCycleListener#onException() is a better place for this - you can get access to the current page via RequestCycle#getActiveRequestHandler().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top