Fix repository.write
Fix error handling in Repository.write