Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(csharp): Move exception handler into client options as an internal property for SDK authors to configure #6282

Merged
merged 1 commit into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Threading.Tasks;
using SystemTask = global::System.Threading.Tasks.Task;

namespace <%= namespace%>;

Expand Down Expand Up @@ -52,7 +51,7 @@ internal T TryCatch<T>(Func<T> func)
}
}

internal async Task TryCatchAsync(Func<Task> func)
internal async SystemTask TryCatchAsync(Func<Task> func)
{
if (_interceptor == null)
{
Expand Down Expand Up @@ -86,4 +85,6 @@ internal async Task<T> TryCatchAsync<T>(Func<Task<T>> func)
throw _interceptor.Intercept(ex);
}
}

internal ExceptionHandler Clone() => new ExceptionHandler(_interceptor);
}
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ export abstract class AbstractEndpointGenerator {
if (returnType != null) {
writer.write("return ");
}
writer.writeLine("await _exceptionHandler.TryCatchAsync(async () => {");
writer.writeLine("await _client.Options.ExceptionHandler.TryCatchAsync(async () => {");
writer.indent();
}
body.write(writer);
Expand Down
25 changes: 24 additions & 1 deletion generators/csharp/sdk/src/options/ClientOptionsGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { BASE_URL_FIELD_NAME, BASE_URL_SUMMARY, BaseOptionsGenerator, OptionArgs

export const CLIENT_OPTIONS_CLASS_NAME = "ClientOptions";
export const GLOBAL_TEST_SETUP_NAME = "GlobalTestSetup";
export const EXCEPTION_HANDLER_MEMBER_NAME = "ExceptionHandler";

export class ClientOptionsGenerator extends FileGenerator<CSharpFile, SdkCustomConfigSchema, SdkGeneratorContext> {
private baseOptionsGenerator: BaseOptionsGenerator;
Expand Down Expand Up @@ -44,6 +45,27 @@ export class ClientOptionsGenerator extends FileGenerator<CSharpFile, SdkCustomC
class_.addField(this.getGrpcOptionsField());
}

if (this.context.includeExceptionHandler()) {
class_.addField(
csharp.field({
summary: "A handler that will handle exceptions thrown by the client.",
access: csharp.Access.Internal,
name: EXCEPTION_HANDLER_MEMBER_NAME,
type: csharp.Type.reference(this.context.getExceptionHandlerClassReference()),
get: true,
set: true,
initializer: csharp.codeblock((writer) => {
writer.writeNode(
csharp.instantiateClass({
classReference: this.context.getExceptionHandlerClassReference(),
arguments_: [csharp.codeblock("null")]
})
);
})
})
);
}

class_.addMethod(this.getCloneMethod(class_));

return new CSharpFile({
Expand Down Expand Up @@ -184,7 +206,8 @@ export class ClientOptionsGenerator extends FileGenerator<CSharpFile, SdkCustomC
HttpClient = HttpClient,
MaxRetries = MaxRetries,
Timeout = Timeout,
Headers = new Headers(new Dictionary<string, HeaderValue>(Headers))
Headers = new Headers(new Dictionary<string, HeaderValue>(Headers)),
${this.context.includeExceptionHandler() ? "ExceptionHandler = ExceptionHandler.Clone()," : ""}
}`
);
}),
Expand Down
41 changes: 0 additions & 41 deletions generators/csharp/sdk/src/root-client/RootClientGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@ import { OauthTokenProviderGenerator } from "../oauth/OauthTokenProviderGenerato

export const CLIENT_MEMBER_NAME = "_client";
export const GRPC_CLIENT_MEMBER_NAME = "_grpc";
export const EXCEPTION_HANDLER_MEMBER_NAME = "_exceptionHandler";

const GetFromEnvironmentOrThrow = "GetFromEnvironmentOrThrow";
const CLIENT_OPTIONS_PARAMETER_NAME = "clientOptions";
const EXCEPTION_INTERCEPTOR_PARAMETER_NAME = "exceptionInterceptor";

interface ConstructorParameter {
name: string;
Expand Down Expand Up @@ -104,17 +102,6 @@ export class RootClientGenerator extends FileGenerator<CSharpFile, SdkCustomConf
);
}

if (this.context.includeExceptionHandler()) {
class_.addField(
csharp.field({
access: csharp.Access.Private,
readonly: true,
name: EXCEPTION_HANDLER_MEMBER_NAME,
type: csharp.Type.reference(this.context.getExceptionHandlerClassReference())
})
);
}

class_.addConstructor(this.getConstructorMethod());

for (const subpackage of this.getSubpackages()) {
Expand Down Expand Up @@ -184,18 +171,6 @@ export class RootClientGenerator extends FileGenerator<CSharpFile, SdkCustomConf
);
}

if (this.context.includeExceptionHandler()) {
parameters.push(
csharp.parameter({
name: EXCEPTION_INTERCEPTOR_PARAMETER_NAME,
type: csharp.Type.optional(
csharp.Type.reference(this.context.getExceptionInterceptorClassReference())
),
initializer: "null"
})
);
}

parameters.push(
csharp.parameter({
name: CLIENT_OPTIONS_PARAMETER_NAME,
Expand Down Expand Up @@ -291,24 +266,11 @@ export class RootClientGenerator extends FileGenerator<CSharpFile, SdkCustomConf
writer.endControlFlow();
writer.endControlFlow();

if (this.context.includeExceptionHandler()) {
writer.writeLine("_exceptionHandler = ");
writer.writeNodeStatement(
csharp.instantiateClass({
classReference: this.context.getExceptionHandlerClassReference(),
arguments_: [csharp.codeblock(EXCEPTION_INTERCEPTOR_PARAMETER_NAME)]
})
);
}

if (this.oauth != null) {
const authClientClassReference = this.context.getSubpackageClassReferenceForServiceIdOrThrow(
this.oauth.configuration.tokenEndpoint.endpointReference.serviceId
);
const arguments_ = [csharp.codeblock("new RawClient(clientOptions.Clone())")];
if (this.context.includeExceptionHandler()) {
arguments_.push(csharp.codeblock("_exceptionHandler"));
}
writer.write("var tokenProvider = new OAuthTokenProvider(clientId, clientSecret, ");
writer.writeNode(
csharp.instantiateClass({
Expand Down Expand Up @@ -347,9 +309,6 @@ export class RootClientGenerator extends FileGenerator<CSharpFile, SdkCustomConf
);
}
const arguments_ = [csharp.codeblock("_client")];
if (this.context.includeExceptionHandler()) {
arguments_.push(csharp.codeblock("_exceptionHandler"));
}
for (const subpackage of this.getSubpackages()) {
writer.writeLine(`${subpackage.name.pascalCase.safeName} = `);
writer.writeNodeStatement(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { GrpcClientInfo } from "../grpc/GrpcClientInfo";

export const CLIENT_MEMBER_NAME = "_client";
export const GRPC_CLIENT_MEMBER_NAME = "_grpc";
export const EXCEPTION_HANDLER_MEMBER_NAME = "_exceptionHandler";

export declare namespace SubClientGenerator {
interface Args {
Expand Down Expand Up @@ -72,17 +71,6 @@ export class SubPackageClientGenerator extends FileGenerator<CSharpFile, SdkCust
);
}

if (this.context.includeExceptionHandler()) {
class_.addField(
csharp.field({
access: csharp.Access.Private,
readonly: true,
name: EXCEPTION_HANDLER_MEMBER_NAME,
type: csharp.Type.reference(this.context.getExceptionHandlerClassReference())
})
);
}

for (const subpackage of this.getSubpackages()) {
class_.addField(
csharp.field({
Expand Down Expand Up @@ -127,22 +115,11 @@ export class SubPackageClientGenerator extends FileGenerator<CSharpFile, SdkCust
type: csharp.Type.reference(this.context.getRawClientClassReference())
})
];
if (this.context.includeExceptionHandler()) {
parameters.push(
csharp.parameter({
name: "exceptionHandler",
type: csharp.Type.reference(this.context.getExceptionHandlerClassReference())
})
);
}
return {
access: csharp.Access.Internal,
parameters,
body: csharp.codeblock((writer) => {
writer.writeLine("_client = client;");
if (this.context.includeExceptionHandler()) {
writer.writeLine("_exceptionHandler = exceptionHandler;");
}

if (this.grpcClientInfo != null) {
writer.writeLine("_grpc = _client.Grpc;");
Expand All @@ -157,9 +134,6 @@ export class SubPackageClientGenerator extends FileGenerator<CSharpFile, SdkCust
}

const arguments_ = [csharp.codeblock("_client")];
if (this.context.includeExceptionHandler()) {
arguments_.push(csharp.codeblock("_exceptionHandler"));
}
for (const subpackage of this.getSubpackages()) {
writer.writeLine(`${subpackage.name.pascalCase.safeName} = `);
writer.writeNodeStatement(
Expand Down
7 changes: 7 additions & 0 deletions generators/csharp/sdk/versions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
# Set `enable-forward-compatible-enums` to `false` in the configuration to generate the old enums.
# irVersion: 53

- version: 1.12.0-rc7
createdAt: "2025-03-03"
irVersion: 56
changelogEntry:
- type: internal
summary: Move exception handler into client options as an internal property for SDK authors to configure.

- version: 1.12.0-rc6
createdAt: "2025-03-03"
irVersion: 56
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading