Error checking before running middleware