KARAF-2741 Refactorings for error and connection handling