Browse Source

Improved exception handling.

pull/539/head
Sebastian 6 years ago
parent
commit
f0f6055ba8
  1. 2
      backend/src/Squidex.Infrastructure.MongoDb/Assets/MongoGridFsAssetStore.cs
  2. 2
      backend/src/Squidex.Infrastructure.MongoDb/EventSourcing/MongoEventStore.cs
  3. 51
      backend/src/Squidex.Infrastructure.MongoDb/MongoDb/MongoExtensions.cs

2
backend/src/Squidex.Infrastructure.MongoDb/Assets/MongoGridFsAssetStore.cs

@ -114,7 +114,7 @@ namespace Squidex.Infrastructure.Assets
await bucket.UploadFromStreamAsync(name, name, stream, cancellationToken: ct); await bucket.UploadFromStreamAsync(name, name, stream, cancellationToken: ct);
} }
catch (MongoWriteException ex) when (ex.WriteError.Category == ServerErrorCategory.DuplicateKey) catch (MongoWriteException ex) when (ex.WriteError?.Category == ServerErrorCategory.DuplicateKey)
{ {
throw new AssetAlreadyExistsException(fileName); throw new AssetAlreadyExistsException(fileName);
} }

2
backend/src/Squidex.Infrastructure.MongoDb/EventSourcing/MongoEventStore.cs

@ -41,7 +41,7 @@ namespace Squidex.Infrastructure.EventSourcing
protected override MongoCollectionSettings CollectionSettings() protected override MongoCollectionSettings CollectionSettings()
{ {
return new MongoCollectionSettings { ReadPreference = ReadPreference.Primary, WriteConcern = WriteConcern.WMajority }; return new MongoCollectionSettings { WriteConcern = WriteConcern.WMajority };
} }
protected override Task SetupCollectionAsync(IMongoCollection<MongoEventCommit> collection, CancellationToken ct = default) protected override Task SetupCollectionAsync(IMongoCollection<MongoEventCommit> collection, CancellationToken ct = default)

51
backend/src/Squidex.Infrastructure.MongoDb/MongoDb/MongoExtensions.cs

@ -40,14 +40,9 @@ namespace Squidex.Infrastructure.MongoDb
{ {
await collection.InsertOneAsync(document, null, ct); await collection.InsertOneAsync(document, null, ct);
} }
catch (MongoWriteException ex) catch (MongoWriteException ex) when (ex.WriteError?.Category == ServerErrorCategory.DuplicateKey)
{ {
if (ex.WriteError.Category == ServerErrorCategory.DuplicateKey) return false;
{
return false;
}
throw;
} }
return true; return true;
@ -108,24 +103,21 @@ namespace Squidex.Infrastructure.MongoDb
await collection.UpdateOneAsync(x => x.Id.Equals(key), update, Upsert); await collection.UpdateOneAsync(x => x.Id.Equals(key), update, Upsert);
} }
} }
catch (MongoWriteException ex) catch (MongoWriteException ex) when (ex.WriteError?.Category == ServerErrorCategory.DuplicateKey)
{ {
if (ex.WriteError.Category == ServerErrorCategory.DuplicateKey) var existingVersion =
{ await collection.Find(x => x.Id.Equals(key)).Only(x => x.Id, x => x.Version)
var existingVersion = .FirstOrDefaultAsync();
await collection.Find(x => x.Id.Equals(key)).Only(x => x.Id, x => x.Version)
.FirstOrDefaultAsync();
if (existingVersion != null) if (existingVersion != null)
{ {
var versionField = GetVersionField<TEntity, TKey>(); var versionField = GetVersionField<TEntity, TKey>();
throw new InconsistentStateException(existingVersion[versionField].AsInt64, oldVersion, ex); throw new InconsistentStateException(existingVersion[versionField].AsInt64, oldVersion, ex);
}
} }
else else
{ {
throw; throw new InconsistentStateException(EtagVersion.Any, oldVersion, ex);
} }
} }
} }
@ -145,24 +137,21 @@ namespace Squidex.Infrastructure.MongoDb
await collection.ReplaceOneAsync(x => x.Id.Equals(key), doc, UpsertReplace); await collection.ReplaceOneAsync(x => x.Id.Equals(key), doc, UpsertReplace);
} }
} }
catch (MongoWriteException ex) catch (MongoWriteException ex) when (ex.WriteError?.Category == ServerErrorCategory.DuplicateKey)
{ {
if (ex.WriteError.Category == ServerErrorCategory.DuplicateKey) var existingVersion =
{ await collection.Find(x => x.Id.Equals(key)).Only(x => x.Id, x => x.Version)
var existingVersion = .FirstOrDefaultAsync();
await collection.Find(x => x.Id.Equals(key)).Only(x => x.Id, x => x.Version)
.FirstOrDefaultAsync();
if (existingVersion != null) if (existingVersion != null)
{ {
var versionField = GetVersionField<TEntity, TKey>(); var versionField = GetVersionField<TEntity, TKey>();
throw new InconsistentStateException(existingVersion[versionField].AsInt64, oldVersion, ex); throw new InconsistentStateException(existingVersion[versionField].AsInt64, oldVersion, ex);
}
} }
else else
{ {
throw; throw new InconsistentStateException(EtagVersion.Any, oldVersion, ex);
} }
} }
} }

Loading…
Cancel
Save