Improved the error management and added unittests

See https://bugs.launchpad.net/oq-tools/+bug/1256914