Java

Global Error Handling

Consistent error responses are crucial for API usability. This article covers implementing RFC 7807 Problem Details, building a global exception handler, and creating a unified error handling strategy across your application.

📋 At a Glance

AspectDetails
StandardRFC 7807 Problem Details for HTTP APIs
Spring SupportProblemDetail (Spring 6+), @ControllerAdvice
Content-Typeapplication/problem+json
BenefitsConsistent errors, machine-readable, self-documenting

🎯 What You'll Learn

  • RFC 7807 Problem Details standard
  • Spring Boot 3 native support
  • Global exception handler implementation
  • Error mapping strategies
  • Logging and monitoring integration

Production Story: The Inconsistent API

An API had inconsistent error responses across endpoints:

JSON(11 lines)
Code
Loading syntax highlighter...
Problems:
  • Frontend needed different parsing for each endpoint
  • No way to programmatically identify error type
  • Documentation was a nightmare
  • Third-party integrations constantly broke
The fix: RFC 7807 everywhere:
JSON(8 lines)
Code
Loading syntax highlighter...

Mental Model: RFC 7807 Structure

TEXT(34 lines)
Code
Loading syntax highlighter...

🔬 Deep Dive

Pattern 1: Spring 6+ Native ProblemDetail

JAVA(66 lines)
Code
Loading syntax highlighter...

Pattern 2: Custom Problem Detail with Builder

JAVA(95 lines)
Code
Loading syntax highlighter...

Pattern 3: Comprehensive Exception Handler

JAVA(171 lines)
Code
Loading syntax highlighter...

Pattern 4: Error Catalog

JAVA(70 lines)
Code
Loading syntax highlighter...

Pattern 5: Logging and Monitoring

JAVA(57 lines)
Code
Loading syntax highlighter...

Pattern 6: Client-Side Error Handling

JAVA(46 lines)
Code
Loading syntax highlighter...

⚠️ Common Mistakes

Mistake 1: Exposing Internal Details

JAVA(9 lines)
Code
Loading syntax highlighter...

Mistake 2: Inconsistent Error Codes

JAVA(7 lines)
Code
Loading syntax highlighter...

🐛 Debug This: The Leaking Stack Trace

A security audit reveals: "API responses include full stack traces and SQL queries in production!"

JAVA(28 lines)
Code
Loading syntax highlighter...
What sensitive information is being exposed and how would you fix it?

✅ Solution:

Multiple security issues:

  1. Stack traces - Reveal internal class names, line numbers, library versions
  2. Cause messages - Can expose database schema, table names
  3. SQL queries - Can expose column names, sometimes even query parameters
  4. Exception messages - May contain user data or system details
Secure implementation:
JAVA(47 lines)
Code
Loading syntax highlighter...
The lesson: Never expose stack traces, causes, or internal details to clients. Log everything server-side with a correlation ID, return only sanitized messages to users.

💻 Exercises

Exercise 1: Basic RFC 7807 Response

⭐ Difficulty: Easy | ⏱️ Time: 15 minutes

Task: Create a simple RFC 7807 problem response.
JAVA(4 lines)
Code
Loading syntax highlighter...
✅ Solution:
JAVA(71 lines)
Code
Loading syntax highlighter...

Exercise 2: Exception Handler with Metrics

⭐⭐ Difficulty: Medium | ⏱️ Time: 20 minutes

Task: Create an exception handler that records error metrics.
JAVA(4 lines)
Code
Loading syntax highlighter...
✅ Solution:
JAVA(74 lines)
Code
Loading syntax highlighter...

Exercise 3: Error Catalog

⭐⭐ Difficulty: Medium | ⏱️ Time: 20 minutes

Task: Create a centralized error catalog with predefined error types.
JAVA(4 lines)
Code
Loading syntax highlighter...
✅ Solution:
JAVA(79 lines)
Code
Loading syntax highlighter...

Exercise 4: Validation Error Formatting

⭐⭐⭐ Difficulty: Medium-Hard | ⏱️ Time: 20 minutes

Task: Handle Spring validation errors with detailed field-level information.
JAVA(2 lines)
Code
Loading syntax highlighter...
✅ Solution:
JAVA(72 lines)
Code
Loading syntax highlighter...

Exercise 5: Client Error Handling

⭐⭐⭐⭐ Difficulty: Hard | ⏱️ Time: 25 minutes

Task: Create an API client that properly handles RFC 7807 error responses.
JAVA(4 lines)
Code
Loading syntax highlighter...
✅ Solution:
JAVA(117 lines)
Code
Loading syntax highlighter...

📝 Summary

ComponentPurpose
typeURI identifying error type
titleHuman-readable summary (same for all instances)
statusHTTP status code
detailSpecific explanation for this occurrence
instanceURI of the request
extensionsCustom fields (errorCode, traceId, errors[])

📅 Review Schedule for This Article

DayTaskTime
Day 1Review RFC 7807 structure diagram5 min
Day 3Redo Exercise 1 (Basic RFC 7807 Response)15 min
Day 7Answer interview questions without looking10 min
Day 14Redo Debug This (Leaking Stack Trace)15 min
Day 30Audit your API's error responses for RFC 7807 compliance20 min

Next: [Part 17: Bean Validation]