Skip to content

Commit 7128fb2

Browse files
committed
Add a try catch block to catch any exceptions thrown during document validation
1 parent 0e39178 commit 7128fb2

File tree

1 file changed

+35
-21
lines changed

1 file changed

+35
-21
lines changed

src/Microsoft.OpenApi.Hidi/OpenApiService.cs

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -354,35 +354,49 @@ public static Dictionary<string, List<string>> ParseJsonCollectionFile(Stream st
354354

355355
internal static async Task ValidateOpenApiDocument(string openapi, LogLevel loglevel, CancellationToken cancellationToken)
356356
{
357-
if (string.IsNullOrEmpty(openapi))
358-
{
359-
throw new ArgumentNullException(nameof(openapi));
360-
}
361357
var logger = ConfigureLoggerInstance(loglevel);
362-
var stream = await GetStream(openapi, logger, cancellationToken);
363358

364-
OpenApiDocument document;
365-
logger.LogTrace("Parsing the OpenApi file");
366-
document = new OpenApiStreamReader(new OpenApiReaderSettings
359+
try
367360
{
368-
RuleSet = ValidationRuleSet.GetDefaultRuleSet()
369-
}
370-
).Read(stream, out var context);
361+
if (string.IsNullOrEmpty(openapi))
362+
{
363+
throw new ArgumentNullException(nameof(openapi));
364+
}
365+
var stream = await GetStream(openapi, logger, cancellationToken);
371366

372-
if (context.Errors.Count != 0)
373-
{
374-
foreach (var error in context.Errors)
367+
OpenApiDocument document;
368+
logger.LogTrace("Parsing the OpenApi file");
369+
document = new OpenApiStreamReader(new OpenApiReaderSettings
375370
{
376-
Console.WriteLine(error.ToString());
371+
RuleSet = ValidationRuleSet.GetDefaultRuleSet()
372+
}
373+
).Read(stream, out var context);
374+
375+
if (context.Errors.Count != 0)
376+
{
377+
foreach (var error in context.Errors)
378+
{
379+
logger.LogError("OpenApi Parsing error: {message}", error.ToString());
380+
Console.WriteLine(error.ToString());
381+
}
377382
}
378-
}
379383

380-
var statsVisitor = new StatsVisitor();
381-
var walker = new OpenApiWalker(statsVisitor);
382-
walker.Walk(document);
384+
var statsVisitor = new StatsVisitor();
385+
var walker = new OpenApiWalker(statsVisitor);
386+
walker.Walk(document);
387+
388+
logger.LogTrace("Finished walking through the OpenApi document. Generating a statistics report..");
389+
Console.WriteLine(statsVisitor.GetStatisticsReport());
390+
}
391+
catch(Exception ex)
392+
{
393+
#if DEBUG
394+
logger.LogCritical(ex, ex.Message);
395+
#else
396+
logger.LogCritical(ex.Message);
397+
#endif
398+
}
383399

384-
logger.LogTrace("Finished walking through the OpenApi document. Generating a statistics report..");
385-
Console.WriteLine(statsVisitor.GetStatisticsReport());
386400
}
387401

388402
private static OpenApiFormat GetOpenApiFormat(string input, ILogger logger)

0 commit comments

Comments
 (0)