From 24daf0ceab2bccb8017d2ef78a980f6a054df443 Mon Sep 17 00:00:00 2001 From: Maggie Kimani Date: Tue, 8 Mar 2022 14:55:49 +0300 Subject: [PATCH 1/7] Better error handling --- src/Microsoft.OpenApi.Hidi/OpenApiService.cs | 58 ++++++++++++++------ src/Microsoft.OpenApi.Hidi/Program.cs | 5 +- 2 files changed, 43 insertions(+), 20 deletions(-) diff --git a/src/Microsoft.OpenApi.Hidi/OpenApiService.cs b/src/Microsoft.OpenApi.Hidi/OpenApiService.cs index 1f86e3c06..def1afb60 100644 --- a/src/Microsoft.OpenApi.Hidi/OpenApiService.cs +++ b/src/Microsoft.OpenApi.Hidi/OpenApiService.cs @@ -22,6 +22,7 @@ using Microsoft.OpenApi.Services; using Microsoft.OpenApi.Validations; using Microsoft.OpenApi.Writers; +using System.Threading; namespace Microsoft.OpenApi.Hidi { @@ -38,7 +39,8 @@ public static async Task ProcessOpenApiDocument( bool resolveexternal, string filterbyoperationids, string filterbytags, - string filterbycollection + string filterbycollection, + CancellationToken cancellationToken ) { var logger = ConfigureLoggerInstance(loglevel); @@ -52,7 +54,11 @@ string filterbycollection } catch (ArgumentNullException ex) { - logger.LogError(ex.Message); +#if DEBUG + logger.LogCritical(ex, ex.Message); +#else + logger.LogCritical(ex.Message); +#endif return; } try @@ -64,19 +70,27 @@ string filterbycollection } catch (ArgumentException ex) { - logger.LogError(ex.Message); +#if DEBUG + logger.LogCritical(ex, ex.Message); +#else + logger.LogCritical(ex.Message); +#endif return; } try { if (output.Exists) { - throw new IOException("The file you're writing to already exists. Please input a new file path."); + throw new IOException($"The file {output} already exists. Please input a new file path."); } } catch (IOException ex) { - logger.LogError(ex.Message); +#if DEBUG + logger.LogCritical(ex, ex.Message); +#else + logger.LogCritical(ex.Message); +#endif return; } @@ -91,12 +105,12 @@ string filterbycollection openApiFormat = format ?? GetOpenApiFormat(csdl, logger); version ??= OpenApiSpecVersion.OpenApi3_0; - stream = await GetStream(csdl, logger); + stream = await GetStream(csdl, logger, cancellationToken); document = await ConvertCsdlToOpenApi(stream); } else { - stream = await GetStream(openapi, logger); + stream = await GetStream(openapi, logger, cancellationToken); // Parsing OpenAPI file stopwatch.Start(); @@ -156,7 +170,7 @@ string filterbycollection } if (!string.IsNullOrEmpty(filterbycollection)) { - var fileStream = await GetStream(filterbycollection, logger); + var fileStream = await GetStream(filterbycollection, logger, cancellationToken); var requestUrls = ParseJsonCollectionFile(fileStream, logger); 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) return doc; } - private static async Task GetStream(string input, ILogger logger) + private static async Task GetStream(string input, ILogger logger, CancellationToken cancellationToken) { var stopwatch = new Stopwatch(); stopwatch.Start(); @@ -263,11 +277,15 @@ private static async Task GetStream(string input, ILogger logger) { DefaultRequestVersion = HttpVersion.Version20 }; - stream = await httpClient.GetStreamAsync(input); + stream = await httpClient.GetStreamAsync(input, cancellationToken); } catch (HttpRequestException ex) { - logger.LogError($"Could not download the file at {input}, reason{ex}"); +#if DEBUG + logger.LogCritical(ex, $"Could not download the file at {input}, reason: {ex.Message}"); +#else + logger.LogCritical($"Could not download the file at {input}, reason: {ex.Message}", input, ex.Message); +#endif return null; } } @@ -286,7 +304,11 @@ ex is UnauthorizedAccessException || ex is SecurityException || ex is NotSupportedException) { - logger.LogError($"Could not open the file at {input}, reason: {ex.Message}"); +#if DEBUG + logger.LogCritical(ex, $"Could not open the file at {input}, reason: {ex.Message}"); +#else + logger.LogCritical($"Could not open the file at {input}, reason: {ex.Message}"); +#endif return null; } } @@ -327,14 +349,14 @@ public static Dictionary> ParseJsonCollectionFile(Stream st return requestUrls; } - internal static async Task ValidateOpenApiDocument(string openapi, LogLevel loglevel) + internal static async Task ValidateOpenApiDocument(string openapi, LogLevel loglevel, CancellationToken cancellationToken) { if (string.IsNullOrEmpty(openapi)) { throw new ArgumentNullException(nameof(openapi)); } var logger = ConfigureLoggerInstance(loglevel); - var stream = await GetStream(openapi, logger); + var stream = await GetStream(openapi, logger, cancellationToken); OpenApiDocument document; logger.LogTrace("Parsing the OpenApi file"); @@ -369,16 +391,16 @@ private static OpenApiFormat GetOpenApiFormat(string input, ILogger logger) private static ILogger ConfigureLoggerInstance(LogLevel loglevel) { // Configure logger options - #if DEBUG +#if DEBUG loglevel = loglevel > LogLevel.Debug ? LogLevel.Debug : loglevel; - #endif +#endif var logger = LoggerFactory.Create((builder) => { builder .AddConsole() - #if DEBUG +#if DEBUG .AddDebug() - #endif +#endif .SetMinimumLevel(loglevel); }).CreateLogger(); diff --git a/src/Microsoft.OpenApi.Hidi/Program.cs b/src/Microsoft.OpenApi.Hidi/Program.cs index 95e6f63f2..f3d455e4a 100644 --- a/src/Microsoft.OpenApi.Hidi/Program.cs +++ b/src/Microsoft.OpenApi.Hidi/Program.cs @@ -3,6 +3,7 @@ using System.CommandLine; using System.IO; +using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Logging; @@ -55,7 +56,7 @@ static async Task Main(string[] args) logLevelOption }; - validateCommand.SetHandler(OpenApiService.ValidateOpenApiDocument, descriptionOption, logLevelOption); + validateCommand.SetHandler(OpenApiService.ValidateOpenApiDocument, descriptionOption, logLevelOption); var transformCommand = new Command("transform") { @@ -72,7 +73,7 @@ static async Task Main(string[] args) resolveExternalOption, }; - transformCommand.SetHandler ( + transformCommand.SetHandler ( OpenApiService.ProcessOpenApiDocument, descriptionOption, csdlOption, outputOption, versionOption, formatOption, logLevelOption, inlineOption, resolveExternalOption, filterByOperationIdsOption, filterByTagsOption, filterByCollectionOption); rootCommand.Add(transformCommand); From 0e39178616322b24862fd6d46debe5e37765f2f2 Mon Sep 17 00:00:00 2001 From: Maggie Kimani Date: Tue, 8 Mar 2022 16:43:02 +0300 Subject: [PATCH 2/7] Remove string interpolation --- src/Microsoft.OpenApi.Hidi/OpenApiService.cs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.OpenApi.Hidi/OpenApiService.cs b/src/Microsoft.OpenApi.Hidi/OpenApiService.cs index def1afb60..7b94c4997 100644 --- a/src/Microsoft.OpenApi.Hidi/OpenApiService.cs +++ b/src/Microsoft.OpenApi.Hidi/OpenApiService.cs @@ -128,10 +128,13 @@ CancellationToken cancellationToken var context = result.OpenApiDiagnostic; if (context.Errors.Count > 0) { + logger.LogTrace("{timestamp}ms: Parsed OpenAPI with errors. {count} paths found.", stopwatch.ElapsedMilliseconds, document.Paths.Count); + var errorReport = new StringBuilder(); foreach (var error in context.Errors) { + logger.LogError("OpenApi Parsing error: {message}", error.ToString()); errorReport.AppendLine(error.ToString()); } logger.LogError($"{stopwatch.ElapsedMilliseconds}ms: OpenApi Parsing errors {string.Join(Environment.NewLine, context.Errors.Select(e => e.Message).ToArray())}"); @@ -282,9 +285,9 @@ private static async Task GetStream(string input, ILogger logger, Cancel catch (HttpRequestException ex) { #if DEBUG - logger.LogCritical(ex, $"Could not download the file at {input}, reason: {ex.Message}"); + logger.LogCritical(ex, "Could not download the file at {inputPath}, reason: {exMessage}", input, ex.Message); #else - logger.LogCritical($"Could not download the file at {input}, reason: {ex.Message}", input, ex.Message); + logger.LogCritical( "Could not download the file at {inputPath}, reason: {exMessage}", input, ex.Message); #endif return null; } @@ -305,9 +308,9 @@ ex is SecurityException || ex is NotSupportedException) { #if DEBUG - logger.LogCritical(ex, $"Could not open the file at {input}, reason: {ex.Message}"); + logger.LogCritical(ex, "Could not open the file at {inputPath}, reason: {exMessage}", input, ex.Message); #else - logger.LogCritical($"Could not open the file at {input}, reason: {ex.Message}"); + logger.LogCritical("Could not open the file at {inputPath}, reason: {exMessage}", input, ex.Message); #endif return null; } From 7128fb235107e1a589ff338c5fcdef11b6dfc7c2 Mon Sep 17 00:00:00 2001 From: Maggie Kimani Date: Tue, 8 Mar 2022 16:54:37 +0300 Subject: [PATCH 3/7] Add a try catch block to catch any exceptions thrown during document validation --- src/Microsoft.OpenApi.Hidi/OpenApiService.cs | 56 ++++++++++++-------- 1 file changed, 35 insertions(+), 21 deletions(-) diff --git a/src/Microsoft.OpenApi.Hidi/OpenApiService.cs b/src/Microsoft.OpenApi.Hidi/OpenApiService.cs index 7b94c4997..64c228387 100644 --- a/src/Microsoft.OpenApi.Hidi/OpenApiService.cs +++ b/src/Microsoft.OpenApi.Hidi/OpenApiService.cs @@ -354,35 +354,49 @@ public static Dictionary> ParseJsonCollectionFile(Stream st internal static async Task ValidateOpenApiDocument(string openapi, LogLevel loglevel, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(openapi)) - { - throw new ArgumentNullException(nameof(openapi)); - } var logger = ConfigureLoggerInstance(loglevel); - var stream = await GetStream(openapi, logger, cancellationToken); - OpenApiDocument document; - logger.LogTrace("Parsing the OpenApi file"); - document = new OpenApiStreamReader(new OpenApiReaderSettings + try { - RuleSet = ValidationRuleSet.GetDefaultRuleSet() - } - ).Read(stream, out var context); + if (string.IsNullOrEmpty(openapi)) + { + throw new ArgumentNullException(nameof(openapi)); + } + var stream = await GetStream(openapi, logger, cancellationToken); - if (context.Errors.Count != 0) - { - foreach (var error in context.Errors) + OpenApiDocument document; + logger.LogTrace("Parsing the OpenApi file"); + document = new OpenApiStreamReader(new OpenApiReaderSettings { - Console.WriteLine(error.ToString()); + RuleSet = ValidationRuleSet.GetDefaultRuleSet() + } + ).Read(stream, out var context); + + if (context.Errors.Count != 0) + { + foreach (var error in context.Errors) + { + logger.LogError("OpenApi Parsing error: {message}", error.ToString()); + Console.WriteLine(error.ToString()); + } } - } - var statsVisitor = new StatsVisitor(); - var walker = new OpenApiWalker(statsVisitor); - walker.Walk(document); + var statsVisitor = new StatsVisitor(); + var walker = new OpenApiWalker(statsVisitor); + walker.Walk(document); + + logger.LogTrace("Finished walking through the OpenApi document. Generating a statistics report.."); + Console.WriteLine(statsVisitor.GetStatisticsReport()); + } + catch(Exception ex) + { +#if DEBUG + logger.LogCritical(ex, ex.Message); +#else + logger.LogCritical(ex.Message); +#endif + } - logger.LogTrace("Finished walking through the OpenApi document. Generating a statistics report.."); - Console.WriteLine(statsVisitor.GetStatisticsReport()); } private static OpenApiFormat GetOpenApiFormat(string input, ILogger logger) From 37ecce28f8459a5b25987d6a5f71c2ade9677030 Mon Sep 17 00:00:00 2001 From: Maggie Kimani Date: Tue, 8 Mar 2022 17:49:47 +0300 Subject: [PATCH 4/7] Clean up code --- src/Microsoft.OpenApi.Hidi/OpenApiService.cs | 30 ++------------------ 1 file changed, 3 insertions(+), 27 deletions(-) diff --git a/src/Microsoft.OpenApi.Hidi/OpenApiService.cs b/src/Microsoft.OpenApi.Hidi/OpenApiService.cs index 64c228387..09ad21d90 100644 --- a/src/Microsoft.OpenApi.Hidi/OpenApiService.cs +++ b/src/Microsoft.OpenApi.Hidi/OpenApiService.cs @@ -49,42 +49,18 @@ CancellationToken cancellationToken { if (string.IsNullOrEmpty(openapi) && string.IsNullOrEmpty(csdl)) { - throw new ArgumentNullException("Please input a file path"); + throw new ArgumentException("Please input a file path"); } - } - catch (ArgumentNullException ex) - { -#if DEBUG - logger.LogCritical(ex, ex.Message); -#else - logger.LogCritical(ex.Message); -#endif - return; - } - try - { if(output == null) { - throw new ArgumentException(nameof(output)); + throw new ArgumentNullException(nameof(output)); } - } - catch (ArgumentException ex) - { -#if DEBUG - logger.LogCritical(ex, ex.Message); -#else - logger.LogCritical(ex.Message); -#endif - return; - } - try - { if (output.Exists) { throw new IOException($"The file {output} already exists. Please input a new file path."); } } - catch (IOException ex) + catch (Exception ex) { #if DEBUG logger.LogCritical(ex, ex.Message); From f683b7212d9a3a0fa6228c61b263eec1b9258ae9 Mon Sep 17 00:00:00 2001 From: Maggie Kimani Date: Tue, 8 Mar 2022 21:03:34 +0300 Subject: [PATCH 5/7] Add an exit statement and use logger to log errors to the console --- src/Microsoft.OpenApi.Hidi/OpenApiService.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.OpenApi.Hidi/OpenApiService.cs b/src/Microsoft.OpenApi.Hidi/OpenApiService.cs index 09ad21d90..ec53b615f 100644 --- a/src/Microsoft.OpenApi.Hidi/OpenApiService.cs +++ b/src/Microsoft.OpenApi.Hidi/OpenApiService.cs @@ -353,7 +353,6 @@ internal static async Task ValidateOpenApiDocument(string openapi, LogLevel logl foreach (var error in context.Errors) { logger.LogError("OpenApi Parsing error: {message}", error.ToString()); - Console.WriteLine(error.ToString()); } } @@ -362,7 +361,7 @@ internal static async Task ValidateOpenApiDocument(string openapi, LogLevel logl walker.Walk(document); logger.LogTrace("Finished walking through the OpenApi document. Generating a statistics report.."); - Console.WriteLine(statsVisitor.GetStatisticsReport()); + logger.LogInformation(statsVisitor.GetStatisticsReport()); } catch(Exception ex) { @@ -371,6 +370,7 @@ internal static async Task ValidateOpenApiDocument(string openapi, LogLevel logl #else logger.LogCritical(ex.Message); #endif + return; } } From abd6f508866c9250b292f3e51c681e5d39fdb996 Mon Sep 17 00:00:00 2001 From: Maggie Kimani Date: Tue, 8 Mar 2022 21:32:17 +0300 Subject: [PATCH 6/7] Clean up code to bubble up exceptions to the global catch block --- src/Microsoft.OpenApi.Hidi/OpenApiService.cs | 216 +++++++++---------- 1 file changed, 103 insertions(+), 113 deletions(-) diff --git a/src/Microsoft.OpenApi.Hidi/OpenApiService.cs b/src/Microsoft.OpenApi.Hidi/OpenApiService.cs index ec53b615f..11fe6decc 100644 --- a/src/Microsoft.OpenApi.Hidi/OpenApiService.cs +++ b/src/Microsoft.OpenApi.Hidi/OpenApiService.cs @@ -59,131 +59,131 @@ CancellationToken cancellationToken { throw new IOException($"The file {output} already exists. Please input a new file path."); } - } - catch (Exception ex) - { -#if DEBUG - logger.LogCritical(ex, ex.Message); -#else - logger.LogCritical(ex.Message); -#endif - return; - } - Stream stream; - OpenApiDocument document; - OpenApiFormat openApiFormat; - var stopwatch = new Stopwatch(); - - if (!string.IsNullOrEmpty(csdl)) - { - // Default to yaml and OpenApiVersion 3 during csdl to OpenApi conversion - openApiFormat = format ?? GetOpenApiFormat(csdl, logger); - version ??= OpenApiSpecVersion.OpenApi3_0; - - stream = await GetStream(csdl, logger, cancellationToken); - document = await ConvertCsdlToOpenApi(stream); - } - else - { - stream = await GetStream(openapi, logger, cancellationToken); + Stream stream; + OpenApiDocument document; + OpenApiFormat openApiFormat; + var stopwatch = new Stopwatch(); - // Parsing OpenAPI file - stopwatch.Start(); - logger.LogTrace("Parsing OpenApi file"); - var result = new OpenApiStreamReader(new OpenApiReaderSettings + if (!string.IsNullOrEmpty(csdl)) { - ReferenceResolution = resolveexternal ? ReferenceResolutionSetting.ResolveAllReferences : ReferenceResolutionSetting.ResolveLocalReferences, - RuleSet = ValidationRuleSet.GetDefaultRuleSet() + // Default to yaml and OpenApiVersion 3 during csdl to OpenApi conversion + openApiFormat = format ?? GetOpenApiFormat(csdl, logger); + version ??= OpenApiSpecVersion.OpenApi3_0; + + stream = await GetStream(csdl, logger, cancellationToken); + document = await ConvertCsdlToOpenApi(stream); } - ).ReadAsync(stream).GetAwaiter().GetResult(); + else + { + stream = await GetStream(openapi, logger, cancellationToken); - document = result.OpenApiDocument; - stopwatch.Stop(); + // Parsing OpenAPI file + stopwatch.Start(); + logger.LogTrace("Parsing OpenApi file"); + var result = new OpenApiStreamReader(new OpenApiReaderSettings + { + ReferenceResolution = resolveexternal ? ReferenceResolutionSetting.ResolveAllReferences : ReferenceResolutionSetting.ResolveLocalReferences, + RuleSet = ValidationRuleSet.GetDefaultRuleSet() + } + ).ReadAsync(stream).GetAwaiter().GetResult(); - var context = result.OpenApiDiagnostic; - if (context.Errors.Count > 0) - { - logger.LogTrace("{timestamp}ms: Parsed OpenAPI with errors. {count} paths found.", stopwatch.ElapsedMilliseconds, document.Paths.Count); + document = result.OpenApiDocument; + stopwatch.Stop(); - var errorReport = new StringBuilder(); + var context = result.OpenApiDiagnostic; + if (context.Errors.Count > 0) + { + logger.LogTrace("{timestamp}ms: Parsed OpenAPI with errors. {count} paths found.", stopwatch.ElapsedMilliseconds, document.Paths.Count); - foreach (var error in context.Errors) + var errorReport = new StringBuilder(); + + foreach (var error in context.Errors) + { + logger.LogError("OpenApi Parsing error: {message}", error.ToString()); + errorReport.AppendLine(error.ToString()); + } + logger.LogError($"{stopwatch.ElapsedMilliseconds}ms: OpenApi Parsing errors {string.Join(Environment.NewLine, context.Errors.Select(e => e.Message).ToArray())}"); + } + else { - logger.LogError("OpenApi Parsing error: {message}", error.ToString()); - errorReport.AppendLine(error.ToString()); + logger.LogTrace("{timestamp}ms: Parsed OpenApi successfully. {count} paths found.", stopwatch.ElapsedMilliseconds, document.Paths.Count); } - logger.LogError($"{stopwatch.ElapsedMilliseconds}ms: OpenApi Parsing errors {string.Join(Environment.NewLine, context.Errors.Select(e => e.Message).ToArray())}"); + + openApiFormat = format ?? GetOpenApiFormat(openapi, logger); + version ??= result.OpenApiDiagnostic.SpecificationVersion; } - else + + Func predicate; + + // Check if filter options are provided, then slice the OpenAPI document + if (!string.IsNullOrEmpty(filterbyoperationids) && !string.IsNullOrEmpty(filterbytags)) { - logger.LogTrace("{timestamp}ms: Parsed OpenApi successfully. {count} paths found.", stopwatch.ElapsedMilliseconds, document.Paths.Count); + throw new InvalidOperationException("Cannot filter by operationIds and tags at the same time."); } + if (!string.IsNullOrEmpty(filterbyoperationids)) + { + logger.LogTrace("Creating predicate based on the operationIds supplied."); + predicate = OpenApiFilterService.CreatePredicate(operationIds: filterbyoperationids); - openApiFormat = format ?? GetOpenApiFormat(openapi, logger); - version ??= result.OpenApiDiagnostic.SpecificationVersion; - } - - Func predicate; + logger.LogTrace("Creating subset OpenApi document."); + document = OpenApiFilterService.CreateFilteredDocument(document, predicate); + } + if (!string.IsNullOrEmpty(filterbytags)) + { + logger.LogTrace("Creating predicate based on the tags supplied."); + predicate = OpenApiFilterService.CreatePredicate(tags: filterbytags); - // Check if filter options are provided, then slice the OpenAPI document - if (!string.IsNullOrEmpty(filterbyoperationids) && !string.IsNullOrEmpty(filterbytags)) - { - throw new InvalidOperationException("Cannot filter by operationIds and tags at the same time."); - } - if (!string.IsNullOrEmpty(filterbyoperationids)) - { - logger.LogTrace("Creating predicate based on the operationIds supplied."); - predicate = OpenApiFilterService.CreatePredicate(operationIds: filterbyoperationids); + logger.LogTrace("Creating subset OpenApi document."); + document = OpenApiFilterService.CreateFilteredDocument(document, predicate); + } + if (!string.IsNullOrEmpty(filterbycollection)) + { + var fileStream = await GetStream(filterbycollection, logger, cancellationToken); + var requestUrls = ParseJsonCollectionFile(fileStream, logger); - logger.LogTrace("Creating subset OpenApi document."); - document = OpenApiFilterService.CreateFilteredDocument(document, predicate); - } - if (!string.IsNullOrEmpty(filterbytags)) - { - logger.LogTrace("Creating predicate based on the tags supplied."); - predicate = OpenApiFilterService.CreatePredicate(tags: filterbytags); + logger.LogTrace("Creating predicate based on the paths and Http methods defined in the Postman collection."); + predicate = OpenApiFilterService.CreatePredicate(requestUrls: requestUrls, source: document); - logger.LogTrace("Creating subset OpenApi document."); - document = OpenApiFilterService.CreateFilteredDocument(document, predicate); - } - if (!string.IsNullOrEmpty(filterbycollection)) - { - var fileStream = await GetStream(filterbycollection, logger, cancellationToken); - var requestUrls = ParseJsonCollectionFile(fileStream, logger); + logger.LogTrace("Creating subset OpenApi document."); + document = OpenApiFilterService.CreateFilteredDocument(document, predicate); + } - logger.LogTrace("Creating predicate based on the paths and Http methods defined in the Postman collection."); - predicate = OpenApiFilterService.CreatePredicate(requestUrls: requestUrls, source:document); + logger.LogTrace("Creating a new file"); + using var outputStream = output?.Create(); + var textWriter = outputStream != null ? new StreamWriter(outputStream) : Console.Out; - logger.LogTrace("Creating subset OpenApi document."); - document = OpenApiFilterService.CreateFilteredDocument(document, predicate); - } - - logger.LogTrace("Creating a new file"); - using var outputStream = output?.Create(); - var textWriter = outputStream != null ? new StreamWriter(outputStream) : Console.Out; + var settings = new OpenApiWriterSettings() + { + ReferenceInline = inline ? ReferenceInlineSetting.InlineLocalReferences : ReferenceInlineSetting.DoNotInlineReferences + }; - var settings = new OpenApiWriterSettings() - { - ReferenceInline = inline ? ReferenceInlineSetting.InlineLocalReferences : ReferenceInlineSetting.DoNotInlineReferences - }; + IOpenApiWriter writer = openApiFormat switch + { + OpenApiFormat.Json => new OpenApiJsonWriter(textWriter, settings), + OpenApiFormat.Yaml => new OpenApiYamlWriter(textWriter, settings), + _ => throw new ArgumentException("Unknown format"), + }; - IOpenApiWriter writer = openApiFormat switch - { - OpenApiFormat.Json => new OpenApiJsonWriter(textWriter, settings), - OpenApiFormat.Yaml => new OpenApiYamlWriter(textWriter, settings), - _ => throw new ArgumentException("Unknown format"), - }; + logger.LogTrace("Serializing to OpenApi document using the provided spec version and writer"); - logger.LogTrace("Serializing to OpenApi document using the provided spec version and writer"); - - stopwatch.Start(); - document.Serialize(writer, (OpenApiSpecVersion)version); - stopwatch.Stop(); + stopwatch.Start(); + document.Serialize(writer, (OpenApiSpecVersion)version); + stopwatch.Stop(); - logger.LogTrace($"Finished serializing in {stopwatch.ElapsedMilliseconds}ms"); + logger.LogTrace($"Finished serializing in {stopwatch.ElapsedMilliseconds}ms"); - textWriter.Flush(); + textWriter.Flush(); + } + catch (Exception ex) + { +#if DEBUG + logger.LogCritical(ex, ex.Message); +#else + logger.LogCritical(ex.Message); +#endif + return; + } } /// @@ -260,12 +260,7 @@ private static async Task GetStream(string input, ILogger logger, Cancel } catch (HttpRequestException ex) { -#if DEBUG - logger.LogCritical(ex, "Could not download the file at {inputPath}, reason: {exMessage}", input, ex.Message); -#else - logger.LogCritical( "Could not download the file at {inputPath}, reason: {exMessage}", input, ex.Message); -#endif - return null; + throw new InvalidOperationException($"Could not download the file at {input}", ex); } } else @@ -283,12 +278,7 @@ ex is UnauthorizedAccessException || ex is SecurityException || ex is NotSupportedException) { -#if DEBUG - logger.LogCritical(ex, "Could not open the file at {inputPath}, reason: {exMessage}", input, ex.Message); -#else - logger.LogCritical("Could not open the file at {inputPath}, reason: {exMessage}", input, ex.Message); -#endif - return null; + throw new InvalidOperationException($"Could not open the file at {input}", ex); } } stopwatch.Stop(); From 9544806f4dfde6bf97c5d165b8af92baaeac5bd0 Mon Sep 17 00:00:00 2001 From: Maggie Kimani Date: Tue, 8 Mar 2022 22:21:04 +0300 Subject: [PATCH 7/7] Add exit codes for process termination handling --- src/Microsoft.OpenApi.Hidi/OpenApiService.cs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.OpenApi.Hidi/OpenApiService.cs b/src/Microsoft.OpenApi.Hidi/OpenApiService.cs index 11fe6decc..2cf1b01ad 100644 --- a/src/Microsoft.OpenApi.Hidi/OpenApiService.cs +++ b/src/Microsoft.OpenApi.Hidi/OpenApiService.cs @@ -28,7 +28,7 @@ namespace Microsoft.OpenApi.Hidi { public class OpenApiService { - public static async Task ProcessOpenApiDocument( + public static async Task ProcessOpenApiDocument( string openapi, string csdl, FileInfo output, @@ -174,6 +174,8 @@ CancellationToken cancellationToken logger.LogTrace($"Finished serializing in {stopwatch.ElapsedMilliseconds}ms"); textWriter.Flush(); + + return 0; } catch (Exception ex) { @@ -182,7 +184,7 @@ CancellationToken cancellationToken #else logger.LogCritical(ex.Message); #endif - return; + return 1; } } @@ -318,7 +320,7 @@ public static Dictionary> ParseJsonCollectionFile(Stream st return requestUrls; } - internal static async Task ValidateOpenApiDocument(string openapi, LogLevel loglevel, CancellationToken cancellationToken) + internal static async Task ValidateOpenApiDocument(string openapi, LogLevel loglevel, CancellationToken cancellationToken) { var logger = ConfigureLoggerInstance(loglevel); @@ -352,6 +354,8 @@ internal static async Task ValidateOpenApiDocument(string openapi, LogLevel logl logger.LogTrace("Finished walking through the OpenApi document. Generating a statistics report.."); logger.LogInformation(statsVisitor.GetStatisticsReport()); + + return 0; } catch(Exception ex) { @@ -360,7 +364,7 @@ internal static async Task ValidateOpenApiDocument(string openapi, LogLevel logl #else logger.LogCritical(ex.Message); #endif - return; + return 1; } }