Skip to content

Commit 0f2ea15

Browse files
Updates to show exception handling and more protocol method examples (#42723)
Adding new exception handling documentation --------- Co-authored-by: Scott Addie <[email protected]>
1 parent 824bde0 commit 0f2ea15

File tree

9 files changed

+99
-8
lines changed

9 files changed

+99
-8
lines changed

docs/azure/sdk/protocol-convenience-methods.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ The preceding code demonstrates the following `System.ClientModel` convenience m
9595

9696
The following code uses a `ChatClient` to call the `CompleteChat` protocol method:
9797

98-
:::code source="snippets/protocol-convenience-methods/SCM/Protocol/Program.cs" highlight="26-31":::
98+
:::code source="snippets/protocol-convenience-methods/SCM/Protocol/Program.cs" highlight="31-34":::
9999

100100
The preceding code demonstrates the following `System.ClientModel` protocol method patterns:
101101

@@ -129,6 +129,20 @@ PipelineResponse response = result.GetRawResponse();
129129

130130
---
131131

132+
## Handle exceptions
133+
134+
When a service call fails, the service client throws an exception that exposes the HTTP status code and the details of the service response, if available. A `System.ClientModel`-dependent library throws a <xref:System.ClientModel.ClientResultException>, while an `Azure.Core`-dependent library throws a <xref:Azure.RequestFailedException>.
135+
136+
# [System.ClientModel exceptions](#tab/system-clientmodel)
137+
138+
:::code source="snippets/protocol-convenience-methods/AzureCore/ExceptionHandling/Program.cs" highlight="21-24":::
139+
140+
# [Azure.Core exceptions](#tab/azure-core)
141+
142+
:::code source="snippets/protocol-convenience-methods/SCM/ExceptionHandling/Program.cs" highlight="17-20":::
143+
144+
---
145+
132146
## Protocol and convenience method usage guidance
133147

134148
Although the Azure SDK for .NET client libraries provide the option to use either protocol or convenience methods, prioritize using convenience methods in most scenarios. Convenience methods are designed to improve the development experience and provide flexibility for authoring requests and handling responses. However, both method types can be used in your app as needed. Consider the following criteria when deciding which type of method to use.

docs/azure/sdk/snippets/protocol-convenience-methods/AzureCore/Convenience/AzureCoreConvenience.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net9.0</TargetFramework>
5+
<TargetFramework>net8.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
</PropertyGroup>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Azure.AI.ContentSafety" />
12+
<PackageReference Include="Azure.Identity" />
13+
</ItemGroup>
14+
15+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using Azure.AI.ContentSafety;
2+
using Azure.Identity;
3+
using Azure;
4+
5+
// Create the client
6+
ContentSafetyClient client = new(
7+
new Uri("https://contentsafetyai.cognitiveservices.azure.com/"),
8+
new DefaultAzureCredential());
9+
10+
try
11+
{
12+
// Call the convenience method
13+
AnalyzeTextResult result = client.AnalyzeText("What is Microsoft Azure?");
14+
15+
// Display the results
16+
foreach (TextCategoriesAnalysis item in result.CategoriesAnalysis)
17+
{
18+
Console.WriteLine($"{item.Category}: {item.Severity}");
19+
}
20+
}
21+
catch (RequestFailedException ex)
22+
{
23+
Console.WriteLine($"Error: {ex.Message}");
24+
}

docs/azure/sdk/snippets/protocol-convenience-methods/AzureCore/Protocol/AzureCoreProtocol.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net9.0</TargetFramework>
5+
<TargetFramework>net8.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
</PropertyGroup>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using OpenAI.Chat;
2+
using System.ClientModel;
3+
4+
// Create the client
5+
ChatClient client = new(
6+
model: "gpt-4o-mini",
7+
credential: Environment.GetEnvironmentVariable("OPENAI_API_KEY")!);
8+
9+
try
10+
{
11+
// Call the convenience method
12+
ChatCompletion completion = client.CompleteChat("What is Microsoft Azure?");
13+
14+
// Display the results
15+
Console.WriteLine($"[{completion.Role}]: {completion}");
16+
}
17+
catch (ClientResultException ex)
18+
{
19+
Console.WriteLine($"Error: {ex.Message}");
20+
}
21+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="OpenAI" />
12+
</ItemGroup>
13+
14+
</Project>

docs/azure/sdk/snippets/protocol-convenience-methods/SCM/Protocol/Program.cs

+7-4
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,16 @@
2222
"""u8.ToArray());
2323
using BinaryContent content = BinaryContent.Create(input);
2424

25+
var requestOptions = new RequestOptions();
26+
27+
requestOptions.AddHeader("CustomHeader", "CustomHeaderValue");
28+
requestOptions.ErrorOptions = ClientErrorBehaviors.NoThrow;
29+
2530
// Call the protocol method
2631
ClientResult result = client.CompleteChat(
2732
content,
28-
new RequestOptions
29-
{
30-
ErrorOptions = ClientErrorBehaviors.NoThrow,
31-
});
33+
requestOptions
34+
);
3235

3336
PipelineResponse response = result.GetRawResponse();
3437

docs/azure/sdk/snippets/protocol-convenience-methods/SCM/Protocol/SCMProtocol.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net9.0</TargetFramework>
5+
<TargetFramework>net8.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
</PropertyGroup>

0 commit comments

Comments
 (0)