Skip to content
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
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<PackageVersion Include="Elastic.Mapping" Version="0.56.0" />
<PackageVersion Include="Elastic.Transport.VirtualizedCluster" Version="1.1.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="11.0.0" />
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="11.0.0" />
<PackageVersion Include="Microsoft.OpenApi" Version="3.11.0" />
<PackageVersion Include="Microsoft.OpenApi.Readers" Version="1.6.31" />
<PackageVersion Include="Tomlyn" Version="2.11.1" />
Expand Down
59 changes: 55 additions & 4 deletions docs/data/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ navigation_title: REST API

{{dbuild}} includes a REST API server (`Elastic.Documentation.Api`) that powers search, AI assistance, and content change feeds for the documentation site.

The server describes itself in an OpenAPI document at `/docs/_api/v1/openapi.json`, generated from its endpoints. It lists every endpoint with its parameters, request bodies, responses, and error codes. Requests through `www.elastic.co` need the cookie `feature_search_or_askai_enabled=true`; without it, the CDN answers `403`.

## Endpoints

| Endpoint | Description |
Expand Down Expand Up @@ -53,7 +55,7 @@ The response has `totalHits` and `results`. Each result has its `operation`, plu

### Errors

A request these resources can't answer returns `{ "errorType": "...", "message": "..." }`. The status is `404` when `errorType` ends in `_not_found`: `operation_not_found`, `api_not_found`, or `group_not_found`. Invalid arguments return `400` with `invalid_arguments`.
The API operations resources report errors like every other endpoint; see [Errors](#errors).

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Severity: P3 (Low)

The page now has two headings called "Errors": this ### Errors and the new ## Errors further down. [Errors](#errors) will resolve to the first one, which is this subsection, so the link points back to itself instead of to the problem-type reference. Give the new section a custom anchor (for example ## Errors [problems]) and link to that.

Generated by Libra. Specialist: libra-complexity. React with 👍 or 👎 to give feedback on this comment.


## Ask AI provider

Expand All @@ -67,6 +69,55 @@ Each deployment of the API server picks its Ask AI provider with the `DOCUMENTAT

The value is case-insensitive. Any other value stops the Ask AI service with a configuration error. LLM Gateway needs `LLM_GATEWAY_FUNCTION_URL` and `LLM_GATEWAY_SERVICE_ACCOUNT`. Agent Builder needs `DOCUMENTATION_KIBANA_URL` and `DOCUMENTATION_KIBANA_APIKEY`. A deployment that uses Agent Builder, such as Codex, must set `DOCUMENTATION_ASK_AI_PROVIDER=AgentBuilder`.

:::{note}
A formal OpenAPI specification should be generated from the API server as a follow-up. This page will be updated with full endpoint details, request/response schemas, and authentication requirements once the spec is available.
:::
## Errors

Every error is an [RFC 9457](https://www.rfc-editor.org/rfc/rfc9457) problem, served as `application/problem+json`:

```json
{
"type": "https://docs-v3-preview.elastic.dev/elastic/docs-builder/tree/main/data/api#invalid-arguments",
"title": "Invalid arguments",
"status": 400,
"detail": "Pass q or endpoint, not both.",
"traceId": "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01"
}
```

- `type` identifies the problem and links to its section below. Match on it, not on `title` or `detail`.
- `title` is the same for every occurrence of a type.
- `detail` explains this occurrence, for people.
- `traceId` names the request's trace. Quote it when you report a problem.

Errors from the CDN, such as the `403` for a missing cookie, aren't problems and aren't JSON.

### Invalid arguments [invalid-arguments]

`400`. A parameter is missing, doesn't parse, or contradicts another one. `detail` says which.

### Not found [not-found]

`404`. No endpoint matches the path.

### API operation not found [operation-not-found]

`404`. No API operation has that id, or serves that endpoint, in the requested API and version.

### API not found [api-not-found]

`404`. No API has that key, or it has no operations in the requested version.

### API group not found [group-not-found]

`404`. The API has no group with that name.

### Internal error [internal-error]

`500`. An unexpected failure. `detail` doesn't include its cause; use `traceId` to report it.

### Search unavailable [search-unavailable]

`503`. Elasticsearch timed out or is overloaded. Retry after the seconds in the `Retry-After` header.

### Feedback unavailable [feedback-unavailable]

`503`. Feedback could not be stored. Retry later.
98 changes: 98 additions & 0 deletions src/api/Elastic.Documentation.Api/ApiProblems.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information

using Elastic.Documentation.Search;
using Microsoft.AspNetCore.Http;

namespace Elastic.Documentation.Api;

/// <summary>
/// The RFC 9457 problem types of the docs API. Every error answers <c>application/problem+json</c> whose
/// <c>type</c> is one of these, resolving to its section of the REST API page (docs/data/api.md).
/// </summary>
public static class ApiProblems
{
private const string TypeBase = "https://docs-v3-preview.elastic.dev/elastic/docs-builder/tree/main/data/api#";

public const string InvalidArguments = "invalid-arguments";
public const string NotFound = "not-found";
public const string InternalError = "internal-error";
public const string SearchUnavailable = "search-unavailable";
public const string FeedbackUnavailable = "feedback-unavailable";

// A title is the same for every occurrence of a type (RFC 9457 §3.1.3); what varies goes in detail.
private static readonly Dictionary<string, string> Titles = new(StringComparer.Ordinal)
{
[InvalidArguments] = "Invalid arguments",
[NotFound] = "Not found",
["operation-not-found"] = "API operation not found",
["api-not-found"] = "API not found",
["group-not-found"] = "API group not found",
[InternalError] = "Internal error",
[SearchUnavailable] = "Search unavailable",
[FeedbackUnavailable] = "Feedback unavailable"
};

/// <summary>The absolute <c>type</c> URI of a problem type, such as <see cref="InvalidArguments"/>.</summary>
public static string TypeUri(string type) => TypeBase + type;

/// <summary>A problem response of a known type.</summary>
public static IResult Problem(int statusCode, string type, string detail) =>
Results.Problem(detail: detail, statusCode: statusCode, title: Titles[type], type: TypeUri(type));

/// <summary>The status an unhandled exception answers with.</summary>
public static int StatusCodeFor(Exception exception) => exception switch
{
// A parameter that does not bind, such as a missing q or a since that is not a date.
BadHttpRequestException badRequest => badRequest.StatusCode,
// Transient by construction (timeout, overload): tell clients and the CDN to retry.
SearchUnavailableException => StatusCodes.Status503ServiceUnavailable,
_ => StatusCodes.Status500InternalServerError
};

/// <summary>
/// Gives the problems ASP.NET writes itself (unhandled exceptions, bodyless status codes) a type of this API.
/// Problems this API builds with <see cref="Problem"/> already carry one and pass through unchanged.
/// </summary>
public static void Customize(ProblemDetailsContext context)
{
var problem = context.ProblemDetails;
switch (context.Exception)
{
case BadHttpRequestException badRequest:
Apply(problem, InvalidArguments, badRequest.Message);
return;
case SearchUnavailableException unavailable:
Apply(problem, SearchUnavailable, unavailable.Message);
context.HttpContext.Response.Headers.RetryAfter = "5";
return;
case not null:
// Never the exception's own message: it can name internals.
Apply(problem, InternalError, "An unexpected error occurred.");
return;
}

if (problem.Type is not null && problem.Type.StartsWith(TypeBase, StringComparison.Ordinal))
return;
switch (problem.Status)
{
case StatusCodes.Status400BadRequest:
Apply(problem, InvalidArguments, "A parameter is missing or does not parse.");
break;
case StatusCodes.Status404NotFound:
Apply(problem, NotFound, "No endpoint matches this path.");
break;
case StatusCodes.Status500InternalServerError:
Apply(problem, InternalError, "An unexpected error occurred.");
break;
}
}

private static void Apply(Microsoft.AspNetCore.Mvc.ProblemDetails problem, string type, string detail)
{
problem.Type = TypeUri(type);
problem.Title = Titles[type];
problem.Detail = detail;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" />
<PackageReference Include="Microsoft.Extensions.Telemetry.Abstractions" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" />
</ItemGroup>
<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
Expand Down
Loading
Loading