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. 21
      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);
}
catch (MongoWriteException ex) when (ex.WriteError.Category == ServerErrorCategory.DuplicateKey)
catch (MongoWriteException ex) when (ex.WriteError?.Category == ServerErrorCategory.DuplicateKey)
{
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()
{
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)

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

@ -40,16 +40,11 @@ namespace Squidex.Infrastructure.MongoDb
{
await collection.InsertOneAsync(document, null, ct);
}
catch (MongoWriteException ex)
{
if (ex.WriteError.Category == ServerErrorCategory.DuplicateKey)
catch (MongoWriteException ex) when (ex.WriteError?.Category == ServerErrorCategory.DuplicateKey)
{
return false;
}
throw;
}
return true;
}
@ -108,9 +103,7 @@ namespace Squidex.Infrastructure.MongoDb
await collection.UpdateOneAsync(x => x.Id.Equals(key), update, Upsert);
}
}
catch (MongoWriteException ex)
{
if (ex.WriteError.Category == ServerErrorCategory.DuplicateKey)
catch (MongoWriteException ex) when (ex.WriteError?.Category == ServerErrorCategory.DuplicateKey)
{
var existingVersion =
await collection.Find(x => x.Id.Equals(key)).Only(x => x.Id, x => x.Version)
@ -122,10 +115,9 @@ namespace Squidex.Infrastructure.MongoDb
throw new InconsistentStateException(existingVersion[versionField].AsInt64, oldVersion, ex);
}
}
else
{
throw;
throw new InconsistentStateException(EtagVersion.Any, oldVersion, ex);
}
}
}
@ -145,9 +137,7 @@ namespace Squidex.Infrastructure.MongoDb
await collection.ReplaceOneAsync(x => x.Id.Equals(key), doc, UpsertReplace);
}
}
catch (MongoWriteException ex)
{
if (ex.WriteError.Category == ServerErrorCategory.DuplicateKey)
catch (MongoWriteException ex) when (ex.WriteError?.Category == ServerErrorCategory.DuplicateKey)
{
var existingVersion =
await collection.Find(x => x.Id.Equals(key)).Only(x => x.Id, x => x.Version)
@ -159,10 +149,9 @@ namespace Squidex.Infrastructure.MongoDb
throw new InconsistentStateException(existingVersion[versionField].AsInt64, oldVersion, ex);
}
}
else
{
throw;
throw new InconsistentStateException(EtagVersion.Any, oldVersion, ex);
}
}
}

Loading…
Cancel
Save