Test your API's error responses: one format, useful messages, nothing leaked

Most API tests check the happy path, and negative tests check that bad input gets a 4xx. Fewer check what the error says. But client code reads error bodies: it shows the message to a user, highlights the right form field, retries or doesn't. If one endpoint returns {"error": "..."}, another {"message": "..."} and a third an HTML page from the proxy, every client ends up with special cases. Test errors like you test success: against a contract.

One shape for every error

RFC 9457 (formerly 7807) "problem details" is a good default:

JSON
{
  "type": "https://api.example.com/errors/validation",
  "title": "The request has invalid fields",
  "status": 422,
  "detail": "email must be a valid email address",
  "instance": "/users",
  "errors": [{ "field": "email", "code": "invalid_format" }]
}

Whatever format you choose, write it down, then enforce it. A post-response script on the collection runs for every request, so one check covers the whole API:

JavaScript
if (pm.response.code >= 400) {
  const contentType = pm.response.headers.get('Content-Type') || '';
  pm.test('error is problem+json', () => {
    pm.expect(contentType).to.include('application/problem+json');
  });

  const problem = pm.response.json();
  pm.test('error has type, title and status', () => {
    pm.expect(problem).to.have.property('type');
    pm.expect(problem).to.have.property('title');
    pm.expect(problem.status).to.equal(pm.response.code);
  });
}

status in the body must match the real status code. A 200 with {"status": 500} in the body is a bug that monitoring never sees.

Messages people can act on

WeakBetter
Bad requestquantity must be between 1 and 100
Invalid inputemail is required
Error 1042This order was already shipped and can't be changed
UnauthorizedThe access token expired at 10:42 UTC

Test the ones your clients depend on exactly. For the rest, a useful check is "the detail names the field":

JavaScript
// POST /users with { "email": "not-an-email" }
pm.test('names the field', () => {
  const problem = pm.response.json();
  pm.expect(problem.errors.map((error) => error.field)).to.include('email');
});

Codes like invalid_format or order_already_shipped should be stable: clients branch on them. Changing one is a breaking change, so it belongs in a test.

Nothing leaked

Error paths are where internals escape. One collection-level check catches most of it:

JavaScript
if (pm.response.code >= 400) {
  const body = pm.response.text();
  pm.test('no internals in the error', () => {
    pm.expect(body).to.not.match(/at [\w.$]+ \(|Traceback|Exception in thread|\.java:\d+|\.go:\d+|\.py", line/);
    pm.expect(body).to.not.match(/SELECT .* FROM|syntax error at or near|ORA-\d{5}|SQLSTATE/i);
    pm.expect(body).to.not.match(/\/home\/|\/var\/www|C:\\\\/);
  });
}

Then trigger the errors that usually leak: malformed JSON ({"email":), a wrong type ("quantity": "ten"), an enormous number, a missing Content-Type, an unknown route, a method the route doesn't support.

Errors from things in front of your API

Your API returns perfect problem details, then the gateway returns 413 Request Entity Too Large as HTML, or the load balancer returns a 502 page. Clients see those too. Send a body over the size limit and call a route during a deploy; if the format is different, either configure the gateway's error pages or make sure clients cope.