ReactiveCommand: get command parameter in ThrownExceptions

477 views Asked by At

In my scenario I use command to delete something from DB. I have ReactiveCollection bound to DataGrid in WPF and following code for handling delete:

 RemoveProductCommand = ReactiveCommand.CreateFromTask<Product>(async product =>
            {
                await _licensingModel.RemoveProduct(product.Id);
            });


            RemoveProductCommand.ThrownExceptions.Subscribe(async ex =>
            {
                await ShowToUserInteraction.Handle("Could not remove product ");
            });

            Products.ItemsRemoved.Subscribe(async p =>
            {
               await RemoveProductCommand.Execute(p);
            });

My question is, is there a built-in way for getting product (command argument) in ThrownExceptions?

I can of course do something like that:

 RemoveProductCommand = ReactiveCommand.CreateFromTask<Product>(async product =>
            {
                try
                {
                    await _licensingModel.RemoveProduct(product.Id);
                }
                catch (Exception e)
                {
                    throw new ProductRemoveException(product, e);
                }

            });

My reasoning is that I want to notify user with information "Could not remove product X".

EDIT: Alright, I edited code for pasting here and noticed difference between awaiting command.Execute() and Subscribing. It is possible to:

  Products.ItemsRemoved.Subscribe(async p =>
            {
                try
                {
                    await RemoveProductCommand.Execute(p);
                }
                catch (Exception e)
                {

                }
            });

but what about ThrownExceptions?

1

There are 1 answers

1
mm8 On BEST ANSWER

My question is, is there a built-in way for getting product (command argument) in ThrownExceptions?

No, ThrownExceptions is an IObservable<Exception>, i.e. you subcribe to it to get a stream of Exceptions only. It doesn't know about any command arguments that were passed to the command before the actual exception occured.

You will need to store this parameter value yourself somehow, for example by defining and throwing a custom Exception from your Execute method.