Add NodeDistance newtype
Fix query exception handling