Quick revision
The short answer
For a public API I would use problem details (RFC 9457) and include a stable machine-readable error code with a type URI that links to documentation, a human-readable title and detail, a trace or correlation id for support, and a list of field-level errors for validation failures. I would never include stack traces, SQL, internal class names, hostnames or any secret, because clients cannot act on them and attackers can.
What to remember
In this answer, we designed a public API error format on top of RFC 9457 problem details. It has a stable code with a documented type URI for client logic, a title and detail written for people, a traceId so support can find our logs, and a field-level errors list for validation, plus retry hints where useful. It never contains stack traces, SQL, internal names, hostnames or secrets, which stay in access-controlled logs under the same trace id.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
For a public API I would use problem details (RFC 9457) and include a stable machine-readable error code with a type URI that links to documentation, a human-readable title and detail, a trace or correlation id for support, and a list of field-level errors for validation failures. I would never include stack traces, SQL, internal class names, hostnames or any secret, because clients cannot act on them and attackers can.
Why this question is asked
A public API's error format is a contract with people we will never meet. Once partners build their integrations on it, every field we publish must keep its meaning for years, and every detail we leak is visible to anyone on the internet. So the format deserves the same design care as the success responses. Interviewers ask this design question to see whether we can choose fields by what clients need and what is safe, rather than by what is easy to dump into a response. In this answer, we will start from what a client needs to do with an error, then design each field, show a full example, and finish with what must never appear.
1What a Client Needs to Do With an Error
An error response serves three readers. Client code needs to decide what to do next: retry, ask the user to fix a field, refresh a token or give up. The developer building the integration needs to understand what went wrong and where the documentation is. And when something is really broken, the partner's support team needs to give us something we can look up.
Each field in the format should serve one of these readers. That rule keeps the format small and stops it growing into a dump of whatever the server knew at the time of the failure. We build on problem details, the RFC 9457 standard sent as application/problem+json, because many clients and tools already understand its five standard members and it allows extensions for the rest.
| Field | Reader | Purpose |
|---|---|---|
| status | client code | the class of error, same as the HTTP status |
| code | client code | stable identifier to branch on, e.g. card_declined |
| type | developer | URI of the documentation page for this code |
| title, detail | developer, end user | short summary and this occurrence's explanation |
| errors[] | client code, end user | which fields failed and why |
traceId | support | find the full story in our logs |
| retryable, Retry-After | client code | whether and when to try again |
2A Stable Code and a Type URI
The most important field is the one clients write logic against, so it must never change meaning. A short code such as card_declined or order_not_found is easy to match in any language, and it is independent of the wording of messages, which we want to be free to improve or translate. The problem details type member gives each code a URI, for example https://developer.example.com/errors/card-declined, which leads to a page explaining the cause and what the client should do.
Clients must never parse title or detail to decide what to do. Saying this explicitly in the API documentation, and making sure every error has a code, is what lets us reword a message without breaking anyone.
3Human-Readable Title and Detail
title is a short summary that is the same for every occurrence of a code, such as Card declined. detail explains this particular case in words a developer, and possibly an end user, can understand: The card issuer declined the payment. Ask the customer to use another card. Both are written for the reader, in plain language, and they may be translated using the Accept-Language header. They describe the problem from the client's side, never our internal cause, so not Connection to issuer-gateway-3 refused.
4A Trace Id and Field-Level Errors
Every error carries a traceId, the id of the distributed trace for that request, which also appears in all our log lines for it. When a partner's support team reports a problem, they send that id and we find the full story in seconds. Without it, support has to guess from timestamps.
Validation failures need more than one message, because a request can be wrong in several places at once. An errors list gives one entry per problem, each with a pointer to the field in the request, a code and a message. Using the field path as the client sent it, such as items[2].quantity, lets a client highlight the exact input.
Notice that the errors do not echo the rejected values. The client already has them, and echoing them can copy personal data into logs and screenshots. For retryable failures we add a retryable flag and, for 429 and 503, a Retry-After header.
5What We Never Include
The never list is short and strict. Stack traces reveal frameworks and versions that can be matched to known vulnerabilities. SQL text and database error messages reveal schema names and confirm that input reaches the database. Internal class names, package names, hostnames, IP addresses and file paths map our system for an attacker. Secrets, tokens, keys and passwords obviously never appear, including through echoed input. And messages from third-party exceptions are never copied into detail, since we do not control what they contain.
None of this information is lost. It is logged under the same traceId, where access is controlled. The format therefore costs our engineers nothing while denying attackers a lot. We enforce the list in a shared error library and with tests that trigger failures and assert that the body has no stack trace or class names.
6Follow-up questions
Why not just use HTTP status codes and a message?
Because many different problems share one status. A 422 can mean a declined card, an expired offer or a limit reached, and a client needs to react differently to each. A stable code distinguishes them, and a message alone is not stable enough to program against.
How do we evolve the format without breaking clients?
We only add. New optional fields and new codes are allowed, and the documentation tells clients to ignore unknown fields and treat unknown codes by their status. Changing the meaning of a field or a code, or removing one, is a breaking change that needs a new API version.
Should the error code be numeric or a string?
A readable string such as card_declined is easier to understand in logs and client code than 40217, and it carries no false hierarchy. What matters most is that each code has one meaning forever and is documented at its type URI.
7Conclusion
In this answer, we designed a public API error format on top of RFC 9457 problem details. It has a stable code with a documented type URI for client logic, a title and detail written for people, a traceId so support can find our logs, and a field-level errors list for validation, plus retry hints where useful. It never contains stack traces, SQL, internal names, hostnames or secrets, which stay in access-controlled logs under the same trace id.
Open with
I'd build it on RFC 9457 problem details, sent as application/problem+json, and choose each field by who uses it. For client code, the key field is a stable machine-readable code like card_declined, which never changes meaning, plus a type URI pointing to a documentation page for that code.
Then, if they want more
I'd tell clients explicitly never to parse the text. For people, there's a title that's the same for every occurrence and a detail that explains this case from the client's point of view. For support, every error carries a trace id that's also in all our logs, so a partner can give us that id and we find everything.
For validation, an errors list with one entry per problem: the field path as the client sent it, like items[2].quantity, a code and a message, but not the rejected value. Retryable errors get a retryable flag and Retry-After on 429 and 503.
What I'd never include: stack traces, SQL or database messages, internal class names, hostnames, IPs, file paths, secrets, or messages copied from third-party exceptions, because clients can't act on them and attackers can. Those details stay in the logs under the trace id.
I'd enforce this in a shared library with tests, and evolve the format only by adding fields and codes.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Spring Web MVC
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Design the error response format for a public API. What fields would you include and...": https://scrivio-interview-library.netlify.app/spring-boot/error-handling/c576613fe9/