Refactor error reporting to reduce duplication.