Skip to content

Commit

Permalink
Better log message for connection failures
Browse files Browse the repository at this point in the history
  • Loading branch information
abock committed Aug 14, 2020
1 parent 372c084 commit adc8ce2
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
25 changes: 21 additions & 4 deletions Goodbye.WordPress.CommandLineTool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ static int ShowHelp(string? error = null, Exception? exception = null)
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.Error.WriteLine($"error: {error ?? exception?.Message}");
Console.ResetColor();
if (verbosity > 0 && exception != null)
if (verbosity > 1 && exception != null)
Console.Error.WriteLine(exception);
Console.Error.WriteLine();
}
Expand Down Expand Up @@ -151,9 +151,26 @@ static async Task<int> Main(string[] args)
return ShowHelp(exception: e);
}

exporter = await exporter
.WithPostReader(postReader)
.ExportAsync();
try
{
exporter = await exporter
.WithPostReader(postReader)
.ExportAsync();
}
catch (ConnectionFailedException e)
{
Log.Fatal(
verbosity > 1 ? e : null,
"Cannot connect to WordPress: {Message}",
e.Message);
}
catch (Exception e)
{
Log.Fatal(
e,
"Failed to perform export process for an unknown reason: {Message}",
e.Message);
}

return 0;
}
Expand Down
18 changes: 18 additions & 0 deletions Goodbye.WordPress/ConnectionFailedException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// https://github.com/abock/goodbye-wordpress
// Copyright 2020 Aaron Bockover.
// Licensed under the MIT License.

using System;

namespace Goodbye.WordPress
{
public sealed class ConnectionFailedException : Exception
{
internal ConnectionFailedException(Exception innerException)
: base(
innerException.Message,
innerException)
{
}
}
}
10 changes: 9 additions & 1 deletion Goodbye.WordPress/MysqlPostReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,15 @@ public async IAsyncEnumerable<Post> ReadPostsAsync(
string? originalPermalinkStructure = null;

using var connection = new MySqlConnection(ConnectionString);
await connection.OpenAsync(cancellationToken);

try
{
await connection.OpenAsync(cancellationToken);
}
catch (Exception e)
{
throw new ConnectionFailedException(e);
}

bool dbSupported = false;
int dbVersion = 0;
Expand Down

0 comments on commit adc8ce2

Please sign in to comment.