Skip to content

Commit

Permalink
add error raising
Browse files Browse the repository at this point in the history
  • Loading branch information
Mimetis committed Nov 28, 2022
1 parent d77d105 commit 490bb41
Show file tree
Hide file tree
Showing 15 changed files with 414 additions and 301 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,8 @@ internal virtual async Task<Exception> InternalApplyTableChangesAsync(ScopeInfo
// conflict resolved count
int conflictsResolvedCount = 0;

string batchPartInfoFileName = null;

try
{
foreach (var batchPartInfo in bpiTables)
Expand All @@ -360,6 +362,9 @@ internal virtual async Task<Exception> InternalApplyTableChangesAsync(ScopeInfo
// We don't report progress if we do not have applied any changes on the table, to limit verbosity of Progress
await this.InterceptAsync(batchChangesApplyingArgs, progress, cancellationToken).ConfigureAwait(false);

// for error handling
batchPartInfoFileName = batchPartInfo.FileName;

// Rows fetch (either of the good state or not) from the BPI
var rowsFetched = 0;

Expand Down Expand Up @@ -500,7 +505,31 @@ internal virtual async Task<Exception> InternalApplyTableChangesAsync(ScopeInfo
await this.InterceptAsync(batchChangesAppliedArgs, progress, cancellationToken).ConfigureAwait(false);
}

}
catch (Exception ex)
{
string errorMessage = null;

if (message != null && message.Changes != null && message.Changes.DirectoryRoot != null)
errorMessage += $"DirectoryName:{message.Changes.DirectoryRoot}.";

if (message != null && message.Changes != null && message.Changes.DirectoryName != null)
errorMessage += $"FolderName:{message.Changes.DirectoryName}.";

if (batchPartInfoFileName != null)
errorMessage += $"FileName:{batchPartInfoFileName}.";

errorMessage += $"IsBatch:{isBatch}.";
errorMessage += $"CommandType:{dbCommandType}.";

if (command != null && command.CommandText != null)
errorMessage += $"CommandText:{command.CommandText}.";

throw GetSyncError(context, ex);
}

try
{
if ((conflictRows != null && conflictRows.Count > 0) || (errorsRows != null && errorsRows.Count > 0))
{
await using var runnerError = await this.GetConnectionAsync(context, Options.TransactionMode == TransactionMode.None ? SyncMode.NoTransaction : SyncMode.WithTransaction, SyncStage.ChangesApplying, connection, transaction, cancellationToken, progress).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ await schemaTables.ForEachAsync(async syncTable =>
if (cancellationToken.IsCancellationRequested)
return default;

DbCommand selectIncrementalChangesCommand = null;
DbCommandType dbCommandType = DbCommandType.None;

try
{
var setupTable = scopeInfo.Setup.Tables[syncTable.TableName, syncTable.SchemaName];
Expand All @@ -176,7 +179,7 @@ await schemaTables.ForEachAsync(async syncTable =>
if (context.SyncWay == SyncWay.Download && setupTable.SyncDirection == SyncDirection.UploadOnly)
return (context, default, default);

var (selectIncrementalChangesCommand, dbCommandType) = await this.InternalGetSelectChangesCommandAsync(scopeInfo, context, syncTable, isNew,
(selectIncrementalChangesCommand, dbCommandType) = await this.InternalGetSelectChangesCommandAsync(scopeInfo, context, syncTable, isNew,
connection, transaction);

if (selectIncrementalChangesCommand == null)
Expand Down Expand Up @@ -358,11 +361,8 @@ await schemaTables.ForEachAsync(async syncTable =>
{
string message = null;

if (batchInfo != null && batchInfo.DirectoryRoot != null)
message += $"Directory:{batchInfo.DirectoryRoot}.";

if (batchInfo != null && batchInfo.DirectoryName!= null)
message += $"Folder:{batchInfo.DirectoryName}.";
if (selectIncrementalChangesCommand != null)
message += $"SelectChangesCommand:{selectIncrementalChangesCommand.CommandText}.";

if (syncTable != null)
message += $"Table:{syncTable.GetFullName()}.";
Expand All @@ -384,6 +384,7 @@ await schemaTables.ForEachAsync(async syncTable =>
DbConnection connection, DbTransaction transaction,
CancellationToken cancellationToken, IProgress<ProgressArgs> progress)
{

try
{
context.SyncStage = SyncStage.ChangesSelecting;
Expand Down
24 changes: 14 additions & 10 deletions Projects/Dotmim.Sync.Core/SyncException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Sockets;
Expand All @@ -27,15 +28,8 @@ public SyncException(string message, SyncStage stage = SyncStage.None) : base(me

}

public SyncException(Exception innerException, SyncStage stage = SyncStage.None) : base(innerException.Message, innerException)
public SyncException(Exception innerException, SyncStage stage = SyncStage.None) : this(innerException, innerException.Message, stage)
{
this.SyncStage = stage;

if (innerException is null)
return;


this.TypeName = innerException.GetType().Name;
}

public SyncException(Exception innerException, string message, SyncStage stage = SyncStage.None) : base(message, innerException)
Expand All @@ -45,7 +39,17 @@ public SyncException(Exception innerException, string message, SyncStage stage =
if (innerException is null)
return;

this.TypeName = innerException.GetType().Name;
if (innerException is SyncException se)
{
this.DataSource = se.DataSource;
this.InitialCatalog = se.InitialCatalog;
this.TypeName = se.TypeName;
this.Number = se.Number;
}
else
{
this.TypeName = innerException.GetType().Name;
}
}

/// <summary>
Expand Down Expand Up @@ -718,7 +722,7 @@ public class ApplyChangesException : Exception
{
const string message = "Error on table [{0}]: {1}. Row:{2}. ApplyType:{3}";

public ApplyChangesException(SyncRow errorRow, SyncTable schemaChangesTable, SyncRowState rowState, Exception innerException)
public ApplyChangesException(SyncRow errorRow, SyncTable schemaChangesTable, SyncRowState rowState, Exception innerException)
: base(string.Format(message, schemaChangesTable.GetFullName(), innerException.Message, errorRow, rowState), innerException) { }
}
}
2 changes: 1 addition & 1 deletion Projects/Dotmim.Sync.Core/SyncOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public SyncOptions()
this.BatchSize = 2000;
this.CleanMetadatas = true;
this.CleanFolder = true;
this.UseVerboseErrors = false;
this.UseVerboseErrors = true;
this.DisableConstraintsOnApplyChanges = false;
this.ScopeInfoTableName = DefaultScopeInfoTableName;
this.ConflictResolutionPolicy = ConflictResolutionPolicy.ServerWins;
Expand Down
1 change: 0 additions & 1 deletion Projects/Dotmim.Sync.Web.Client/HttpRequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ public async Task<HttpResponseMessage> ProcessRequestAsync(HttpClient client, Sy
var exrror = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

throw new HttpResponseContentException(exrror);

}

}
Expand Down
Loading

0 comments on commit 490bb41

Please sign in to comment.