Repository navigation
Publish an OpenAPI document for the docs API and answer errors as RFC 9457 problems #4896
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c2f1783
Publish an OpenAPI document for the docs API and use one error body
akira28 c3e1947
Name every docs API operation in the OpenAPI document
akira28 11d1b07
Answer every docs API error as an RFC 9457 problem
akira28 638f314
Quote a current error in the problem example
akira28 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
### Errorsand the new## Errorsfurther 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.