Just my few cents to this.
I'm using something very similar to what Sebastien wrote but because I have 
several different ModelObject Types where I need this, I wrote it a bit more 
generic:

/**
 * @param   <T>  Type of Object
 *
 * @author  mrichter
 */
public abstract class ExceptionHandledModel<T> extends 
LoadableDetachableModel<List<T>> {

    private static final Logger LOG = 
LoggerFactory.getLogger(ExceptionHandledModel.class);

    @Override
    protected List<T> load() {
        try {
            return this.onLoad();
        } catch (Exception e) {
            LOG.error("Failed to load List of Type " + T, e);
            this.onError(e.getMessage());
        }
        return Collections.emptyList();
    }

    protected abstract List<T> onLoad() throws Exception;
    protected abstract void onError(String message);
}

Best,
Marvin


-----Original Message-----
From: Sebastien [mailto:seb...@gmail.com] 
Sent: Wednesday, January 22, 2014 2:43 PM
To: users@wicket.apache.org
Subject: Re: Handling exceptions thrown by a model

Hi Olivier,

I simple way to achieve this is to have a (abstract) #onError method:

public abstract class MyModel extends
LoadableDetachableModel<List<MyObject>>
{
    private static final long serialVersionUID = 1L;
    private static final Logger LOG =
LoggerFactory.getLogger(DashboardModel.class);

    public MyModel()
    {
    }

    @Override
    protected List<MyObject> load()
    {
        List<MyObject> list = new ArrayList<MyObject>();

        try
        {
            //TODO fill list;
        }
        catch (TheException e)
        {
            this.error(e.getMessage(), e);
        }

        return list;
    }

    private void error(String message, Exception e)
    {
        LOG.error(message, e);
        this.onError(message);
    }

    protected abstract void onError(String message); }

In MyPage

private MyModel newMyModel()
{
    return new MyModel() {

        private static final long serialVersionUID = 1L;

        @Override
        protected void onError(String message)
        {
            MyPage.this.error(message);
        }
    };
}


Hope this helps,
Sebastien.


On Wed, Jan 22, 2014 at 2:07 PM, Oliver B. Fischer <mails...@swe-blog.net>wrote:

> I have implementation of LoadableDetachableModel which might throw an 
> exception in load(). Unfortunately this exception might be thrown 
> while rendering the page.
>
> Currently I handle the exception with an AbstractRequestCycleListener 
> added to the application itself.
>
> But don't like it because I don't want to add an 
> AbstractRequestCycleListener for each possible exception.
>
> It is possible to handle specific exceptions page local? I tried to 
> add a custom Behaviour implementation to my page but the onException() 
> method was never called.
>
> Any idea how to handle such exceptions locally?
>
> Oliver
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
> For additional commands, e-mail: users-h...@wicket.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org

Reply via email to