Error format and service response headers
The API error format is part of the contract. The standard is Problem Details (RFC 9457, application/problem+json), but a custom shape works if it is uniform for all codes. The client relies on a stable machine code, not the text, and the shape never exposes a stack trace or SQL. To find a request in logs, the response and bug report carry a request id such as X-Request-Id or traceparent.
API · Request anatomy
The client got a 500 and the bug report says only "does not work". How do you find that request in the logs?
The frontend shows "Something went wrong". The bug report has a screenshot and a time. The developer asks: "Which request?". The service logs hold thousands of similar lines for that minute. Without a request identifier the search takes an hour, with it a few seconds. That is why the error format and the service response headers are as much a part of the contract as a successful body: a client parses them in code, and a person reads them during an investigation.
How an error is built
An error should be as structured a response as a success, otherwise the client cannot handle it. The standard option is Problem Details, RFC 9457 (the successor of RFC 7807), with the media type application/problem+json:
HTTP/1.1 422 Unprocessable Content
Content-Type: application/problem+json
X-Request-Id: 5b0e7c2a
{
"type": "https://api.shop.test/errors/validation",
"title": "Validation failed",
"status": 422,
"detail": "amount must be greater than 0",
"instance": "/v1/accounts/42/transfers",
"errors": [{ "field": "amount", "message": "must be greater than 0" }]
}
The fields type (a URI identifying the kind of error), title (a short name), status, detail (an explanation for this occurrence) and instance are standard, the rest are extensions (here errors with a list of fields). A custom shape (code, message, details) is fine too, as long as it is the same everywhere.
Why it is like this. A client that gets an error has to do one of three things: show the user a message, highlight a field or retry. That needs a stable machine key (type/code), a description for a person and a pointer to the field. The message text gets changed and localised, so the client must not depend on it. That is why the error code is stable and the text is not.
One id through all the layers
For such an id, X-Request-Id (an informal but widespread convention) or the standard traceparent header from W3C Trace Context, supported by tracing systems, is used. Date (the server clock) and Content-Length (matching the actual body) are useful too.
Example: HTML instead of JSON and a leak in the error
When the backend fails, a gateway may return its own error page instead of an API response:
HTTP/1.1 502 Bad Gateway
Content-Type: text/html
<html><head><title>502 Bad Gateway</title></head>...
An error the client cannot parse and an error that follows the contract
HTML or a stack in the body
text/html, an exception name, SQL, a file path. The client cannot parse the response, and an attacker learns the internals.
Problem Details + request id
application/problem+json, stable fields, the validation field, an id for the logs and no internal details.
To avoid checking this by hand in every test, we move the error check into one function:
async function expectProblem(res: APIResponse, status: number) {
expect(res.status()).toBe(status);
expect(res.headers()['content-type']).toContain('application/problem+json');
const body = await res.json();
expect(body.status).toBe(status);
expect(typeof body.title).toBe('string');
expect(JSON.stringify(body)).not.toMatch(/\bat \w+\.|SELECT |Exception/);
expect(res.headers()['x-request-id']).toBeTruthy();
return body;
}
test('validation error has a stable shape', async ({ request }) => {
const res = await request.post('/v1/accounts/42/transfers', {
data: { toAccount: 'UA21...', amount: -1, currency: 'UAH' },
});
const body = await expectProblem(res, 422);
expect(body.errors[0].field).toBe('amount');
});
The function checks the status, the media type, that status in the body matches the HTTP status, that title exists, that there are no traces of a stack or SQL, and the request id header. The regex for "traces" is rough (it looks for common words), so extend it for the project's stack.
What to check
- The same structure for 400, 401, 403, 404, 409, 422 and even 500. A common defect: an HTML page from a gateway or framework instead of JSON.
Content-Typematches the body, andstatusin the body matches the HTTP status.- Validation errors name the field (
field), not just "bad request". - The body does not reveal internals: call stack, SQL, file paths, library versions.
- The login message is the same for "no such user" and "wrong password", so as not to reveal whether an email exists.
- The error code is stable for the client, while the text can be changed or localised.
Limits and alternatives
- Problem Details is not the only format: a project may have its own shape, and GraphQL often returns errors in an
errorsfield with status 200 (the GraphQL over HTTP specification recommends 4xx for errors in the request itself). The requirement stays the same: the shape is uniform and documented. - If a gateway or a load balancer fails, the response may not come from the API, so the format of gateway errors (502, 503, 504) is agreed separately.
- A request id does not guarantee finding the request if the logs are not indexed by it. That is an observability question, to be discussed with developers.
- For client errors (4xx) a detailed message helps, while for 5xx the details are better left in the logs and the client gets only the id.
Common mistakes
- Checking only the error status. A body with HTML or a stack goes unnoticed.
- Attaching only a screenshot to a bug report. Without a request id and the time it is hard for a developer to find the request.
- Distinguishing "no such user" and "wrong password" in the message. That allows email enumeration.
- Tying the client to the error text. After a rewording or a translation everything breaks, so the client relies on the code.
Check yourself
On a 500 the service returns a gateway HTML page, and the status is right. Is it a defect?
Answer
Yes, if the contract promises JSON for all errors: a client that calls res.json() will fail on parsing. The test should check Content-Type and the body structure even for 5xx, not only the status.
What should you add to a bug report about an API error so a developer finds the request in the logs within a minute?
Answer
The method and URL, headers without secrets, the request body, the response status and body, X-Request-Id (or traceparent) and the exact time with a time zone. The request id matters most: it finds the request in the logs directly.
Content-Type, that status in the body matches the status, the validation field, the same login response for both cases and a request id even in 5xx.