Case File: The Route Table
A new parcel service must decide, for every request, what the server says back. Write the handler that finds the route, refuses a wrong method with a list of the right ones, tells a stranger from a person who is not allowed, and answers success with the right code. Check it on request lists you cannot see. The routes and tokens are generated for you alone.
Sabi Logistics · Backend 1
The Route Table
The mobile app shows Something went wrong for every error, because our server answers 200 or 500 and nothing in between
Chidi Engineering manager ·
Chidi. The new mobile app has one error message: “Something went wrong”. Our server only ever says 200 (fine) or 500 (broken), so the app cannot tell a wrong address from a missing login from a bug, and support is flooded.
I want a proper handler. Given a request (a method, a path and maybe a token), it must say which thing is wrong, with the right HTTP status, in a fixed order, so that the app can show the right message and so that we can test it. Here are the routes and the tokens. Please write the handler and run it on the requests.
The brief
What you need to know first.
- A route is a method plus a path pattern.
/parcels/{id}matches/parcels/42(digits only) but not/parcels/abc. A trailing slash makes a different path, so/parcels/42/is not found. Split the path after the first slash and compare the parts one by one. - The order of the checks is the whole lesson: first the path (no route: 404), then the method (a route exists but not for this method: 405, with the allowed methods in alphabetical order), then who you are (no token or an unknown token: 401), then what you may do (a known token whose role is too low: 403), and only then success (200, 201, 204).
- 401 and 403 are different. 401 means “I do not know who you are”: log in. 403 means “I know who you are, and you may not”: asking again will not help. Mixing them up sends users to the login page for a problem that a login cannot fix.
- Roles. A dict of levels (
useris 1,adminis 2) lets you compare roles with a number: a token is good enough when its level is at least the level that the method needs. - Write
match(path),handle(req)andhandle_all(reqs); fill inresultsandby_status(the count of each status, with the status written as text). The checks runhandle_allon lists you cannot see.
The lab gives you the data for this case, invented and generated for you alone. Today is 2026-11-09.
Preparing your data…
On a real system
The lab modelled the rules with a script. These are the commands that you would type on the real thing.
| You want to | Type | What you get |
|---|---|---|
| see the status line and headers of a response | curl -i -X DELETE http://localhost:8000/parcels/7 | the status (401, 403, 404, 405 and so on) and headers such as Allow |
| print only the status code | curl -s -o /dev/null -w "%{http_code}\n" http://localhost:8000/health | just the number |
| send a token with a request | curl -H "Authorization: Bearer t-abc123" http://localhost:8000/parcels | the answer for a logged-in caller |
Only run commands on systems that you own or have written permission to test.
Chidi: “The app now shows the right message for each case. A security reviewer asks something odd: should a request to a route that exists but needs a login say 401, or pretend that it does not exist and say 404?”
🔒 The debrief unlocks when you close the case. Finish the lab above.
The debrief
Chidi: “So the answer depends on what the route reveals.”
What a senior backend developer would do next. Decide what must not leak: for private data, many services answer 404 to a user who may not see it, so that nobody can find out which ids exist; for a login page 401 is right. Send an Allow header with every 405, and a WWW-Authenticate header with a 401. Put the status table in one place (a router or a framework), not in each handler. Return a consistent error body (a code and a message, never a stack trace) so that the app can show it. And write tests for each status, because the day that someone reorders the checks, a missing login will become a 403, or worse, a 200.
The trap. Checking the permission before checking that the route exists, or answering 200 with an error in the body. Clients, caches and monitoring tools all trust the status code.
Write the handler note
Write the note for the engineering manager: the order of the checks and why (route, method, token, role, success), what each status means and the difference between 401 and 403, what the 405 answer must include, which statuses your handler gave in the sample and how often, what an error body should contain, and what you would test so that the order cannot change by accident.
🧪 Want to practise this on your own computer? See how to set up your lab →