davidrs 0.1.1

A small, feature-gated framework for AWS Lambda functions: one ordered pipeline per trigger, absolute invocation deadlines, bounded work and safe built-in error responses.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
//! The buffered pipeline, driven one request at a time through
//! [`davidrs::http::Api::handle`].
//!
//! Admission, decode, policy, handler and serialization run in that order
//! under one deadline; every failure reaches the one renderer and every
//! response reaches the finalizer. The policy and renderer below are written
//! the way an application writes its own, so whatever compiles here is what a
//! downstream crate can do.
#![cfg(feature = "http")]

use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use davidrs::http::{
    Admission, Api, Body, ErrorRenderer, Failure, HeaderValue, HttpResponse, Json, PlainErrors,
    Policy, Public, Request, StatusCode,
};
use davidrs::{Context, Invocation};
use lambda_http::RequestExt;
use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize)]
struct Item {
    name: String,
}

#[derive(Debug, Serialize)]
struct Created {
    name: String,
}

/// Counts handler calls, so a test can prove the handler never ran.
#[derive(Default)]
struct App {
    calls: AtomicUsize,
}

impl App {
    fn calls(&self) -> usize {
        self.calls.load(Ordering::SeqCst)
    }
}

async fn create(app: Arc<App>, item: Item, _: Context<()>) -> Result<Json<Created>, Failure> {
    app.calls.fetch_add(1, Ordering::SeqCst);
    Ok(Json(Created { name: item.name }))
}

fn decode_item(request: &Request<'_>) -> Result<Item, Failure> {
    request.json::<Item>()
}

fn post(body: &str) -> lambda_http::Request {
    lambda_http::http::Request::builder()
        .method("POST")
        .uri("https://example.com/items")
        .body(Body::Text(body.to_owned()))
        .expect("request")
}

/// Attaches a Lambda context whose deadline is `budget_ms` from now.
fn with_budget(request: lambda_http::Request, budget_ms: u64) -> lambda_http::Request {
    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .expect("clock")
        .as_millis() as u64;
    let mut context = lambda_runtime::Context::default();
    context.request_id = "request-1".to_owned();
    context.deadline = now + budget_ms;
    request.with_lambda_context(context)
}

fn text(response: &HttpResponse) -> &str {
    match response.body() {
        Body::Text(text) => text,
        other => panic!("expected a text body, got {other:?}"),
    }
}

fn api() -> Api<Public, PlainErrors> {
    Api::new("create-item", Public, PlainErrors)
}

/// Admits every request and reports a budget, the way a rate limiter does.
struct Budget;

impl Admission for Budget {
    async fn check(
        &self,
        _: &Request<'_>,
        _: &Invocation,
    ) -> Result<Vec<(String, String)>, Failure> {
        Ok(vec![
            ("RateLimit".to_owned(), "\"default\";r=9;t=60".to_owned()),
            ("bad header".to_owned(), "dropped".to_owned()),
        ])
    }
}

/// Refuses every request with a `429`.
struct Exhausted;

impl Admission for Exhausted {
    async fn check(
        &self,
        _: &Request<'_>,
        _: &Invocation,
    ) -> Result<Vec<(String, String)>, Failure> {
        Err(
            Failure::new(StatusCode::TOO_MANY_REQUESTS, "ERROR_BUSY", "Slow down")
                .with_header("Retry-After", "60"),
        )
    }
}

/// Admits every request and sets two cookies.
struct Cookies;

impl Admission for Cookies {
    async fn check(
        &self,
        _: &Request<'_>,
        _: &Invocation,
    ) -> Result<Vec<(String, String)>, Failure> {
        Ok(vec![
            ("Set-Cookie".to_owned(), "session=a".to_owned()),
            ("Set-Cookie".to_owned(), "tenant=b".to_owned()),
        ])
    }
}

/// Never answers, so only the deadline can end the request.
struct Stuck;

impl Admission for Stuck {
    async fn check(
        &self,
        _: &Request<'_>,
        _: &Invocation,
    ) -> Result<Vec<(String, String)>, Failure> {
        std::future::pending().await
    }
}

impl Policy for Stuck {
    type Scope = ();

    async fn authorize(&self, _: &Request<'_>, _: &Invocation) -> Result<(), Failure> {
        std::future::pending().await
    }
}

/// What [`UserHeader`] establishes. Its field is private, so a handler can
/// only receive one the policy produced.
#[derive(Debug)]
struct Caller {
    user: String,
}

/// An application's own authentication: a header checked against an
/// allowlist.
struct UserHeader {
    allowed: Vec<String>,
}

impl Policy for UserHeader {
    type Scope = Caller;

    async fn authorize(&self, request: &Request<'_>, _: &Invocation) -> Result<Caller, Failure> {
        let user = request.header("x-user").unwrap_or_default();
        if user.is_empty() {
            return Err(Failure::new(
                StatusCode::UNAUTHORIZED,
                "ERROR_UNAUTHORIZED",
                "Sign in first",
            ));
        }
        if !self.allowed.iter().any(|allowed| allowed == user) {
            return Err(Failure::new(
                StatusCode::FORBIDDEN,
                "ERROR_FORBIDDEN",
                "That user may not do this",
            ));
        }
        Ok(Caller {
            user: user.to_owned(),
        })
    }
}

/// An application's own error shape, which is not the crate's default.
struct XmlErrors;

impl ErrorRenderer for XmlErrors {
    fn render(&self, failure: &Failure) -> HttpResponse {
        davidrs::http::literal(
            failure.status(),
            "application/xml",
            format!(
                "<error code=\"{}\">{}</error>",
                failure.code(),
                failure.public_message()
            ),
        )
    }
}

#[derive(Debug, Serialize)]
struct Owned {
    user: String,
    name: String,
}

async fn create_owned(
    _: Arc<App>,
    item: Item,
    context: Context<Caller>,
) -> Result<Json<Owned>, Failure> {
    Ok(Json(Owned {
        user: context.scope().user.clone(),
        name: item.name,
    }))
}

fn guarded() -> Api<UserHeader, XmlErrors> {
    Api::new(
        "create-owned-item",
        UserHeader {
            allowed: vec!["ada".to_owned()],
        },
        XmlErrors,
    )
}

fn post_as(user: Option<&str>, body: &str) -> lambda_http::Request {
    let mut request = post(body);
    if let Some(user) = user {
        request
            .headers_mut()
            .insert("x-user", HeaderValue::from_str(user).expect("header"));
    }
    request
}

#[tokio::test]
async fn a_successful_request_renders_the_handler_value() {
    let app = Arc::new(App::default());
    let response = api()
        .handle(
            Arc::clone(&app),
            post(r#"{"name":"widget"}"#),
            &decode_item,
            &create,
        )
        .await;
    assert_eq!(response.status(), StatusCode::OK);
    assert_eq!(text(&response), r#"{"name":"widget"}"#);
    assert_eq!(app.calls(), 1);
}

#[tokio::test]
async fn a_decode_failure_is_rendered_without_echoing_the_body_and_the_handler_never_runs() {
    let app = Arc::new(App::default());
    let response = api()
        .handle(
            Arc::clone(&app),
            post("\"secret-submitted-value\""),
            &decode_item,
            &create,
        )
        .await;
    assert_eq!(response.status(), StatusCode::BAD_REQUEST);
    assert!(text(&response).contains("ERROR_MALFORMED_BODY"));
    assert!(!text(&response).contains("secret-submitted-value"));
    assert_eq!(app.calls(), 0);
}

/// The decoder reads raw bytes and would accept anything; the endpoint's
/// limit still applies, because the pipeline checks it first.
#[tokio::test]
async fn a_body_over_the_endpoint_limit_is_refused_before_the_decoder_runs() {
    let app = Arc::new(App::default());
    let response = api()
        .body_limit(3)
        .handle(
            Arc::clone(&app),
            post("1234"),
            &|_: &Request<'_>| -> Result<Item, Failure> { panic!("the decoder must not run") },
            &create,
        )
        .await;
    assert_eq!(response.status(), StatusCode::PAYLOAD_TOO_LARGE);
    assert_eq!(app.calls(), 0);
}

/// The body is not JSON, so a `429` proves admission ran before decoding.
#[tokio::test]
async fn admission_runs_before_decoding_and_its_refusal_keeps_its_headers() {
    let response = api()
        .admission(Exhausted)
        .handle(
            Arc::new(App::default()),
            post("not json"),
            &decode_item,
            &create,
        )
        .await;
    assert_eq!(response.status(), StatusCode::TOO_MANY_REQUESTS);
    assert_eq!(response.headers()["retry-after"], "60");
}

#[tokio::test]
async fn admission_headers_reach_a_success_and_invalid_ones_are_dropped() {
    let response = api()
        .admission(Budget)
        .handle(
            Arc::new(App::default()),
            post(r#"{"name":"widget"}"#),
            &decode_item,
            &create,
        )
        .await;
    assert_eq!(response.status(), StatusCode::OK);
    assert_eq!(response.headers()["ratelimit"], "\"default\";r=9;t=60");
    assert_eq!(response.headers().len(), 2, "content-type and ratelimit");
}

#[tokio::test]
async fn an_admission_header_named_twice_keeps_both_values_on_a_success() {
    let response = api()
        .admission(Cookies)
        .handle(
            Arc::new(App::default()),
            post(r#"{"name":"widget"}"#),
            &decode_item,
            &create,
        )
        .await;
    assert_eq!(response.status(), StatusCode::OK);
    let cookies: Vec<_> = response.headers().get_all("set-cookie").iter().collect();
    assert_eq!(cookies, ["session=a", "tenant=b"]);
}

#[tokio::test]
async fn admission_headers_reach_a_later_failure() {
    let response = api()
        .admission(Budget)
        .handle(
            Arc::new(App::default()),
            post("not json"),
            &decode_item,
            &create,
        )
        .await;
    assert_eq!(response.status(), StatusCode::BAD_REQUEST);
    assert_eq!(response.headers()["ratelimit"], "\"default\";r=9;t=60");
}

#[tokio::test]
async fn a_serialization_failure_becomes_a_rendered_500_not_a_200() {
    struct Broken;
    impl Serialize for Broken {
        fn serialize<S: serde::Serializer>(&self, _: S) -> Result<S::Ok, S::Error> {
            Err(serde::ser::Error::custom("private upstream detail"))
        }
    }
    async fn broken(_: Arc<App>, _: Item, _: Context<()>) -> Result<Json<Broken>, Failure> {
        Ok(Json(Broken))
    }
    let response = api()
        .handle(
            Arc::new(App::default()),
            post(r#"{"name":"widget"}"#),
            &decode_item,
            &broken,
        )
        .await;
    assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
    assert_eq!(
        text(&response),
        r#"{"errorCode":"FAULT_SERIALIZATION","errorMessage":"InternalServerError"}"#
    );
}

#[tokio::test]
async fn a_slow_admission_is_cut_off_with_a_504() {
    let app = Arc::new(App::default());
    let response = tokio::time::timeout(
        Duration::from_secs(2),
        api().admission(Stuck).handle(
            Arc::clone(&app),
            with_budget(post(r#"{"name":"widget"}"#), 150),
            &decode_item,
            &create,
        ),
    )
    .await
    .expect("admission must not hang");
    assert_eq!(response.status(), StatusCode::GATEWAY_TIMEOUT);
    assert!(text(&response).contains("ERROR_TIMEOUT"));
    assert_eq!(app.calls(), 0);
}

#[tokio::test]
async fn a_slow_policy_is_cut_off_with_a_504_that_keeps_admission_headers() {
    let app = Arc::new(App::default());
    let response = tokio::time::timeout(
        Duration::from_secs(2),
        Api::new("stuck-policy", Stuck, PlainErrors)
            .admission(Budget)
            .handle(
                Arc::clone(&app),
                with_budget(post(r#"{"name":"widget"}"#), 150),
                &decode_item,
                &create,
            ),
    )
    .await
    .expect("the policy must not hang");
    assert_eq!(response.status(), StatusCode::GATEWAY_TIMEOUT);
    assert_eq!(response.headers()["ratelimit"], "\"default\";r=9;t=60");
    assert_eq!(app.calls(), 0);
}

#[tokio::test]
async fn a_slow_handler_is_cut_off_with_a_504() {
    async fn stuck(_: Arc<App>, _: Item, _: Context<()>) -> Result<Json<bool>, Failure> {
        std::future::pending().await
    }
    let response = tokio::time::timeout(
        Duration::from_secs(2),
        api().handle(
            Arc::new(App::default()),
            with_budget(post(r#"{"name":"widget"}"#), 150),
            &decode_item,
            &stuck,
        ),
    )
    .await
    .expect("the handler must not hang");
    assert_eq!(response.status(), StatusCode::GATEWAY_TIMEOUT);
}

#[tokio::test]
async fn an_expired_invocation_runs_no_step() {
    let app = Arc::new(App::default());
    let response = api()
        .handle(
            Arc::clone(&app),
            with_budget(post(r#"{"name":"widget"}"#), 0),
            &|_: &Request<'_>| -> Result<Item, Failure> { panic!("the decoder must not run") },
            &create,
        )
        .await;
    assert_eq!(response.status(), StatusCode::GATEWAY_TIMEOUT);
    assert_eq!(app.calls(), 0);
}

#[tokio::test]
async fn a_finalizer_runs_on_success_and_on_failure_alike() {
    let stamped = || {
        api().finalize(|invocation, response| {
            response
                .headers_mut()
                .insert("cache-control", HeaderValue::from_static("no-store"));
            let id = HeaderValue::from_str(&invocation.request_id).expect("header");
            response.headers_mut().insert("x-request-id", id);
        })
    };
    let success = stamped()
        .handle(
            Arc::new(App::default()),
            with_budget(post(r#"{"name":"widget"}"#), 5_000),
            &decode_item,
            &create,
        )
        .await;
    let failure = stamped()
        .handle(
            Arc::new(App::default()),
            with_budget(post("not json"), 5_000),
            &decode_item,
            &create,
        )
        .await;
    for response in [success, failure] {
        assert_eq!(response.headers()["cache-control"], "no-store");
        assert_eq!(response.headers()["x-request-id"], "request-1");
    }
}

#[derive(Debug, Serialize)]
struct Seen {
    request_id: String,
    trace_id: Option<String>,
    invoked_arn: Option<String>,
    remaining_ms: u128,
}

async fn describe(_: Arc<App>, _: (), context: Context<()>) -> Result<Json<Seen>, Failure> {
    let invocation = context.invocation();
    Ok(Json(Seen {
        request_id: invocation.request_id.clone(),
        trace_id: invocation.trace_id.clone(),
        invoked_arn: invocation.invoked_arn.clone(),
        remaining_ms: context.deadline().remaining().as_millis(),
    }))
}

fn seen(response: &HttpResponse) -> serde_json::Value {
    serde_json::from_str(text(response)).expect("JSON")
}

#[tokio::test]
async fn the_handler_sees_the_invocation_the_lambda_context_carries() {
    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .expect("clock")
        .as_millis() as u64;
    let mut context = lambda_runtime::Context::default();
    context.request_id = "request-7".to_owned();
    context.xray_trace_id = Some("Root=1-abc;Parent=def;Sampled=1".to_owned());
    context.invoked_function_arn =
        "arn:aws:lambda:us-east-1:123456789012:function:create-item".to_owned();
    context.deadline = now + 5_000;
    let request = post("").with_lambda_context(context);

    let response = api()
        .handle(
            Arc::new(App::default()),
            request,
            &|_: &Request<'_>| Ok(()),
            &describe,
        )
        .await;
    let seen = seen(&response);
    assert_eq!(seen["request_id"], "request-7");
    assert_eq!(seen["trace_id"], "Root=1-abc;Parent=def;Sampled=1");
    assert_eq!(
        seen["invoked_arn"],
        "arn:aws:lambda:us-east-1:123456789012:function:create-item"
    );
    let remaining = seen["remaining_ms"].as_u64().expect("number");
    assert!(remaining <= 5_000 && remaining > 3_000, "{remaining}");
}

/// A request built by hand has no Lambda context; the pipeline must not
/// panic, and the handler gets Lambda's maximum budget of 15 minutes.
#[tokio::test]
async fn a_request_without_a_lambda_context_gets_an_empty_id_and_the_maximum_budget() {
    let response = api()
        .handle(
            Arc::new(App::default()),
            post(""),
            &|_: &Request<'_>| Ok(()),
            &describe,
        )
        .await;
    let seen = seen(&response);
    assert_eq!(seen["request_id"], "");
    assert!(seen["trace_id"].is_null());
    assert!(seen["invoked_arn"].is_null());
    let remaining = seen["remaining_ms"].as_u64().expect("number");
    assert!(remaining > 14 * 60 * 1_000, "{remaining}");
}

#[test]
fn an_endpoint_reports_its_operation_and_configuration() {
    let api = api().body_limit(64).finalize(|_, _| {});
    assert_eq!(api.operation(), "create-item");
    let debug = format!("{api:?}");
    assert!(debug.contains("create-item"), "{debug}");
    assert!(debug.contains("body_limit: 64"), "{debug}");
    assert!(debug.contains("finalize: true"), "{debug}");
}

#[tokio::test]
async fn a_policy_scope_reaches_the_handler_as_a_checked_value() {
    let response = guarded()
        .handle(
            Arc::new(App::default()),
            post_as(Some("ada"), r#"{"name":"widget"}"#),
            &decode_item,
            &create_owned,
        )
        .await;
    assert_eq!(response.status(), StatusCode::OK);
    assert_eq!(text(&response), r#"{"user":"ada","name":"widget"}"#);
}

#[tokio::test]
async fn a_policy_refusal_is_rendered_by_the_application_renderer() {
    let unknown = guarded()
        .handle(
            Arc::new(App::default()),
            post_as(Some("grace"), r#"{"name":"widget"}"#),
            &decode_item,
            &create_owned,
        )
        .await;
    assert_eq!(unknown.status(), StatusCode::FORBIDDEN);
    assert_eq!(unknown.headers()["content-type"], "application/xml");
    assert_eq!(
        text(&unknown),
        "<error code=\"ERROR_FORBIDDEN\">That user may not do this</error>"
    );

    let anonymous = guarded()
        .handle(
            Arc::new(App::default()),
            post_as(None, r#"{"name":"widget"}"#),
            &decode_item,
            &create_owned,
        )
        .await;
    assert_eq!(anonymous.status(), StatusCode::UNAUTHORIZED);
}

#[tokio::test]
async fn a_decode_failure_is_rendered_by_the_application_renderer() {
    let response = guarded()
        .handle(
            Arc::new(App::default()),
            post_as(Some("ada"), "not json"),
            &decode_item,
            &create_owned,
        )
        .await;
    assert_eq!(response.status(), StatusCode::BAD_REQUEST);
    assert_eq!(
        text(&response),
        "<error code=\"ERROR_MALFORMED_BODY\">Invalid request body</error>"
    );
}

/// The renderer reads `public_message`, the only message accessor a
/// downstream crate has, so a 5xx cannot leak through it.
#[tokio::test]
async fn an_application_renderer_cannot_leak_a_5xx() {
    async fn unreachable_store(
        _: Arc<App>,
        _: Item,
        _: Context<Caller>,
    ) -> Result<Json<Owned>, Failure> {
        Err(Failure::new(
            StatusCode::INTERNAL_SERVER_ERROR,
            "FAULT_STORE",
            "connection refused to 10.0.4.17:5432",
        ))
    }
    let response = guarded()
        .handle(
            Arc::new(App::default()),
            post_as(Some("ada"), r#"{"name":"widget"}"#),
            &decode_item,
            &unreachable_store,
        )
        .await;
    assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
    assert_eq!(
        text(&response),
        "<error code=\"FAULT_STORE\">InternalServerError</error>"
    );
}

#[cfg(feature = "logs")]
mod logs {
    use std::io::Write;
    use std::sync::{Arc, Mutex};

    use davidrs::Context;
    use davidrs::http::{Failure, Json, Request, StatusCode};

    use super::{App, Exhausted, Item, api, decode_item, post};

    /// Collects everything the subscriber writes.
    #[derive(Clone, Default)]
    struct Captured(Arc<Mutex<Vec<u8>>>);

    impl Write for Captured {
        fn write(&mut self, bytes: &[u8]) -> std::io::Result<usize> {
            self.0.lock().expect("buffer").extend_from_slice(bytes);
            Ok(bytes.len())
        }

        fn flush(&mut self) -> std::io::Result<()> {
            Ok(())
        }
    }

    async fn store_down(_: Arc<App>, _: Item, _: Context<()>) -> Result<Json<bool>, Failure> {
        Err(Failure::new(
            StatusCode::INTERNAL_SERVER_ERROR,
            "FAULT_STORE",
            "connection refused to 10.0.4.17:5432",
        ))
    }

    /// Refuses every request with the same `429 ERROR_BUSY` the admission
    /// [`Exhausted`] returns, but defaults to `FailureKind::Handler`. Paired
    /// with [`Exhausted`] in a log capture, it proves two same-code 4xx
    /// failures from different steps stay distinguishable by `kind`.
    async fn busy_handler(_: Arc<App>, _: Item, _: Context<()>) -> Result<Json<bool>, Failure> {
        Err(Failure::new(
            StatusCode::TOO_MANY_REQUESTS,
            "ERROR_BUSY",
            "Slow down",
        ))
    }

    /// The subscriber is installed for this thread only, so other tests are
    /// unaffected.
    #[tokio::test]
    async fn a_failure_is_logged_with_its_code_but_never_its_message() {
        let captured = Captured::default();
        let writer = captured.clone();
        let subscriber = tracing_subscriber::fmt()
            .with_max_level(tracing::Level::DEBUG)
            .with_writer(move || writer.clone())
            .finish();
        let _guard = tracing::subscriber::set_default(subscriber);

        api()
            .handle(
                Arc::new(App::default()),
                post(r#"{"name":"widget"}"#),
                &decode_item,
                &store_down,
            )
            .await;
        api()
            .handle(
                Arc::new(App::default()),
                post("not json"),
                &|request: &Request<'_>| request.json::<Item>(),
                &store_down,
            )
            .await;

        let logs = String::from_utf8(captured.0.lock().expect("buffer").clone()).expect("UTF-8");
        assert!(logs.contains("request failed"), "{logs}");
        assert!(logs.contains("FAULT_STORE"), "{logs}");
        assert!(logs.contains("kind=Handler"), "{logs}");
        assert!(logs.contains("request refused"), "{logs}");
        assert!(logs.contains("ERROR_MALFORMED_BODY"), "{logs}");
        assert!(logs.contains("kind=Decode"), "{logs}");
        assert!(!logs.contains("10.0.4.17"), "{logs}");
    }

    /// Two 4xx failures that share a code and status but come from different
    /// pipeline steps must stay distinguishable in the DEBUG log: `kind`
    /// names the step that failed. Before the fix the 4xx branch logged
    /// `status` and dropped `kind`, so an admission `429 ERROR_BUSY` and a
    /// handler `429 ERROR_BUSY` rendered as byte-identical lines.
    #[tokio::test]
    async fn four_xx_log_lines_carry_kind_so_different_steps_are_distinct() {
        let captured = Captured::default();
        let writer = captured.clone();
        let subscriber = tracing_subscriber::fmt()
            .with_max_level(tracing::Level::DEBUG)
            .with_writer(move || writer.clone())
            .finish();
        let _guard = tracing::subscriber::set_default(subscriber);

        api()
            .admission(Exhausted)
            .handle(
                Arc::new(App::default()),
                post("not json"),
                &decode_item,
                &busy_handler,
            )
            .await;
        api()
            .handle(
                Arc::new(App::default()),
                post(r#"{"name":"widget"}"#),
                &decode_item,
                &busy_handler,
            )
            .await;

        let logs = String::from_utf8(captured.0.lock().expect("buffer").clone()).expect("UTF-8");
        let refused: Vec<&str> = logs
            .lines()
            .filter(|line| line.contains("request refused"))
            .collect();
        assert_eq!(refused.len(), 2, "expected two 4xx log lines, got: {logs}");

        let kinds: Vec<&str> = refused
            .iter()
            .map(|line| {
                line.split_whitespace()
                    .find(|field| field.starts_with("kind="))
                    .expect("a 4xx log line carries kind=")
            })
            .collect();
        assert!(
            kinds.contains(&"kind=Admission"),
            "admission kind missing: {logs}"
        );
        assert!(
            kinds.contains(&"kind=Handler"),
            "handler kind missing: {logs}"
        );
        assert_ne!(
            kinds[0], kinds[1],
            "two steps logged with the same kind: {logs}"
        );

        assert!(
            !logs.contains("Slow down"),
            "message leaked into the log: {logs}"
        );
    }
}