Properly handle sync errors

On sync errors, it terminates the current sync cycle so that it doesn't go into an infinite loop.

It also fixes some previously hidden problems in the versioning test suite and in the SyncError class.

This PR supersedes #746, fixing #549 and #740.