Skip to content

Mjansrud csharp throw on any error#23684

Closed
wing328 wants to merge 6 commits intomasterfrom
mjansrud-csharp-throw-on-any-error
Closed

Mjansrud csharp throw on any error#23684
wing328 wants to merge 6 commits intomasterfrom
mjansrud-csharp-throw-on-any-error

Conversation

@wing328
Copy link
Copy Markdown
Member

@wing328 wing328 commented May 4, 2026

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR solves a reported issue, reference it using GitHub's linking syntax (e.g., having "fixes #123" present in the PR description)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

Summary by cubic

Adds an opt-in throwOnAnyError option for C# RestSharp clients to surface deserialization and transport errors by throwing them, including when a Polly retry policy is used. Default stays off to preserve current behavior.

  • New Features

    • New generator switch throwOnAnyError (restsharp only, default false); documented in csharp generator options.
    • When enabled: set RestClientOptions.ThrowOnAnyError = true and rethrow PolicyResult.FinalException in the retry path so errors reach callers and logs.
    • Sample config updated to include throwOnAnyError: true.
  • Migration

    • To enable, add throwOnAnyError: true under additionalProperties in your config, or pass --additional-properties throwOnAnyError=true.
    • No changes needed if you want to keep the current null-on-error behavior.

Written for commit 13e4897. Summary will update on new commits.

mjansrud and others added 6 commits April 30, 2026 07:46
By default, RestSharp swallows deserialization and transport
exceptions into RestResponse.ErrorException, and the generated
ToApiResponse<T> in this template only carries ErrorText — the
actual exception is dropped. Combined with a generated GetXxxAsync
that returns Data directly, callers silently receive null on any
deserialization failure (e.g. a required property missing in the
upstream JSON). The error never reaches application logs or APM.

Add an opt-in `throwOnAnyError` switch (default false, restsharp
library only) that sets `ThrowOnAnyError = true` on RestClientOptions,
making RestSharp rethrow the original ApiException(500, ...) that
the generated deserializer already throws. The exception then
propagates to the caller and into normal application error handling.

Default kept off to preserve backwards compatibility — opt in when
you want bugs to surface instead of silently producing null/[].
…gured

Polly's ExecuteAndCapture catches the rethrown ApiException, so without
this change the option is silently neutralized whenever
RetryConfiguration.RetryPolicy != null: the exception ends up in
RestResponse.ErrorException, which ToApiResponse discards.

When throwOnAnyError is enabled, rethrow PolicyResult.FinalException
from DeserializeRestResponseFromPolicyAsync so the contract is
consistent across both the no-retry and retry paths. Default-off branch
is byte-identical to the previous output.
Polly's PolicyResult.FinalException is null when Outcome is Failure but
the failure type is ResultHandledByThisPolicy (e.g. a retry policy
configured with .HandleResult(...) that gives up after N retries).
Throwing it directly would NRE; fall back to InvalidOperationException
so the option still surfaces an error.

Also restores the trailing whitespace on the if line so existing
restsharp samples don't all need regenerating.
@wing328 wing328 closed this May 4, 2026
@wing328 wing328 deleted the mjansrud-csharp-throw-on-any-error branch May 4, 2026 06:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants