danielwertheim

danielwertheim


notes from a passionate developer

Developer that lives by the mantra "code is meant to be shared".

Share


Tags


Disclaimer

This is a personal blog. The opinions expressed here represent my own and not those of my employer, nor current or previous. All content is published "as is", without warranty of any kind and I don't take any responsibility and can't be liable for any claims, damages or other liabilities that might be caused by the content.

MyNatsClient and Exceptions

Daniel WertheimDaniel Wertheim

The Client that I'm currently building for NATS Server is based around IObservable<T> and when you hook up a subscription against the stream of MsgOp you have the possibility of injecting an IObserver<T> which allows you to inject a handler: Action<Exception>; for OnError. This gives you the opportunity to register a handler for unhandled exceptions. Lets look at some samples.

Catch exceptions using an Observer

Subscribing with the use of an observer makes it easy for you to catch exceptions and handle them.

var c = 0;

//Only the OnNext (the first argument) is required.
var myObserver = new DelegatingObserver<MsgOp>(  
  msg =>
  {
    Console.WriteLine($"Observer OnNext got: {msg.GetPayloadAsString()}");

    throw new Exception(c++.ToString());
  },
  err =>
    Console.WriteLine("Observer OnError got:" + err.Message),
  () =>
    Console.WriteLine("Observer completed"));

//Subscribe to subject "test" and hook up the observer
//for incoming messages on that subject
var sub = _client.SubWithObserver("test", myObserver);


//Publish some messages
while (true)  
{
  Console.WriteLine("Run? (y=yes;n=no)");
  var key = Console.ReadKey().KeyChar;

  Console.WriteLine();
  if (key == 'n')
    break;

  _client.Pub("test", $"test{c.ToString()}");
}

//Tear down subscription (both against NATS server and observable stream)
sub.Dispose();  

This will give the following output:

Run? (y=yes;n=no)  
y  
Run? (y=yes;n=no)  
Observer OnNext got: test0  
Observer OnError got:0  
y  
Run? (y=yes;n=no)  
Observer OnNext got: test1  
Observer OnError got:1  
n  
Observer completed  

What happens if you use an handler?

The handler will just swallow the exception and continue working.

Changing the subscribing part from the first sample above to:

var sub = _client.SubWithHandler("test", msg =>  
{
  Console.WriteLine($"Observer OnNext got: {msg.GetPayloadAsString()}");

  throw new Exception(c++.ToString());
});

This will give the following output:

Run? (y=yes;n=no)  
y  
Run? (y=yes;n=no)  
Observer OnNext got: test0  
y  
Run? (y=yes;n=no)  
Observer OnNext got: test1  
y  
Run? (y=yes;n=no)  
Observer OnNext got: test2  
y  
Run? (y=yes;n=no)  
Observer OnNext got: test3  
n  

There are some more improvements coming to this area. Like generic exception handlers per client etc. This will be release v0.9.0 which will be out really soon.

Cheers,

//Daniel

Developer that lives by the mantra "code is meant to be shared".

Comments