← All Exercises

Trace a Request Pipeline Until the First Failure

intermediatejavascript~30min

Background

Production backends short-circuit at the first failing middleware. Reading a pipeline as data makes the order auditable and prevents handlers from running on unauthenticated requests.

Problem

A request passes through an ordered pipeline (rate limit, auth, validation, handler). Report which stage blocked the request, which stages ran before it, and whether the handler ran at all.

Input

JSON object: { pipeline: string[], results: { [stage]: "pass" | "fail" } }

Output

JSON object: { blockedBy, ranBefore: string[], handlerRan: boolean }

Sample tests

Sample 1

Input: {"pipeline":["rate-limit","auth","validate","handler"],"results":{"rate-limit":"pass","auth":"fail","validate":"pass"}}
Expected: {"blockedBy":"auth","ranBefore":["rate-limit"],"handlerRan":false}

Sample 2

Input: {"pipeline":["auth","validate","handler"],"results":{"auth":"pass","validate":"pass"}}
Expected: {"blockedBy":null,"ranBefore":["auth","validate"],"handlerRan":true}

Your solution

Learning objective

Explain middleware ordering by predicting exactly where a request stops.