From 979276a574e615213891463ab23569f7bbaf07b4 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Tue, 17 Jan 2017 20:16:25 +0100 Subject: Handle errors without crashing. --- common/synchronizer.cpp | 35 +++++++++++++++++++++++------------ common/synchronizerstore.cpp | 9 ++++++++- 2 files changed, 31 insertions(+), 13 deletions(-) (limited to 'common') diff --git a/common/synchronizer.cpp b/common/synchronizer.cpp index a341f63..cfe1f09 100644 --- a/common/synchronizer.cpp +++ b/common/synchronizer.cpp @@ -279,10 +279,11 @@ KAsync::Job Synchronizer::processSyncQueue() if (mSyncRequestQueue.isEmpty() || mSyncInProgress) { return KAsync::null(); } - mSyncInProgress = true; - mMessageQueue->startTransaction(); - auto job = KAsync::null(); + auto job = KAsync::syncStart([this] { + mMessageQueue->startTransaction(); + mSyncInProgress = true; + }); while (!mSyncRequestQueue.isEmpty()) { auto request = mSyncRequestQueue.takeFirst(); if (request.requestType == Synchronizer::SyncRequest::Synchronization) { @@ -334,13 +335,17 @@ KAsync::Job Synchronizer::processSyncQueue() Sink::Commands::FinishFlushBuffer(fbb, location); enqueueCommand(Sink::Commands::FlushCommand, BufferUtils::extractBuffer(fbb)); } - } else { + } else if (request.requestType == Synchronizer::SyncRequest::ChangeReplay) { job = replayNextRevision(); + } else { + SinkWarning() << "Unknown request type: " << request.requestType; + return KAsync::error(KAsync::Error{"Unknown request type."}); } } return job.then([this](const KAsync::Error &error) { - mSyncStore.clear(); + mSyncTransaction.abort(); mMessageQueue->commit(); + mSyncStore.clear(); mSyncInProgress = false; if (allChangesReplayed()) { emit changesReplayed(); @@ -399,6 +404,18 @@ KAsync::Job Synchronizer::replay(const QByteArray &type, const QByteArray Sink::EntityBuffer buffer(value); const Sink::Entity &entity = buffer.entity(); const auto metadataBuffer = Sink::EntityBuffer::readBuffer(entity.metadata()); + if (!metadataBuffer) { + SinkError() << "No metadata buffer available."; + return KAsync::error("No metadata buffer"); + } + if (mSyncTransaction) { + SinkError() << "Leftover sync transaction."; + mSyncTransaction.abort(); + } + if (mSyncStore) { + SinkError() << "Leftover sync store."; + mSyncStore.clear(); + } Q_ASSERT(metadataBuffer); Q_ASSERT(!mSyncStore); Q_ASSERT(!mSyncTransaction); @@ -411,13 +428,7 @@ KAsync::Job Synchronizer::replay(const QByteArray &type, const QByteArray if (operation != Sink::Operation_Creation) { oldRemoteId = syncStore().resolveLocalId(type, uid); - if (oldRemoteId.isEmpty()) { - SinkWarning() << "Couldn't find the remote id for: " << type << uid; - mSyncStore.clear(); - mSyncTransaction.abort(); - mEntityStore->abortTransaction(); - return KAsync::error(1, "Couldn't find the remote id."); - } + //oldRemoteId can be empty if the resource implementation didn't return a remoteid } SinkTrace() << "Replaying " << key << type << uid << oldRemoteId; diff --git a/common/synchronizerstore.cpp b/common/synchronizerstore.cpp index a9da2fe..31c1391 100644 --- a/common/synchronizerstore.cpp +++ b/common/synchronizerstore.cpp @@ -117,13 +117,20 @@ void SynchronizerStore::writeValue(const QByteArray &prefix, const QByteArray &k void SynchronizerStore::removeValue(const QByteArray &prefix, const QByteArray &key) { - mTransaction.openDatabase("values").remove(prefix + key, [&](const Sink::Storage::DataStore::Error &error) { + auto assembled = prefix + key; + if (assembled.isEmpty()) { + return; + } + mTransaction.openDatabase("values").remove(assembled, [&](const Sink::Storage::DataStore::Error &error) { SinkWarning() << "Failed to remove the value: " << prefix + key << error; }); } void SynchronizerStore::removePrefix(const QByteArray &prefix) { + if (prefix.isEmpty()) { + return; + } //FIXME remove all values matching prefix // mTransaction.openDatabase("values").remove(prefix, [](const Sink::Storage::DataStore::Error &) { // //Ignore errors because we may not find the value -- cgit v1.2.3