Freigeben über


Using Rx? Subscribe to exceptions!

I have an app in the Windows Phone Marketplace which suddenly saw a spike in Little Watson email reports back to me last weekend. Each one looked like the last so I responded back to each user (always keep the user’s happy!) and went to investigate.

The service it was talking to suddenly changed its behavior unexpectedly. This caused a ripple effect in that my WebClient code calls started to fail and one backwater section of my app did not have good error handling. So a service error (not my fault) caused the app to crash (my fault big time!). I had dealt with it nearly everywhere but missed it in one place in which it had never happened before.

Using the Rx pattern discussed before my code looked something like this

 1 var outstandingRequest = svc.GetDataFromWeb(id)
2     .SubscribeOnDispatcher()
3     .Subscribe( data => DoSomething(data));

Notice something? I’m not using the overload of Subscribe() which provides the exception handling. If you are subscribing, its a good idea to grab the errors that come out of the observable. Don’t do it and the phone app crashes.

A small but important change handles the exception and solved the problem. (In reality I added a little more complex logic than shown here so that it would retry the request to the service). Next step was to quickly get it into the marketplace.

 1 var outstandingRequest = svc.GetDataFromWeb(id)
2     .SubscribeOnDispatcher()
3     .Subscribe( 
4         data => DoSomething(data),
5         ex => { App.LogError("Error loading data {0}: {1}", id, ex),
6         () => 
7     );

Unfortunately the marketplace crash data isn't real time so I didn’t have this graph available until yesterday. That hockey stick shape is not something you want to see on your dashboard!

image

Hopefully the users weren't scared away from the app by the experience. Its easier to get them to come in the first place than it is to get them to come back after a bad experience.