The most popular traps
The main trap is a dishonest status, such as 200 with an error in the body, since clients, caches, retries and monitoring read only the status. Others: 500 on malformed input, 404 on an empty list instead of an empty array, 201 without Location or an id, 204 with a body, different responses for another's and a non-existent resource. GraphQL is the exception: field errors arrive in errors with a 2xx status.
API · Status codes
The response is 200 and the body says "Insufficient funds". Why is the monitoring dashboard green?
A payment failed, the response is { "error": "Insufficient funds" }, but the status is 200. The status test is green, the error dashboard is empty, and clients complain. This "error in a success wrapper" is one of the most popular traps. There are others, and each has a cause and a consequence for the client. Below: the traps and a way to catch them with tests.
Why the status must be honest
The status is read by everything that does not understand the body: HTTP clients (what counts as success, what to retry), proxies and CDNs (what to cache), monitoring (counts 5xx and 4xx), retry logic. Only your code parses the body. If the status says "success", all of these nodes believe it and act accordingly: a cache stores the response with the error, a retry does not fire, an alert does not come.
HTTP/1.1 200 OK
Content-Type: application/json
{ "error": "Insufficient funds", "code": 1042 }
An error in the body with status 200, and an honest response
200 and error in the body
The client, the cache and monitoring see success. Only whoever parses the body notices the error.
4xx or 5xx with the error in the body
The status signals every node, the body explains the details. The dashboard and retries work.
Traps and how to catch them
- 200 with an error in the body. Clients, monitoring and autotests consider the request successful. An exception: GraphQL, where by convention execution errors go into the
errorsarray together with status 200. There you check the body, and that is normal. - 500 on malformed input. The server did not handle validation and failed with an exception. Such a response often leaks a stack trace, and the same input may turn out to be an injection.
- 404 for an empty list.
/orders?status=archivedwith no results is 200 and an empty array: the collection exists, there are no items. 404 says the address itself does not exist. - 201 without
Locationor an id. The client does not know where the created resource is. - 204 with a body. Per RFC 9110 a 204 has no content, a client ignores the body or gets a length mismatch.
- 404 or 403 for someone else's resource. Both are acceptable: 404 does not reveal that the id exists. A defect is different behaviour for "someone else's" and "non-existent", which lets ids be enumerated.
- A stack trace, SQL or paths in the error body. A leak of internals even when the code is right.
The first trap is caught by a helper that can be attached to every positive case:
import { expect, type APIResponse } from '@playwright/test';
export async function expectNoHiddenError(res: APIResponse) {
if (!res.ok()) return; // 4xx and 5xx are honest about it
const body = await res.json().catch(() => null);
expect(body?.error, 'error inside a 2xx body').toBeUndefined();
expect(body?.errors ?? [], 'errors inside a 2xx body').toHaveLength(0);
}
It lets 4xx and 5xx through (they are honest), and in a 2xx it looks for error and errors. Add it to the base of your tests and you learn about all "successes with an error" without a separate case for each. For a 500 on malformed input a table of "garbage" values fits:
const garbage = ['', 'abc', '-1', '9'.repeat(40), "' OR 1=1 --", '%00'];
for (const value of garbage) {
test('no 5xx for page=' + JSON.stringify(value), async ({ request }) => {
const res = await request.get('/v1/orders', { params: { page: value } });
expect(res.status()).toBeLessThan(500);
});
}
The test does not check the exact code (the contract knows it), it asserts the main thing: a client error must not turn into a 5xx. The empty-list and 204 traps are checked directly:
test('empty list is 200, not 404', async ({ request }) => {
const res = await request.get('/v1/orders', { params: { status: 'archived' } });
expect(res.status()).toBe(200);
expect((await res.json()).items).toEqual([]);
});
test('204 has no body', async ({ request }) => {
const res = await request.delete('/v1/orders/17');
expect(res.status()).toBe(204);
expect(await res.text()).toBe('');
});
Someone else's and a non-existent resource
403 for someone else's
Honestly says "not allowed", but reveals that the resource exists. If ids are predictable, it is a hint for enumeration.
404 for someone else's
Does not reveal existence. The point is that "someone else's" and "non-existent" answer the same way.
Limits and alternatives
- Some teams deliberately return 200 with an application error code (old SOAP-like integrations, some payment gateways). For QA it is a risk that is raised, but the decision is the contract's.
- The helper must be adapted to the project's error format (
error,errors,code) and exclude fields that legitimately contain these words. - For batch operations (an array of items, some with errors) partial success is expressed by 207 or by 200 with a status per item. This too must be described in the contract.
- A no-5xx test with garbage values is not a replacement for fuzzing: it only guarantees that known kinds of input do not break the server.
Common mistakes
- Checking only the status and not looking at the body. A 2xx with an error in the body passes.
- Treating "an error was returned" as enough. A 500 with a message is still an unhandled situation.
- Accepting 404 for an empty collection. A client cannot tell "no items" from "no such address".
- Checking 403 and 404 only for someone else's resource. They forget to compare with a non-existent id: a different response reveals that the resource exists.
Check yourself
A payment endpoint returns 200 and {"status":"failed","reason":"insufficient_funds"}. Is it a bug?
Answer
It depends on the contract. If status is the state of a payment (a payment was created and its status is "failed"), 200 or 201 is acceptable. If it is a refusal of the operation, a 4xx is more honest (for example 422 or 409), because clients and monitoring go by the code. It is a question for the contract, raised as a remark when the code and the body disagree.
A GraphQL request returned 200 and errors in the body. Should a defect be filed?
Answer
No, by convention GraphQL returns 200 when the request was parsed and executed (with partial errors in errors). There you check the errors array. A request that fails to parse or validate gets a 4xx under the GraphQL over HTTP spec (application/graphql-response+json).
Location, a 204 has no body. Middle/Senior: The status is read by caches, proxies, retries and monitoring, which do not parse the body, so a 200 with an error hides a failure. I attach a check that a 2xx has no error to the positive tests and run garbage values separately so that a client error never becomes a 5xx. GraphQL is the exception, with execution errors in errors under status 200 (a request that fails to parse or validate gets a 4xx under GraphQL over HTTP with graphql-response+json). For someone else's resource I accept 404 or 403 per the contract, but require the same response for "someone else's" and "non-existent".