从 GWT 客户端中的 Restlet 框架接收自定义异常 [英] Receive custom exceptions from Restlet Framework in GWT client

查看:15
本文介绍了从 GWT 客户端中的 Restlet 框架接收自定义异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对 Restlet 框架和 GWT 客户端使用异常处理.Restlet 框架支持本文中描述的带注释异常的概念;

并在 GWT 客户端调用 onFailure();

私有类 PostLocationCallback 实现了 AsyncCallback{...@覆盖public void onFailure(Throwable pCaught){mCallback.onFailure(pCaught, mLocationDto);}}

但参数 pCaught 仅包含状态码为 409 的 ResourceException.我的 LocationNameException 未包含在根本原因堆栈中.我需要此 LocationNameException 以进行适当的错误消息处理.

原因是Restlet GWT ClientProxyGenerator生成的ServerResourceProxy LocationListServerResourceProxyImpl;

public void postLocation(LocationDto param1, final LocationDto> callback){...公共无效句柄(请求请求,响应响应){如果 (getClientResource().getStatus().isError()){callback.onFailure(new ResourceException(getClientResource().getStatus()));}别的{...}

我想我必须重写ClientProxyGenerator中的Post方法;

LocationNameException 存在于响应数据中,所以

在ServerResourceProxy(ClientProxy)中指定接口异常即可

可以立即在您的项目中使用此自定义 ClientProxyGenerator().

在 GWT 模块 XML 中,将 ClientProxyGenerator 更改为服务器上的新版本;

并且您已经准备好使用您的自定义异常,但如果此扩展能够集成到 Restlet 框架中,那就太好了.

I want to use Exception handling with the Restlet Framework and GWT clients. The Restlet Framework supports the concept of annotated exceptions as described in this post;

http://restlet.com/company/blog/2015/12/21/exception-handling-with-restlet-framework/

In my project i created a LocationNameException

@Status(value = 409)
public class LocationNameException extends Exception
{
    ...

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    public LocationNameException(String pMessage, Throwable pCause)
    {
        super(pMessage, pCause);
    }
}

And use this in my ServerResource;

   @Override
    @Transactional(rollbackOn = LocationNameException.class)
    public LocationDto postLocation(LocationDto pLocationDto) throws LocationNameException
    {
        ...

        Location lLocation = new Location(pLocationDto);

        try
        {
            LocationDao lLocationDao = getLocationDao();
            lLocationDao.persist(lLocation);
        }
        catch (PersistenceException pPersistenceException)
        {
            throw new LocationNameException("Location requires unique Name", pPersistenceException);
        }

        ...

        return lLocationDto;
    }

With the interface

public interface LocationListServerResourceInt
{
    ...

    @Post
    LocationDto postLocation(LocationDto pLocationDto) throws LocationNameException;

    ...
}  

This works, in the case of an exception the call returns code 409;

And at the GWT client side onFailure() is called;

private class PostLocationCallback implements AsyncCallback<LocationDto>
{
    ...

    @Override
    public void onFailure(Throwable pCaught)
    {
        mCallback.onFailure(pCaught, mLocationDto);
    }
}

But parameter pCaught only contains a ResourceException with the status code 409. My LocationNameException isn't included in the underlying cause stack. I need this LocationNameException for appropriate error message handling.

The reason is the generated ServerResourceProxy LocationListServerResourceProxyImpl by the Restlet GWT ClientProxyGenerator;

public void postLocation(LocationDto param1, final LocationDto> callback)
{       
    ...

    public void handle(Request request, Response response)
    {
       if (getClientResource().getStatus().isError())
       {       
          callback.onFailure(new ResourceException(getClientResource().getStatus()));
       }
       else
       {

    ...

}

I think i have to rewrite the Post method in the ClientProxyGenerator;

The LocationNameException is present in the Response data so the Basic approach using the getResponseEntity() method of the ClientResource class should be possible.

Is this the way to go? Or can i catch the LocationNameException exception somewhere else as suggested by Catching annotated exceptions?

It is really hard to try a different approach because of the generated code. Is there an easy way to circumvent the code generator with custom classes?

解决方案

With help of Jerome Louvel and Thierry Boileau i created a new ClientProxyGenerator() that supports custom exceptions towards a GWT client;

Just specify the exception from the interface in the ServerResourceProxy (ClientProxy)

and voilà

It is possible to use this custom ClientProxyGenerator() in your project right away.

Download custom ClientProxyGenerator

And place it in a package on the server (for example package com.ludus.server.util)

In GWT module XML change the ClientProxyGenerator to the new version on the server;

And you're ready to go with your custom exceptions, but it would be nice if this extension would be integrated in the Restlet framework.

这篇关于从 GWT 客户端中的 Restlet 框架接收自定义异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆