Skip to content

Harden the docs API error paths and drop dead startup code - #4894

Merged
akira28 merged 3 commits into
mainfrom
fix/docs-api-hardening
Nov 18, 2026
Merged

akira28 merged 3 commits into
mainfrom
fix/docs-api-hardening

Conversation

@akira28

@akira28 akira28 commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Fixes six small problems in the docs API (Elastic.Documentation.Api). Each one is either a bug a client can hit or code that does nothing. A temporarily unavailable search backend now answers 503 with Retry-After instead of an empty 500. /search can no longer pass an unbounded page size to Elasticsearch. Failed Ask AI feedback is no longer reported as stored.

Affects: API, MCP

Closes: elastic/docs-eng-team#1206

Prompt summary: Review the whole docs API for refactoring opportunities, then fix the small, safe findings in one follow-up PR, separate from the API operations work in #4887.

Why

A review of the API during #4887 found these. They are independent of that PR and of each other, and each is small.

What

Errors

  • Search unavailable: the exception handler turns SearchUnavailableException (a timeout or an overloaded cluster) into a 503 with Retry-After: 5, and logs it as a warning.
  • Other errors: every other unhandled error is still a 500. It now has a body instead of none.
  • Error body: both use { "errorType", "message" }, a new ApiError record in ApiJsonContext. The 500 message does not include exception details.

The MCP server already treated SearchUnavailableException as retryable; the API did not.

Search limits

FullSearchService clamps page to at least 1 and size to 1–50 before it queries the index. Before, /search?size=0 or size=100000 reached Elasticsearch and came back as a 500.

Both the API and MCP go through FullSearchService, so the MCP tool's own clamp is removed. The site's largest page size is 50.

Ask AI

  • Feedback: IAskAiMessageFeedbackService.RecordFeedbackAsync returns whether the write succeeded, like IPageFeedbackService. The endpoint answers 503 when it did not; before, it returned 204 and the feedback was lost.
  • Activity sources: both handlers created a new ActivitySource on every request and never disposed it, so each one stayed registered for the life of the process. They are now static readonly, as in the stream transformers and the cache.
  • Stream activity: if the Ask AI call or the transform throws, the handler now ends the stream activity. Before that point, the transformer doesn't own it yet.

Startup and dead code

  • GetLogger: it built the whole service provider (four times in the API, once in the search project) only to log "registered successfully" messages, inside try/log/rethrow blocks. It is gone. The one useful message, an unknown environment name, now goes to the console like the rest of startup.
  • Removed: IParameterProvider and its Lambda and local implementations (a comment already marked them for removal), the test fake for it, the unreferenced Validation/IValidator, and the named "StreamingHttpClient".
  • The named client: nothing requested it, because the Ask AI gateways take an unnamed HttpClient. Its 10-minute timeout never applied, so removing it changes nothing.

Tests for the fixes

  • Feedback gateway: AskAiMessageFeedbackGatewayTests drives the real gateway through an in-memory transport with 200, 201, 400 and 503 responses.
  • Stream activity: AskAiStreamFailureTests makes AskAi and then the transform throw, and checks the chat activity was ended with an Error status. Removing the handler's cleanup fails both cases.
  • Span tests one at a time: every factory listens to every Ask AI span in the process, so the tests that read those spans (AskAiStreamFailureTests, EuidEnrichmentTests) share a NotInParallel key.

Verify

dotnet test --project tests/Elastic.Documentation.Api.Tests
# ApiErrorResponseTests: 503 with Retry-After, 500 with a body, feedback 204 and 503
# AskAiMessageFeedbackGatewayTests, AskAiStreamFailureTests: the gateway result and the span on failure
# FullSearchServiceTests: page and size clamped before the index query
dotnet test --project tests/Mcp.Remote.Tests
dotnet test --project tests/Elastic.Documentation.Api.Infrastructure.Tests

All three pass locally (282, 76 and 87 tests).

Out of scope: one error format for every endpoint and typed results. Those fit with the OpenAPI spec in elastic/docs-eng-team#1205.

Stack: second layer of #4895, on top of #4887. #4896 builds on it.

🤖 Generated with Claude Code

@akira28
akira28 requested a review from a team as a code owner November 17, 2026 14:35
@akira28
akira28 requested a review from technige November 17, 2026 14:35
@akira28 akira28 added the bug label Nov 17, 2026
@akira28
akira28 force-pushed the fix/docs-api-hardening branch from 60bcb42 to 4cc921a Compare November 17, 2026 14:40
@akira28
akira28 changed the base branch from main to feature/api-operations-rest-endpoints November 17, 2026 14:40
@akira28
akira28 added this pull request to stack #4895 November 17, 2026 14:40
@github-actions

Copy link
Copy Markdown
Contributor

@akira28
akira28 force-pushed the fix/docs-api-hardening branch from b4ee6a4 to 7c011a2 Compare November 17, 2026 16:20
Base automatically changed from feature/api-operations-rest-endpoints to main November 18, 2026 09:19
@akira28
akira28 force-pushed the fix/docs-api-hardening branch from 069fae6 to a76316a Compare November 18, 2026 09:19
akira28 and others added 3 commits November 18, 2026 10:38
A search backend that is temporarily unavailable now answers 503 with
Retry-After, and every unhandled error has a JSON body. /search clamps
page and size once in FullSearchService. Ask AI message feedback answers
503 when it could not be stored. The Ask AI activity sources are created
once, and the stream activity ends when the call throws. Startup no longer
builds the service provider to log, and the unused parameter providers,
validator and named HTTP client are gone.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The gateway test drives the real gateway through an in-memory transport, so returning true on a failed write fails it. The stream tests fail AskAi and the transformer in turn and check the chat activity was ended with an error; without the handler's catch, the activity is never exported.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Every factory's listener sees every Ask AI span in the process, so EuidEnrichmentTests could read the span of a parallel AskAiStreamFailureTests run, which has no euid.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@akira28
akira28 force-pushed the fix/docs-api-hardening branch from a76316a to 12207a5 Compare November 18, 2026 09:38
@akira28
akira28 merged commit f925e24 into main Nov 18, 2026
44 checks passed
@akira28
akira28 deleted the fix/docs-api-hardening branch November 18, 2026 10:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants