From 167a8de93bec6f4e55170554f47f2e84f91e24a8 Mon Sep 17 00:00:00 2001 From: guythetechie <31325620+guythetechie@users.noreply.github.com> Date: Wed, 9 Oct 2024 19:56:15 -0400 Subject: [PATCH] Return an empty list if product groups are not supported (#681) Co-authored-by: Guy Fankam --- tools/code/common/ProductGroup.cs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/tools/code/common/ProductGroup.cs b/tools/code/common/ProductGroup.cs index 2b049f80..cc10340c 100644 --- a/tools/code/common/ProductGroup.cs +++ b/tools/code/common/ProductGroup.cs @@ -6,6 +6,8 @@ using System.Diagnostics.CodeAnalysis; using System.IO; using System.Linq; +using System.Net; +using System.Net.Http; using System.Threading; using System.Threading.Tasks; @@ -122,10 +124,19 @@ public sealed record ProductGroupDto public static class ProductGroupModule { - public static IAsyncEnumerable ListNames(this ProductGroupsUri uri, HttpPipeline pipeline, CancellationToken cancellationToken) => - pipeline.ListJsonObjects(uri.ToUri(), cancellationToken) + public static IAsyncEnumerable ListNames(this ProductGroupsUri uri, HttpPipeline pipeline, CancellationToken cancellationToken) + { + var exceptionHandler = (HttpRequestException exception) => + exception.StatusCode == HttpStatusCode.BadRequest + && exception.Message.Contains("MethodNotAllowedInPricingTier", StringComparison.OrdinalIgnoreCase) + ? AsyncEnumerable.Empty() + : throw exception; + + return pipeline.ListJsonObjects(uri.ToUri(), cancellationToken) .Select(jsonObject => jsonObject.GetStringProperty("name")) - .Select(GroupName.From); + .Select(GroupName.From) + .Catch(exceptionHandler); + } public static IAsyncEnumerable<(GroupName Name, ProductGroupDto Dto)> List(this ProductGroupsUri productGroupsUri, HttpPipeline pipeline, CancellationToken cancellationToken) => productGroupsUri.ListNames(pipeline, cancellationToken)