Skip to content

Commit

Permalink
RestApiOperationException and OpenApiDocumentParsingException are rep…
Browse files Browse the repository at this point in the history
…laced by SKException.
  • Loading branch information
SergeyMenshykh committed Jul 18, 2023
1 parent 582d560 commit 38fa1a9
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 89 deletions.
9 changes: 5 additions & 4 deletions dotnet/src/Skills/Skills.OpenAPI/Model/RestApiOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Web;
using Microsoft.SemanticKernel.Diagnostics;

namespace Microsoft.SemanticKernel.Skills.OpenAPI.Model;

Expand Down Expand Up @@ -162,12 +163,12 @@ public Uri BuildOperationUrl(IDictionary<string, string> arguments)

//Getting metadata for the header
var headerMetadata = this.Parameters.FirstOrDefault(p => p.Location == RestApiOperationParameterLocation.Header && p.Name == headerName)
?? throw new RestApiOperationException($"No value for the '{headerName} header is found.'");
?? throw new SKException($"No value for the '{headerName} header is found.'");

//If parameter is required it's value should always be provided.
if (headerMetadata.IsRequired)
{
throw new RestApiOperationException($"No value for the '{headerName} header is found.'");
throw new SKException($"No value for the '{headerName} header is found.'");
}

//Parameter is not required and no default value provided.
Expand Down Expand Up @@ -207,7 +208,7 @@ string ReplaceParameter(Match match)
var parameterMetadata = this.Parameters.First(p => p.Location == RestApiOperationParameterLocation.Path && p.Name == parameterName);
if (parameterMetadata?.DefaultValue == null)
{
throw new RestApiOperationException($"No argument found for parameter - '{parameterName}' for operation - '{this.Id}'");
throw new SKException($"No argument found for parameter - '{parameterName}' for operation - '{this.Id}'");
}

return parameterMetadata.DefaultValue;
Expand Down Expand Up @@ -246,7 +247,7 @@ private string AddQueryString(string path, IDictionary<string, string> arguments
//Throw an exception if the parameter is a required one but no value is provided.
if (parameter.IsRequired)
{
throw new RestApiOperationException($"No argument found for required query string parameter - '{parameter.Name}' for operation - '{this.Id}'");
throw new SKException($"No argument found for required query string parameter - '{parameter.Name}' for operation - '{this.Id}'");
}
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Readers;
using Microsoft.SemanticKernel.Diagnostics;
using Microsoft.SemanticKernel.Skills.OpenAPI.Model;
using Microsoft.SemanticKernel.Text;

Expand Down Expand Up @@ -94,7 +95,7 @@ private async Task<JsonObject> DowngradeDocumentVersionToSupportedOneAsync(Strea
if (jsonObject == null)
{
// The document is malformed.
throw new OpenApiDocumentParsingException("Parsing of OpenAPI document failed.");
throw new SKException("Parsing of OpenAPI document failed.");
}

if (!jsonObject.TryGetPropertyValue(OpenApiVersionPropertyName, out var propertyNode))
Expand Down Expand Up @@ -211,12 +212,12 @@ private static List<RestApiOperationParameter> CreateRestApiOperationParameters(
{
if (parameter.In == null)
{
throw new OpenApiDocumentParsingException($"Parameter location of {parameter.Name} parameter of {operationId} operation is undefined.");
throw new SKException($"Parameter location of {parameter.Name} parameter of {operationId} operation is undefined.");
}

if (parameter.Style == null)
{
throw new OpenApiDocumentParsingException($"Parameter style of {parameter.Name} parameter of {operationId} operation is undefined.");
throw new SKException($"Parameter style of {parameter.Name} parameter of {operationId} operation is undefined.");
}

var restParameter = new RestApiOperationParameter(
Expand Down Expand Up @@ -262,7 +263,7 @@ private static List<RestApiOperationParameter> CreateRestApiOperationParameters(
var mediaType = s_supportedMediaTypes.FirstOrDefault(smt => requestBody.Content.ContainsKey(smt));
if (mediaType == null)
{
throw new OpenApiDocumentParsingException($"Neither of the media types of {operationId} is supported.");
throw new SKException($"Neither of the media types of {operationId} is supported.");
}

var mediaTypeMetadata = requestBody.Content[mediaType];
Expand Down Expand Up @@ -290,7 +291,7 @@ private static List<RestApiOperationParameter> CreateRestApiOperationParameters(

if (level > PayloadPropertiesHierarchyMaxDepth)
{
throw new OpenApiDocumentParsingException($"Max level {PayloadPropertiesHierarchyMaxDepth} of traversing payload properties of {operationId} operation is exceeded.");
throw new SKException($"Max level {PayloadPropertiesHierarchyMaxDepth} of traversing payload properties of {operationId} operation is exceeded.");
}

var result = new List<RestApiOperationPayloadProperty>();
Expand Down Expand Up @@ -375,7 +376,7 @@ private static List<RestApiOperationParameter> CreateRestApiOperationParameters(
return passwordValue.Value.ToString(CultureInfo.InvariantCulture);

default:
throw new OpenApiDocumentParsingException($"The value type - {value.PrimitiveType} is not supported.");
throw new SKException($"The value type - {value.PrimitiveType} is not supported.");
}
}

Expand All @@ -397,7 +398,7 @@ private void AssertReadingSuccessful(ReadResult readResult, bool ignoreNonCompli

if (!ignoreNonCompliantErrors)
{
throw new OpenApiDocumentParsingException(message);
throw new SKException(message);
}
}
}
Expand Down

This file was deleted.

9 changes: 5 additions & 4 deletions dotnet/src/Skills/Skills.OpenAPI/RestApiOperationRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Text.Json.Nodes;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.SemanticKernel.Diagnostics;
using Microsoft.SemanticKernel.Skills.OpenAPI.Authentication;
using Microsoft.SemanticKernel.Skills.OpenAPI.Model;

Expand Down Expand Up @@ -143,13 +144,13 @@ public RestApiOperationRunner(HttpClient httpClient, AuthenticateRequestAsyncCal
{
if (!arguments.TryGetValue(RestApiOperation.ContentTypeArgumentName, out mediaType))
{
throw new RestApiOperationException($"No content type is provided for the {operation.Id} operation.");
throw new SKException($"No content type is provided for the {operation.Id} operation.");
}
}

if (!s_payloadFactoryByMediaType.TryGetValue(mediaType!, out var payloadFactory))
{
throw new RestApiOperationException($"The media type {mediaType} of the {operation.Id} operation is not supported by {nameof(RestApiOperationRunner)}.");
throw new SKException($"The media type {mediaType} of the {operation.Id} operation is not supported by {nameof(RestApiOperationRunner)}.");
}

return payloadFactory.Invoke(arguments);
Expand All @@ -164,7 +165,7 @@ private static HttpContent BuildAppJsonPayload(IDictionary<string, string> argum
{
if (!arguments.TryGetValue(RestApiOperation.PayloadArgumentName, out var content))
{
throw new RestApiOperationException($"No argument is found for the '{RestApiOperation.PayloadArgumentName}' payload content.");
throw new SKException($"No argument is found for the '{RestApiOperation.PayloadArgumentName}' payload content.");
}

return new StringContent(content, Encoding.UTF8, MediaTypeApplicationJson);
Expand All @@ -179,7 +180,7 @@ private static HttpContent BuildPlainTextPayload(IDictionary<string, string> arg
{
if (!arguments.TryGetValue(RestApiOperation.PayloadArgumentName, out var propertyValue))
{
throw new RestApiOperationException($"No argument is found for the '{RestApiOperation.PayloadArgumentName}' payload content.");
throw new SKException($"No argument is found for the '{RestApiOperation.PayloadArgumentName}' payload content.");
}

return new StringContent(propertyValue, Encoding.UTF8, MediaTypeTextPlain);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Net.Http;
using System.Web;
using Microsoft.SemanticKernel.Diagnostics;
using Microsoft.SemanticKernel.Skills.OpenAPI.Model;
using Xunit;

Expand Down Expand Up @@ -257,7 +258,7 @@ public void ShouldThrowExceptionIfNoValueIsProvideForRequiredQueryStringParamete
};

//Act and assert
Assert.Throws<RestApiOperationException>(() => sut.BuildOperationUrl(arguments));
Assert.Throws<SKException>(() => sut.BuildOperationUrl(arguments));
}

[Theory]
Expand Down Expand Up @@ -404,7 +405,7 @@ public void ItShouldThrowExceptionIfHeadersHaveNoValuesAndHeadersMetadataNotSupp
void Act() => sut.RenderHeaders(new Dictionary<string, string>());

// Assert
Assert.Throws<RestApiOperationException>(Act);
Assert.Throws<SKException>(Act);
}

[Fact]
Expand All @@ -429,7 +430,7 @@ public void ShouldThrowExceptionIfNoValueProvidedForRequiredHeader()
void Act() => sut.RenderHeaders(new Dictionary<string, string>());

// Assert
Assert.Throws<RestApiOperationException>(Act);
Assert.Throws<SKException>(Act);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.SemanticKernel.Diagnostics;
using Microsoft.SemanticKernel.Skills.OpenAPI.Model;
using Microsoft.SemanticKernel.Skills.OpenAPI.OpenApi;
using SemanticKernel.Skills.UnitTests.OpenAPI.TestSkills;
Expand Down Expand Up @@ -225,7 +226,7 @@ public async Task ItShouldThrowExceptionForNonCompliantDocumentAsync()
var nonComplaintOpenApiDocument = ResourceSkillsProvider.LoadFromResource("nonCompliant_documentV3_0.json");

// Act and Assert
await Assert.ThrowsAsync<OpenApiDocumentParsingException>(async () => await this._sut.ParseAsync(nonComplaintOpenApiDocument));
await Assert.ThrowsAsync<SKException>(async () => await this._sut.ParseAsync(nonComplaintOpenApiDocument));
}

[Fact]
Expand Down

0 comments on commit 38fa1a9

Please sign in to comment.