CAS-1244 Refactor error handling to reduce code duplication.