How to best handle error in Button click action

Oct 25, 2010 at 7:33 AM

Hi,

I have the following code running when I press a button:

 

        public IEnumerable ShowDevices()
        {
            yield return Show.Busy();

            FetchResult fetchResult = Portal.Fetch();
            yield return fetchResult;

             yield return Show.Child().In().Configured(m => m.Devices = fetchResult.Result);

            yield return Show.NotBusy();
        }

 

FetchResult is preatty straight forward:

 

        public void Execute(ActionExecutionContext context)
        {
            ApmtBusinessListBase.GetList((sender, args) =>
                                                          {
                                                              Result = args.Object;                                                              
                                                              Completed(args, new ResultCompletionEventArgs { Error = args.Error });
                                                          });
        }

The problem here is that if the Error is not null the SequentialResult breaks and the busy screen never get's hidden + I can't show the error to the user (it gets logged).
An ideea on how to best handle this ?

Regards,

 

 

Oct 25, 2010 at 8:14 PM

I don't know if it's the best way but I have been using a custom IResult and always make sure that the Result gets the Completed event fired so I can take care of it from the trigger for example

    public interface IRpcResult : IResult
    {
        [Import]
        IServerConnection Server { get; set; }
        bool Succeeded { get; }
        Exception RpcException { get; }
    }
    public class RpcConnectResult : IRpcResult
    {
        [Import]
        public IServerConnection Server { get; set; }
        public bool Succeeded { get; private set; }
        public Exception RpcException { get; private set; }

        public void Execute(ActionExecutionContext context)
        {
            if (Server.Socket.Connected)
                return;
            SocketAsyncEventArgs connectArgs;
            connectArgs = new SocketAsyncEventArgs();
            connectArgs.RemoteEndPoint = new DnsEndPoint(Server.ServerHost, Server.ServerPort);
            connectArgs.Completed += new EventHandler(OnConnect);
            var callback = Server.Socket.ConnectAsync(connectArgs);
            if (!callback)
                OnConnect(this, connectArgs);
        }

        void OnConnect(object sender, SocketAsyncEventArgs e)
        {
            e.Completed -= OnConnect;
            if (e.SocketError != SocketError.Success)
            {
                Succeeded = false;
                RpcException = e.ConnectByNameError;
                Completed(this, new ResultCompletionEventArgs());
            }
            else
            {
                Succeeded = true;
                Completed(this, new ResultCompletionEventArgs());
            }
        }

        public event EventHandler Completed = delegate { };
    }

And then you can use it like this

        IEnumerable Connect()
        {
            var connect = Rpc.Connect();
            yield return connect;
            if (!connect.Succeeded)
                yield return ShowMessageBox.With(connect.RpcException.ToString());
        }