about summary refs log tree commit diff
path: root/src/error.rs
diff options
context:
space:
mode:
authorPjotr <wrk@bosa.lan>2026-10-08 11:24:58 +0000
committerPjotr <wrk@bosa.lan>2026-10-08 11:55:30 +0000
commit22540223c43342473d7d428dffeeab9d94afb55c (patch)
treef81c6af04ea9157054a58d3fc20e99f55973a0ae /src/error.rs
parent5650076cdf45a26aed8c6166069d74281a7f4bd7 (diff)
downloadgenenetwork4-22540223c43342473d7d428dffeeab9d94afb55c.tar.gz
* src/response.rs: wrap every success response in one envelope.
Breaking: every success body is now {"data", "meta": {"state",
"took_ms", "request_id"}, "notes", "next"}; clients read `data`. Only
`/` (plain text) and `/openapi.json` are unwrapped.

* src/response.rs (Envelope, Meta, State, Ctx, RequestInfo): handlers
take Ctx first and build the response with ctx.envelope(data), so
timing and the request id are never filled in by hand. `state` is
`complete` or `partial`; notes are server-authored only; `next` are
concrete relative hrefs (path_segment percent-encodes names).
* src/routes/*.rs, src/lib.rs (version): every handler returns
Envelope<..> with next links; /search is `partial` when the locus
lookup fails or a sorted 1000-hit window may be truncated, and
/publications/{pubmed_id}/traits when datasets were left out for
authorization.
* src/error.rs (ApiError): add `took_ms`, an RFC 7807 extension member.
* src/auth.rs (AuthToken): the 401 now carries request_id and took_ms
(it never had a request_id); Ctx stores RequestInfo in the request
extensions for it.

* tests: common::data / envelope_data parse every success body through
the envelope; tests/envelope.rs and test/python/test_envelope.py check
the shape, errors, and that every `next` link resolves;
tests/envelope_partial.rs checks the `partial` search without a
database; tests/openapi.rs fails if a JSON success response is not an
Envelope_* schema.
* README.md, docs/SKILL.md, docs/gn-auth.md, RELEASE_NOTES.md: document
the envelope and took_ms on errors.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Diffstat (limited to 'src/error.rs')
-rw-r--r--src/error.rs18
1 files changed, 14 insertions, 4 deletions
diff --git a/src/error.rs b/src/error.rs
index 8d85e83..3f9479a 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -2,8 +2,11 @@
 //! (Problem Details for HTTP APIs) plus a few extra fields: a machine
 //! readable `code`, the OpenAPI `operation_id` that failed, a `spec_url`
 //! pointing back at /openapi.json, the request's `request_id` (see the
-//! SetRequestIdLayer/PropagateRequestIdLayer in lib.rs), and `next`, a
-//! hint at a related endpoint the caller might try instead.
+//! SetRequestIdLayer/PropagateRequestIdLayer in lib.rs), `took_ms`
+//! (server time spent before the error, the same measure as a success
+//! response's `meta.took_ms`), and `next`, a hint at a related endpoint
+//! the caller might try instead. RFC 7807 allows extension members
+//! like these next to the standard ones.
 //!
 //! `type`/`spec_url` are relative paths, not absolute URLs, since this
 //! service has no fixed public domain; RFC 7807 explicitly allows `type`
@@ -14,6 +17,8 @@ use axum::http::StatusCode;
 use axum::response::{IntoResponse, Response};
 use serde::Serialize;
 
+use crate::response::RequestInfo;
+
 #[derive(Debug, Serialize, utoipa::ToSchema)]
 pub struct ApiError {
     pub r#type: String,
@@ -30,6 +35,8 @@ pub struct ApiError {
     pub operation_id: String,
     pub spec_url: String,
     pub request_id: Option<String>,
+    /// Milliseconds the server spent on the request before this error.
+    pub took_ms: Option<u64>,
     pub next: Option<String>,
 }
 
@@ -70,6 +77,7 @@ impl ApiError {
             operation_id: operation_id.to_string(),
             spec_url: "/openapi.json".to_string(),
             request_id: None,
+            took_ms: None,
             next: None,
         }
     }
@@ -92,8 +100,10 @@ impl ApiError {
         self
     }
 
-    pub fn with_request_id(mut self, request_id: Option<String>) -> Self {
-        self.request_id = request_id;
+    /// Stamps the error with the request's id and the time taken so far.
+    pub fn with_request_id(mut self, request: RequestInfo) -> Self {
+        self.request_id = request.id();
+        self.took_ms = Some(request.took_ms());
         self
     }