Skip to content

Commit 5c105c5

Browse files
committed
Log errors that happen during file copy
1 parent 419abd9 commit 5c105c5

File tree

5 files changed

+11
-10
lines changed

5 files changed

+11
-10
lines changed

src/LibraryManager.Build/Contracts/HostInteraction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public async Task<bool> CopyFileAsync(string sourcePath, string destinationPath,
8383
throw new UnauthorizedAccessException();
8484
}
8585

86-
bool result = await FileHelpers.CopyFileAsync(sourcePath, absoluteDestinationPath, cancellationToken);
86+
bool result = await FileHelpers.CopyFileAsync(sourcePath, absoluteDestinationPath, Logger, cancellationToken);
8787
if (result)
8888
{
8989
Logger.Log(string.Format(Resources.Text.FileWrittenToDisk, destinationPath.Replace('\\', '/')), LogLevel.Operation);

src/LibraryManager.Contracts/FileHelpers.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,21 +136,22 @@ public static Task<Stream> ReadFileAsStreamAsync(string fileName, CancellationTo
136136
/// </summary>
137137
/// <param name="sourceFile">Full path to the source file</param>
138138
/// <param name="destinationFile">Full path to the destination file</param>
139+
/// <param name="logger">Pass logger to log errors.</param>
139140
/// <param name="cancellationToken">Cancellation token</param>
140141
/// <returns>A boolean indicating whether the file was copied successfully</returns>
141-
public static async Task<bool> CopyFileAsync(string sourceFile, string destinationFile, CancellationToken cancellationToken)
142+
public static async Task<bool> CopyFileAsync(string sourceFile, string destinationFile, ILogger logger, CancellationToken cancellationToken)
142143
{
143144
try
144145
{
145-
using (FileStream sourceStream = File.Open(sourceFile, FileMode.Open, FileAccess.Read))
146-
{
147-
await WriteToFileAsync(destinationFile, sourceStream, cancellationToken);
148-
}
146+
using FileStream sourceStream = File.Open(sourceFile, FileMode.Open, FileAccess.Read);
147+
148+
await WriteToFileAsync(destinationFile, sourceStream, cancellationToken);
149149

150150
return true;
151151
}
152-
catch (Exception)
152+
catch (Exception exception)
153153
{
154+
logger.Log($"Error during copying file {exception}", LogLevel.Error);
154155
return false;
155156
}
156157
}

src/LibraryManager.Vsix/Contracts/HostInteraction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public async Task<bool> CopyFileAsync(string sourcePath, string destinationPath,
121121
}
122122

123123
await VsHelpers.CheckFileOutOfSourceControlAsync(absoluteDestinationPath);
124-
bool result = await FileHelpers.CopyFileAsync(sourcePath, absoluteDestinationPath, cancellationToken);
124+
bool result = await FileHelpers.CopyFileAsync(sourcePath, absoluteDestinationPath, Logger, cancellationToken);
125125

126126
if (result)
127127
{

src/libman/Contracts/HostInteraction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public async Task<bool> CopyFileAsync(string sourcePath, string destinationPath,
108108
throw new UnauthorizedAccessException();
109109
}
110110

111-
bool result = await FileHelpers.CopyFileAsync(sourcePath, absoluteDestinationPath, cancellationToken);
111+
bool result = await FileHelpers.CopyFileAsync(sourcePath, absoluteDestinationPath, Logger, cancellationToken);
112112
if(result)
113113
{
114114
Logger.Log(string.Format(Resources.Text.FileWrittenToDisk, destinationPath.Replace('\\', '/')), LogLevel.Operation);

test/LibraryManager.Mocks/HostInteraction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public async Task<bool> CopyFileAsync(string sourcePath, string destinationPath,
152152
throw new UnauthorizedAccessException();
153153
}
154154

155-
bool result = await FileHelpers.CopyFileAsync(sourcePath, absoluteDestinationPath, cancellationToken);
155+
bool result = await FileHelpers.CopyFileAsync(sourcePath, absoluteDestinationPath, Logger, cancellationToken);
156156

157157
return result;
158158
}

0 commit comments

Comments
 (0)