Problem statement
Complete the implementation of CatalogExceptionHandler and ExceptionHandlingRegistration so that every failure this application raises is answered with a problem document that carries the right status, reports what the failure knows, and says nothing about how the application is built.
Sample Data
The catalogue is pre-seeded with three items, held in memory, with the ids AB-1001, CD-2040 and EF-3115. Here is an example of a catalogue item JSON object:
{
"itemId": "CD-2040",
"name": "Bearing sleeve",
"unitPrice": 12.75,
"stock": 84
}
Two failure types ship with the repository, and each carries the one value its answer has to report:
- CatalogItemNotFoundException carries ItemId, the id that was asked for. It is raised wherever an item is looked up, checked or removed under an id the catalogue does not hold.
- CatalogValidationException carries Member, the name of the field that was rejected. It is raised while a submitted item is being validated.
Every failure of any other type is an unexpected one, including types that appear nowhere in the application's own code.
Service
The Answer
A failure is answered with a problem document, served as application/problem+json, carrying:
- status, the same number as the HTTP status of the answer.
- title, a short name for what went wrong.
- detail, one sentence about this particular failure.
- instance, the path of the request that failed, such as /catalog/items/ZX-4400.
- one further member naming the value the failure carries, where it carries one.
Which of those values each failure produces:
- CatalogItemNotFoundException is answered 404, with a title of Catalog item not found, the failure's own message as detail, and its ItemId under a member named itemId. There is no member.
- CatalogValidationException is answered 400, with a title of Catalog item is not valid, the failure's own message as detail, and its Member under a member named member. There is no itemId.
- Every other failure is answered 500, with a title of Unexpected server error and a detail of An unexpected error occurred while handling the request., and carries neither itemId nor member. That sentence is the whole of what an unexpected failure reports, the same for every one of them: the failure's own message never reaches the caller.
No answer says anything about how the application is built. No failure type name, no namespace, no file name, no line number and no stack frame reaches the body of any of them.
CatalogExceptionHandler
Implement TryHandleAsync so that it answers the failure it is handed itself, building that answer out of the failure and the path of the request that raised it, writing it to the response under the status code and the content type above, and reporting that failure answered, so that nothing behind it answers again. CatalogExceptionHandler takes no constructor arguments, so anything further it needs it takes from the request it is handed.
ExceptionHandlingRegistration
Program calls AddCatalogExceptionHandling while the application is being built, and UseCatalogExceptionHandling at the front of the request pipeline. Implement both so that:
- AddCatalogExceptionHandling registers CatalogExceptionHandler as the application's one failure mapping, together with the problem details service that mapping needs.
- CatalogExceptionHandler answers every failure this application raises, wherever it is raised: inside an endpoint, and in any stage of the request pipeline behind the one you register. Endpoints this document does not describe are covered on the same terms as the ones it does.
- A request that does not fail is left alone. Its status, its body and its content type stay the ones its endpoint produced, and a failure on one request changes nothing about the next.
Sample Requests & Responses
GET /catalog/items/ZX-4400
No item is held under that id, so the lookup fails and the answer is:
Response code: 404
{
"title": "Catalog item not found",
"status": 404,
"detail": "Catalog item ZX-4400 is not in the catalog.",
"instance": "/catalog/items/ZX-4400",
"itemId": "ZX-4400"
}
An answer may carry further members of its own, such as a type or a trace id, and nothing here depends on them.
The same failure raised anywhere else is answered the same way, always reporting the id exactly as it was asked for:
- DELETE /catalog/items/QF-9012 answers 404 with an itemId of QF-9012 and an instance of /catalog/items/QF-9012.
- GET /catalog/items/zz-0001 answers 404 with an itemId of zz-0001, in that spelling.
POST /catalog/items with {"itemId": "GH-4400", "name": "", "unitPrice": 1.15, "stock": 40}
The name is blank, so validation fails and the answer is 400, with a detail of Item name must not be blank., a member of name and an instance of /catalog/items. A submission rejected for any other reason answers the same shape:
- A unitPrice of 0 or less answers a detail of Unit price must be greater than zero. under a member of unitPrice.
- An itemId that is not two capital letters, a dash and four digits, such as gh-44, answers a detail of Item id must be two capital letters, a dash and four digits. under a member of itemId.
GET /catalog/items/AB-1001/quote
The price feed refuses to answer, which nothing here expects, so the answer is 500 with a title of Unexpected server error, the one fixed detail, and an instance of /catalog/items/AB-1001/quote. Nothing the feed said about itself appears in it. GET /catalog/ledger/2026-Q1 fails in its own way further inside the application and is answered identically, under an instance of /catalog/ledger/2026-Q1.
A request that does not fail is answered by its own endpoint, untouched, before and after any of the failures above.
