Quick revision
The short answer
I would start with a shared definition of four error categories, client errors, expected business outcomes, dependency failures and internal bugs, each with one handling rule. Expected outcomes are represented in types and unexpected failures as exceptions mapped centrally to RFC 9457 problem details; every error is logged as a structured event with the exception and a correlation ID and counted in metrics by category. A platform or architecture group owns the shared libraries and the guide, and reviews the standard against incident data at regular intervals.
What to remember
In this answer, we defined errors by category, client errors, business outcomes, dependency failures and internal bugs, each with a handling rule. We represented outcomes in types and failures as exceptions mapped centrally to problem details, required structured logs with the exception and a correlation ID and metrics tagged by category and code, gave ownership to a platform group with a review forum and shared libraries, and kept the standard honest by reviewing it against incident data at regular intervals.
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.
I would start with a shared definition of four error categories, client errors, expected business outcomes, dependency failures and internal bugs, each with one handling rule. Expected outcomes are represented in types and unexpected failures as exceptions mapped centrally to RFC 9457 problem details; every error is logged as a structured event with the exception and a correlation ID and counted in metrics by category. A platform or architecture group owns the shared libraries and the guide, and reviews the standard against incident data at regular intervals.
Why this question is asked
With hundreds of services, errors stop being a local coding concern and become a company-wide language. If one service calls a timeout a 500 and another calls it a 400, retries go wrong, dashboards lie and on-call engineers waste the first twenty minutes of every incident working out what an error even means. Interviewers ask this at principal level to see whether we can design something that many teams will live with for years, and keep it alive after the launch. In this answer, we will define what an error is, decide how each category is represented in code, logs, metrics and APIs, set up ownership, and see how incident data keeps the standard honest.
1Starting With a Definition
The word error covers very different things, and most inconsistency comes from treating them the same. So the first step is to split them by who caused them and what should happen next. A client error is a request that is wrong: invalid input, missing permission, a resource that does not exist. An expected business outcome is a normal result that is not success, such as a declined payment or an out-of-stock item. A dependency failure is when something we rely on is slow, down or overloaded. An internal bug is our own code doing something it should not, such as a NullPointerException.
Each category gets one handling rule. Client errors are reported to the caller and not retried, and do not page anyone. Business outcomes are handled by the caller as part of the flow and are often not errors in monitoring at all. Dependency failures may be retried with backoff and are watched for rising rates. Internal bugs fail the request, are logged with full detail, and count against the service's error budget, the share of failed requests its reliability target allows.
| Category | Example | In code | API | Alerting |
|---|---|---|---|---|
| Client error | invalid field, not found | exception, 4xx type | 400, 404, 409 | rate only, no page |
| Business outcome | payment declined | sealed result type | 200 with outcome, or 422 | business dashboards |
| Dependency failure | database timeout | retryable exception | 503 with Retry-After | page on sustained rate |
| Internal bug | NullPointerException | uncaught exception | 500, no details | page, error budget |
2Representing Errors in Code and APIs
In code, the representation follows the category. Business outcomes are part of the method's return type, using Optional for simple absence and sealed result types for outcomes with detail, so the compiler forces callers to handle them. Failures are unchecked exceptions from a small shared hierarchy organized by category, each with a stable error code and structured context, and always carrying the cause.
At the API boundary, a shared @RestControllerAdvice in the platform starter maps exceptions to RFC 9457 problem details: a type URI, a title, the status, a stable code and a correlation ID, and never a stack trace or internal message. For asynchronous messaging, the equivalent is a dead-letter message with the same code and category in its headers. Because every service uses the same mapping, a client library can turn a 503 problem from any service back into the same retryable exception, and retry policies behave consistently across call chains.
3Representing Errors in Logs and Metrics
Logs are where people debug, so the rule is structured logs: one JSON event per error with the level, the service, the error code, the category, the correlation ID or trace ID, and the full exception including causes and suppressed exceptions. The shared logging starter configures the encoder, so services get this by default. A short rule, log an exception once, at the place where it is handled, keeps traces from appearing four times.
Metrics are where people notice, so every error is counted with the category and code as tags. A Micrometer counter in the shared handler, plus the standard HTTP server metrics tagged by status and outcome, gives each team the same dashboards. Alerts are then written per category: a rising rate of internal bugs or dependency failures pages someone; a rising rate of client errors may only open a ticket, because it often means a client released a bug.
We keep the number of distinct codes bounded, perhaps a few hundred across the company, because metrics systems struggle with unbounded tag values. Order IDs or user IDs belong in logs, never in metric tags.
4Who Owns the Standard
A standard without an owner decays within a year: new teams copy old code, frameworks change, and nobody updates the guide. So a named group owns it, usually the platform or architecture group that already maintains the service template and shared libraries. Its responsibilities are concrete: the written guide, the shared starter with the exception types, error handler, logging encoder and metrics, the error code registry, and the static analysis rules that check the basics.
Ownership does not mean the group writes every decision alone. A small review forum with senior engineers from product teams approves changes, such as a new category or a change in a status mapping, so the standard reflects real needs. Product teams own their own error codes within the registry. Changes to the shared types follow semantic versioning, because hundreds of services depend on them.
- Guide and error code registryowned by the platform group, reviewed by a forum
- Shared starterexception types, error handler, log encoder, metrics
- Static analysis and templatesrules in CI, new services start compliant
- Service codeteams choose codes and handle outcomes
- Logs, metrics, APIsthe same shape everywhere
5 layers, from the top down. Step through them one at a time.
5Keeping It Honest With Incident Data
The best test of an error standard is whether incidents get easier. So at a regular interval, perhaps every quarter, the owning group reviews incident reports and asks specific questions. Was the root cause in the logs? Did the error category on the dashboards match what was actually happening? Did retries help or make things worse? Were there errors that fit no category?
Each answer leads to a concrete change. If root causes were missing, the logging encoder or a lint rule needs work. If dependency failures were labelled as internal bugs, the client libraries need better mapping. If a new kind of failure keeps appearing, such as rate limiting by a third-party API, it may deserve its own code or category. Over several years this loop is what keeps the standard alive, because it ties the rules to outcomes people care about, not to taste.
6Follow-up questions
How do you introduce this to hundreds of existing services?
Gradually and through tooling. The shared starter is adopted first, which gives consistent API errors and logging with little code change. New code follows the representation rules from a set date, and existing code moves as it is touched. Lint rules start as warnings, and a dashboard shows each service's adoption, which creates gentle pressure without blocking delivery.
Should business outcomes appear in error metrics?
Usually not as errors, because a declined payment is the system working correctly. They belong in business metrics, such as decline rate by reason, which product teams watch. Counting them as errors would make error rates meaningless and trigger false alerts.
What if teams use different languages, not only Java?
The definition, the categories, the problem detail format, the log fields and the metric names are language-neutral, so they apply to every service. Only the shared library is language-specific, and the platform group provides an equivalent for each supported language or documents the format clearly enough for teams to implement it.
7Conclusion
In this answer, we defined errors by category, client errors, business outcomes, dependency failures and internal bugs, each with a handling rule. We represented outcomes in types and failures as exceptions mapped centrally to problem details, required structured logs with the exception and a correlation ID and metrics tagged by category and code, gave ownership to a platform group with a review forum and shared libraries, and kept the standard honest by reviewing it against incident data at regular intervals.
Open with
I would start by defining what an error is, because most inconsistency comes from mixing different things. I use four categories: client errors like invalid input or not found, expected business outcomes like a declined payment, dependency failures like a database timeout, and internal bugs.
Then, if they want more
Each has one rule: client errors go back to the caller and are not retried, business outcomes are handled in the flow, dependency failures may be retried with backoff, and bugs fail the request and count against the error budget.
In code, business outcomes are return types, Optional or sealed result types, and failures are exceptions from a small shared hierarchy with stable codes and causes. At the API, one shared handler maps them to RFC 9457 problem details with a code and correlation ID, never a stack trace.
Logs are structured, with the full exception, the code, the category and the correlation ID, and every error increments a metric tagged by category and code, so alerts can page on bugs and dependency failures but not on client mistakes.
A platform or architecture group owns the guide, the error code registry, the shared starter and the lint rules, with a forum of senior engineers approving changes. And every quarter we review incidents: was the root cause in the logs, did the categories match reality, and we adjust the standard based on that.
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
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
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 "Across a company with hundreds of Java services, how would you define what an error is...": https://scrivio-interview-library.netlify.app/java/exceptions/21c911409d/