Skip to content

Read the error body asynchronously and expose Gotenberg-Trace - #89

Merged
Jaben merged 1 commit into
ChangemakerStudios:developfrom
lahma:fix/async-error-path-and-trace
Nov 8, 2026
Merged

Jaben merged 1 commit into
ChangemakerStudios:developfrom
lahma:fix/async-error-path-and-trace

Conversation

@lahma

@lahma lahma commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

Reading an error response had three problems, all in GotenbergApiException:

  • Create read the body with .Result. That blocks a thread inside every async conversion path, on a response fetched with ResponseHeadersRead, so the body may still be in transit. It also ignored the caller's cancellation token.
  • The exception kept the HttpResponseMessage and never disposed it unless ToVerboseJson was called.
  • The response's Gotenberg-Trace header was dropped. The request side supports setting it (ConfigBuilder.SetTrace), but correlating a failure with Gotenberg's log needs the value Gotenberg sent back. Gotenberg 9 puts the trace header on every response, 404 and 405 included.

Changes:

  • New GotenbergApiException.CreateAsync(request, response, cancelToken). It reads the body with the caller's token and disposes the response, also when reading fails or is cancelled. SendRequestAsync uses it for every error response. Create stays for source compatibility and is marked [Obsolete]; it now disposes the response as well.
  • New GotenbergApiException.Trace (string?).
  • The exception copies what it reports from the response (status, reason, version, headers, content headers, request URI and method) and no longer holds the response, which is what makes disposing it safe.

Behaviour change in ToVerboseJson: GotenbergResponseReceived used to be a reflection dump of HttpResponseMessage: headers as [{"Key": ..., "Value": [...]}] arrays, plus TrailingHeaders and a nested RequestMessage. It is now a fixed set: StatusCode, ReasonPhrase, Version, IsSuccessStatusCode, RequestUri, RequestMethod, Headers and ContentHeaders (name to comma-joined value). I captured the old output on develop to compare. Trace is left out of the dump because it is on the exception. This overlaps with #82, which also rewrites ToVerboseJson and chooses a similar fixed shape, so whichever merges second will need a small rebase.

Tests (GotenbergApiExceptionTests):

  • CreateAsync captures status, message and trace, and disposes the response.
  • No trace header gives a null Trace.
  • Cancellation while the body is still in transit throws OperationCanceledException and still disposes the response.
  • ToVerboseJson works from the copy after the response is disposed, and can be called twice.
  • Through the DI registration, an error response yields Trace and a disposed response.

The full suite passes against gotenberg/gotenberg:8: 251 tests, all target frameworks build.

I've left CHANGES.MD alone to avoid conflicts between the small PRs. A suggested line: Added: GotenbergApiException.Trace and CreateAsync. Changed: error bodies are read asynchronously with the caller's cancellation token, the response is disposed, Create is obsolete, and the GotenbergResponseReceived section of ToVerboseJson has a fixed shape.

GotenbergApiException.Create read the error body with .Result, blocking
a thread inside every async conversion path on a response fetched with
ResponseHeadersRead, so the body may still be in transit. It also kept
the HttpResponseMessage and never disposed it unless ToVerboseJson was
called, and dropped the Gotenberg-Trace header, which is what correlates
a failure with Gotenberg's log.

Add CreateAsync, which reads the body with the caller's cancellation
token and disposes the response, and use it for every error response.
Create is kept and marked obsolete. The exception now copies what it
reports (status, headers, request URI and method) instead of holding the
response, exposes the header as Trace, and ToVerboseJson dumps that copy.
@coderabbitai

coderabbitai Bot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 37 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 2 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: e0588d29-cafa-4998-9c1c-f7441d3b7568
Commits

Reviewing files that changed from the base of the PR and between b3998d7 and 872269f.

Files selected for processing: 3

  • src/Gotenberg.Sharp.Api.Client/GotenbergSharpClient.cs
  • src/Gotenberg.Sharp.Api.Client/Infrastructure/GotenbergApiException.cs
  • test/GotenbergSharpClient.Tests/GotenbergApiExceptionTests.cs
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@Jaben
Jaben merged commit 55016c1 into ChangemakerStudios:develop Nov 8, 2026
1 check passed
@lahma
lahma deleted the fix/async-error-path-and-trace branch November 8, 2026 11:59
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