Sending extra data in Java Exceptions to Flex Clients

When building Java services there is always need for handling exceptions thrown by all kind of sources. Wether those are ConstraintViolationException thrown by wrong database queries, NullPointerException thrown by wrong data manipulations or any other exceptions, all of them should end up handled in the Flex application calling the services.
One wrong usage of data transfer between client and backend services is when no matter what happens on the services, all exeptions are handled on the service and the client is basically listening only for the result method. Lets take a user management service for example. One method should be registerUser. The wrong usage would be that this method returns an generic object, if the register succeeded then it will return a User class (User class is mapped between Flex and Java using [RemoteClass] metadata in Actionscript) and if it fails then it will return an object with some messages and extra data in it. A better alternative to this is by using Java Exceptions, that are anyway handled in the services.

First, a custom exception need to be build, as in this example:

package com. myproject. exceptions;

public class UserServiceException extends Exception
{
    private static final long serialVersionUID = 1L;

    private String customMessage;

    public UserServiceException ( String customMessage )
    {
        this. customMessage = customMessage;
    }

    public String getCustomMessage ( )
    {
        return customMessage;
    }
}


This exception has an extra “customMessage” property that will hold the message sent from Java to Flex when registration is invalid. Of course you can add as many properties as you want in this exception. Lets take a look at how the exception is thrown:

public User registerUser ( User user ) throws UserServiceException
{
    //data processing such as transactions using Hibernate

    try
    {
        //updating such as session.save( user ) when using Hibernate
    }
    catch ( ConstraintViolationException e )
    {
                //exception thrown when there is another user with the same userName registered

        String message = "User with username " + user. getUserName ( ) +
            " already exists. Please choose another username";

        throw new UserServiceException ( message );
    }

    return user;
}

The code is pretty straight forward, if there is something wrong when trying to register a new user, we throw a custom UserServiceException. Lets take a look at how the exception is handled on the Flex part:

private function registerUser ( ): void
{
    var userService:RemoteObject =
        ServiceLocator. getInstance ( ). getRemoteObject ( "userService" );
   
    var user:User = new User ( );
    user. userName = "testUserName";
    user. firstName = "testFirstName";
    user. lastName = "testLastName";
    user. emailAddress = "testEmailAddress";
   
    var token:AsyncToken = userService. registerUser ( user );
    var responder:AsyncResponder =
        new AsyncResponder ( handleRegisterUserResult, handleRegisterUserFault, token );
    token. addResponder ( responder );
}

private function handleRegisterUserResult ( event:ResultEvent, token:AsyncToken ): void
{
    //process registration complete
    //event.result should be the new created User class
   
    var newRegisteredUser:User = event. result as User;
}

private function handleRegisterUserFault ( event:FaultEvent, token:AsyncToken ): void
{
    var errorMessage:ErrorMessage = event. message as ErrorMessage;
    var customMessage: String = errorMessage. rootCause. customMessage;
   
    //process and show custom message
}

We have the basic call to the service using AsyncToken and AsyncResponder. Where we need to look is in the handleRegisterUserFault method where an mx.messaging.messages.ErrorMessage is present in event.message. The rootCause property is the actual Exception that we had thrown from Java service. And in it we have our customMessage property. If there is need for extra properties those can be added in UserServiceException and will end up in the rootCause property as well.

This is a good alternative to keep the result handler for a service call nice and clean and all fault events, no matter if that is a fault from network connection or a fault returned by the service, should be processed on the fault handler method.

摘自 : http://blogs.eyepartner.com/adrian/flex/sending-extra-data-in-java-exceptions-to-flex-clients/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值