Skip to content

Commit 24daf0c

Browse files
committed
Better error handling
1 parent 9ce2b27 commit 24daf0c

File tree

2 files changed

+43
-20
lines changed

2 files changed

+43
-20
lines changed

src/Microsoft.OpenApi.Hidi/OpenApiService.cs

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using Microsoft.OpenApi.Services;
2323
using Microsoft.OpenApi.Validations;
2424
using Microsoft.OpenApi.Writers;
25+
using System.Threading;
2526

2627
namespace Microsoft.OpenApi.Hidi
2728
{
@@ -38,7 +39,8 @@ public static async Task ProcessOpenApiDocument(
3839
bool resolveexternal,
3940
string filterbyoperationids,
4041
string filterbytags,
41-
string filterbycollection
42+
string filterbycollection,
43+
CancellationToken cancellationToken
4244
)
4345
{
4446
var logger = ConfigureLoggerInstance(loglevel);
@@ -52,7 +54,11 @@ string filterbycollection
5254
}
5355
catch (ArgumentNullException ex)
5456
{
55-
logger.LogError(ex.Message);
57+
#if DEBUG
58+
logger.LogCritical(ex, ex.Message);
59+
#else
60+
logger.LogCritical(ex.Message);
61+
#endif
5662
return;
5763
}
5864
try
@@ -64,19 +70,27 @@ string filterbycollection
6470
}
6571
catch (ArgumentException ex)
6672
{
67-
logger.LogError(ex.Message);
73+
#if DEBUG
74+
logger.LogCritical(ex, ex.Message);
75+
#else
76+
logger.LogCritical(ex.Message);
77+
#endif
6878
return;
6979
}
7080
try
7181
{
7282
if (output.Exists)
7383
{
74-
throw new IOException("The file you're writing to already exists. Please input a new file path.");
84+
throw new IOException($"The file {output} already exists. Please input a new file path.");
7585
}
7686
}
7787
catch (IOException ex)
7888
{
79-
logger.LogError(ex.Message);
89+
#if DEBUG
90+
logger.LogCritical(ex, ex.Message);
91+
#else
92+
logger.LogCritical(ex.Message);
93+
#endif
8094
return;
8195
}
8296

@@ -91,12 +105,12 @@ string filterbycollection
91105
openApiFormat = format ?? GetOpenApiFormat(csdl, logger);
92106
version ??= OpenApiSpecVersion.OpenApi3_0;
93107

94-
stream = await GetStream(csdl, logger);
108+
stream = await GetStream(csdl, logger, cancellationToken);
95109
document = await ConvertCsdlToOpenApi(stream);
96110
}
97111
else
98112
{
99-
stream = await GetStream(openapi, logger);
113+
stream = await GetStream(openapi, logger, cancellationToken);
100114

101115
// Parsing OpenAPI file
102116
stopwatch.Start();
@@ -156,7 +170,7 @@ string filterbycollection
156170
}
157171
if (!string.IsNullOrEmpty(filterbycollection))
158172
{
159-
var fileStream = await GetStream(filterbycollection, logger);
173+
var fileStream = await GetStream(filterbycollection, logger, cancellationToken);
160174
var requestUrls = ParseJsonCollectionFile(fileStream, logger);
161175

162176
logger.LogTrace("Creating predicate based on the paths and Http methods defined in the Postman collection.");
@@ -245,7 +259,7 @@ public static OpenApiDocument FixReferences(OpenApiDocument document)
245259
return doc;
246260
}
247261

248-
private static async Task<Stream> GetStream(string input, ILogger logger)
262+
private static async Task<Stream> GetStream(string input, ILogger logger, CancellationToken cancellationToken)
249263
{
250264
var stopwatch = new Stopwatch();
251265
stopwatch.Start();
@@ -263,11 +277,15 @@ private static async Task<Stream> GetStream(string input, ILogger logger)
263277
{
264278
DefaultRequestVersion = HttpVersion.Version20
265279
};
266-
stream = await httpClient.GetStreamAsync(input);
280+
stream = await httpClient.GetStreamAsync(input, cancellationToken);
267281
}
268282
catch (HttpRequestException ex)
269283
{
270-
logger.LogError($"Could not download the file at {input}, reason{ex}");
284+
#if DEBUG
285+
logger.LogCritical(ex, $"Could not download the file at {input}, reason: {ex.Message}");
286+
#else
287+
logger.LogCritical($"Could not download the file at {input}, reason: {ex.Message}", input, ex.Message);
288+
#endif
271289
return null;
272290
}
273291
}
@@ -286,7 +304,11 @@ ex is UnauthorizedAccessException ||
286304
ex is SecurityException ||
287305
ex is NotSupportedException)
288306
{
289-
logger.LogError($"Could not open the file at {input}, reason: {ex.Message}");
307+
#if DEBUG
308+
logger.LogCritical(ex, $"Could not open the file at {input}, reason: {ex.Message}");
309+
#else
310+
logger.LogCritical($"Could not open the file at {input}, reason: {ex.Message}");
311+
#endif
290312
return null;
291313
}
292314
}
@@ -327,14 +349,14 @@ public static Dictionary<string, List<string>> ParseJsonCollectionFile(Stream st
327349
return requestUrls;
328350
}
329351

330-
internal static async Task ValidateOpenApiDocument(string openapi, LogLevel loglevel)
352+
internal static async Task ValidateOpenApiDocument(string openapi, LogLevel loglevel, CancellationToken cancellationToken)
331353
{
332354
if (string.IsNullOrEmpty(openapi))
333355
{
334356
throw new ArgumentNullException(nameof(openapi));
335357
}
336358
var logger = ConfigureLoggerInstance(loglevel);
337-
var stream = await GetStream(openapi, logger);
359+
var stream = await GetStream(openapi, logger, cancellationToken);
338360

339361
OpenApiDocument document;
340362
logger.LogTrace("Parsing the OpenApi file");
@@ -369,16 +391,16 @@ private static OpenApiFormat GetOpenApiFormat(string input, ILogger logger)
369391
private static ILogger ConfigureLoggerInstance(LogLevel loglevel)
370392
{
371393
// Configure logger options
372-
#if DEBUG
394+
#if DEBUG
373395
loglevel = loglevel > LogLevel.Debug ? LogLevel.Debug : loglevel;
374-
#endif
396+
#endif
375397

376398
var logger = LoggerFactory.Create((builder) => {
377399
builder
378400
.AddConsole()
379-
#if DEBUG
401+
#if DEBUG
380402
.AddDebug()
381-
#endif
403+
#endif
382404
.SetMinimumLevel(loglevel);
383405
}).CreateLogger<OpenApiService>();
384406

src/Microsoft.OpenApi.Hidi/Program.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.CommandLine;
55
using System.IO;
6+
using System.Threading;
67
using System.Threading.Tasks;
78
using Microsoft.Extensions.Logging;
89

@@ -55,7 +56,7 @@ static async Task<int> Main(string[] args)
5556
logLevelOption
5657
};
5758

58-
validateCommand.SetHandler<string, LogLevel>(OpenApiService.ValidateOpenApiDocument, descriptionOption, logLevelOption);
59+
validateCommand.SetHandler<string, LogLevel, CancellationToken>(OpenApiService.ValidateOpenApiDocument, descriptionOption, logLevelOption);
5960

6061
var transformCommand = new Command("transform")
6162
{
@@ -72,7 +73,7 @@ static async Task<int> Main(string[] args)
7273
resolveExternalOption,
7374
};
7475

75-
transformCommand.SetHandler<string, string, FileInfo, OpenApiSpecVersion?, OpenApiFormat?, LogLevel, bool, bool, string, string, string> (
76+
transformCommand.SetHandler<string, string, FileInfo, OpenApiSpecVersion?, OpenApiFormat?, LogLevel, bool, bool, string, string, string, CancellationToken> (
7677
OpenApiService.ProcessOpenApiDocument, descriptionOption, csdlOption, outputOption, versionOption, formatOption, logLevelOption, inlineOption, resolveExternalOption, filterByOperationIdsOption, filterByTagsOption, filterByCollectionOption);
7778

7879
rootCommand.Add(transformCommand);

0 commit comments

Comments
 (0)