The failure
With no validation at the edge, every piece of code has to remember to check the signed-in user, access to the data and the input itself. Some forget. Others add a null check deep in the code that stops the bad value but turns it into a 500. Values that should never change slip through and get saved.
The simulator
Send the long title, then switch to After. Try the other examples, and break the JSON yourself.
SimulatorUpdate an item, then send it
500 Internal Server Error expected 422 Unprocessable Contentaccidental 500
Stopped at the repository.
- Session checknot in this design
- Entity loadernot in this design
- Body validationnot in this design
- ControllerpassedRuns the checks this endpoint remembered, then calls the code below.
- Handlernot in this design
- Step: save the itempassedSaves the item and commits at once. Null checks on the user, the item and the title.
- Repositorystopped hereThe database columns limit the title and note length.The database column rejected the value on the first write.
- Step: update your item listnot reachedUpdates the summary of your items; the title must be unique among them.
- Exception mappernone: uncaught exceptions become 500
- Guard checks on the path
- 8
- Guard checks below the controller
- 4
- Writes committed
- 0 of 2
- Partial write
- no
500 Internal Server Error
{"error":"Internal Server Error"}| Design | Stopped at | Status | Expected | Guard checks | Below the controller | Writes committed | Partial write |
|---|---|---|---|---|---|---|---|
| Before (current) | Repository | 500 | 422 | 8 | 4 | 0 of 2 | no |
| After | Body validation | 422 | 422 | 8 | 0 | 0 of 2 | no |
| Example | Before: status | After: status | Partial write before | Partial write after |
|---|---|---|---|---|
| A valid update | 200 | 200 | no | no |
| Blank title | 422 | 422 | no | no |
| Title too long | 500 (expected 422) | 422 | no | no |
| Broken JSON | 400 | 400 | no | no |
| Not signed in | 401 | 401 | no | no |
| Someone else’s item | 500 (expected 403) | 403 | no | no |
| Item not found | 404 | 404 | no | no |
| Change a field that must not change | 200 (expected 422) | 422 | no | no |
| Business rule fails mid-write | 500 (expected 409) | 409 | yes | no |
Model with illustrative checks, statuses and counts, not code from the real system.
What’s happening
Every mechanism here is general. The model is not the real system’s code.
Scattered checks. Whether a request gets the right error depends on what each endpoint remembered to check. Here the controller checked the JSON, the session, that the item exists and that the title is not blank, but not the length. The long title passes the controller and the code below it, and the database column rejects it on the first write. A null check or a column limit deep in the code does stop the bad value, but it cannot say what was wrong, so the client gets a 500 instead of a 422.
A partial write, even when every check is there. Tick all the checks and pick the last example. The item is saved and committed straight away, and only then does the next step find that the title is already used by another of your items. The request fails with 1 of 2 writes committed. More checks in the controller do not fix this: it comes from where the commit happens, not from what was checked. This is a general mechanism of the structure, not something reported from the real system.
The fix. Listeners run before the controller. They load the signed-in user and the entity, and validate the body. The controller only calls one plain handler for the action. The handler holds business rules and nothing else, because its input is already valid, so the same handler can be called from a controller, an API endpoint or the command line. A top-level handler owns the transaction; the steps under it build their changes without committing, and it commits once at the end. A failed step rolls everything back. Exceptions are mapped to statuses in one place, so no controller deals with them.
Where validation stops and business rules start. “The title is at most 80 characters” can be checked by looking at the request alone, so it is checked at the edge. “The title is unique among your items” needs the user’s other items, so it is a business rule in the handler.
The cost. More classes, but less boilerplate. The edge layer has to be built once, up front; after that it runs on every request without anyone remembering it.
Where I hit this in production
The input came from users of a front-end web app.
It was less a single bug than a way of writing code. Without validation at the edge, each piece of code had to check everything itself: the connected user, access to the data, and the data it received, such as its length or whether blank was allowed. That meant defensive null checks in many places.
Some endpoints didn’t check properly. Lower layers still blocked the actual problem, so the request ended in a 500 instead of the error it should have returned. Some endpoints also didn’t fully block changes to values that should not have been changeable.
The design put Symfony listeners at a very high level, before the controller. They load the user from the session and the entity, check that the user may access it, and validate the JSON body with Symfony Validator constraints. There is no command bus: each action has one plain handler class. Handlers contain no input validation, only business rules. Transactions live in the handlers: a high-level orchestrator handler always decides the transaction, and the smaller handlers or services under it build the data without committing until the end, so there are no partial writes. Exceptions are mapped centrally and generically.
Adoption was gradual: controllers opted in one at a time, so feature work went on while the structure was rolled out.
The outcome: far fewer errors and faster work on many endpoints. The code became easy to test. Onboarding a new team member in that part of the app took about a week, against about a month in the legacy parts, with their long controllers, lots of logic and no clear place for things.
Why it matters more with AI
The structure made it easy to write architecture tests for the harness. They actually run, and the AI cannot get around them. It also gives agents a clear place for each kind of check.