Last month I was wiring up a client against three internal APIs. One answered errors with {"error": "..."}, the second with {"code": 42, "msg": "..."}, and the third occasionally handed me an HTML page from a proxy. Three parsers for one job. That is exactly the kind of waste a standard should remove, and for HTTP error responses we have one: RFC 9457, "Problem Details for HTTP APIs".
In the full article on RFC 9457 with Lambda I build a complete Serverless bike rental API around it. Here is the short version: what the standard gives you, how I apply it in a Lambda handler, and where AWS itself spoils the fun.
A shared error shape instead of in-house JSON
RFC 9457 was published in July 2023 and supersedes RFC 7807 without changing the format. If you already speak 7807, you are essentially done. A problem details response uses the content type application/problem+json and carries up to five standard members:
-
type– a URI that identifies the kind of problem; clients branch on it -
title– a short, stable summary of that type -
status– the HTTP status code, which must match the real one -
detail– a human-readable explanation of this specific occurrence -
instance– an identifier for this one occurrence, handy for support
On top of that, each problem type may define its own extension members. For a bike that is already booked, I add freeFrom so a client knows when to try again. This is the part I find most valuable: anything a program needs to evaluate goes into its own field. detail is prose for humans, and the moment a client starts parsing it with a regex, every rewording becomes a breaking change.
Design the types before the code
A problem type is a small contract: a stable URI, a title, a status code and guidance for the client. A few rules I follow:
- Use a URI on a domain you control, not the generated API Gateway URL, because that one dies with the API instance.
- Use
about:blankwhen the status code already says everything, such as an unknown route or an internal failure. - Create a new type only when the client should react differently. "Bike unavailable" and "reservation limit reached" are two reactions, so they are two types. A generic "conflict" type for every 409 adds nothing.
- Never put stack traces, table names or SQL errors into the response. The
instancemember links the response to the logs, and that is enough for troubleshooting.
The Lambda side in a nutshell
In TypeScript I keep a catalog of problem types keyed by error kind, and satisfies makes the compiler complain if a kind is missing. One function turns a domain error into the RFC object, and one helper builds the HTTP response from it so the status can never drift:
const problemResponse = (p: Problem) => ({
statusCode: p.status,
headers: { 'content-type': 'application/problem+json' },
body: JSON.stringify(p),
});
// in the handler
const instance = `urn:uuid:${context.awsRequestId}`;
return problemResponse({ ...toProblem(error), instance });
Using the Lambda request ID as instance is a nice side effect: the same ID shows up in every CloudWatch log line of that invocation. For a synchronous HTTP API, I also catch unexpected exceptions at the top level and answer with a clean 500, while asynchronous triggers should still let exceptions through so retries work.
Where API Gateway gets in the way
Your handler can be perfect and clients will still see other formats. With HTTP APIs, responses that API Gateway generates itself – 401 from the JWT authorizer, 429 throttling, 404 for unknown routes – cannot be customized. A $default route fixes the 404 case; for the rest, document the exception and let clients decide by content type. REST APIs let you rewrite gateway responses, at a higher price.
My take: for new APIs I would not choose anything else. For existing APIs with many consumers, treat the switch as a breaking change and roll it out with a new version or via content negotiation.
What the full article covers
- The complete TypeScript project: Result type, error catalog, handler and a docs page per problem type
- Tests with the Node.js test runner that check status, content type and leak-free 500s
- Deployment with Serverless Framework v4 and a client that reads problem details safely
- An OpenAPI schema for problem details and the REST API gateway response templates
- Common pitfalls and an FAQ on RFC 7807 vs. RFC 9457
Read it here: Problem Details for HTTP APIs: RFC 9457 with Lambda
📖 This is the condensed version. The full article on sascha-ms.de goes deeper – with all code samples, diagrams and a short knowledge check.
👉 Next step: Curious what your own Lambda functions cost? Try my Lambda cost calculator – it runs right in your browser.
📬 Stay updated: New articles on serverless, AI and leading tech teams via RSS, browser push or the weekly newsletter (in German).
Originally published on sascha-ms.de. More articles on serverless, AI in everyday development and leading tech teams: sascha-ms.de/en
Top comments (0)