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
| Aspect | Details |
|---|---|
| Standard | RFC 7807 Problem Details for HTTP APIs |
| Spring Support | ProblemDetail (Spring 6+), @ControllerAdvice |
| Content-Type | application/problem+json |
| Benefits | Consistent 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)CodeLoading 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)CodeLoading syntax highlighter...
Mental Model: RFC 7807 Structure
TEXT(34 lines)CodeLoading syntax highlighter...
🔬 Deep Dive
Pattern 1: Spring 6+ Native ProblemDetail
JAVA(66 lines)CodeLoading syntax highlighter...
Pattern 2: Custom Problem Detail with Builder
JAVA(95 lines)CodeLoading syntax highlighter...
Pattern 3: Comprehensive Exception Handler
JAVA(171 lines)CodeLoading syntax highlighter...
Pattern 4: Error Catalog
JAVA(70 lines)CodeLoading syntax highlighter...
Pattern 5: Logging and Monitoring
JAVA(57 lines)CodeLoading syntax highlighter...
Pattern 6: Client-Side Error Handling
JAVA(46 lines)CodeLoading syntax highlighter...
⚠️ Common Mistakes
Mistake 1: Exposing Internal Details
JAVA(9 lines)CodeLoading syntax highlighter...
Mistake 2: Inconsistent Error Codes
JAVA(7 lines)CodeLoading 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)CodeLoading syntax highlighter...
What sensitive information is being exposed and how would you fix it?
✅ Solution:
Multiple security issues:
- Stack traces - Reveal internal class names, line numbers, library versions
- Cause messages - Can expose database schema, table names
- SQL queries - Can expose column names, sometimes even query parameters
- Exception messages - May contain user data or system details
Secure implementation:
JAVA(47 lines)CodeLoading 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)CodeLoading syntax highlighter...
✅ Solution:
JAVA(71 lines)CodeLoading 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)CodeLoading syntax highlighter...
✅ Solution:
JAVA(74 lines)CodeLoading syntax highlighter...
Exercise 3: Error Catalog
⭐⭐ Difficulty: Medium | ⏱️ Time: 20 minutes
Task: Create a centralized error catalog with predefined error types.
JAVA(4 lines)CodeLoading syntax highlighter...
✅ Solution:
JAVA(79 lines)CodeLoading 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)CodeLoading syntax highlighter...
✅ Solution:
JAVA(72 lines)CodeLoading 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)CodeLoading syntax highlighter...
✅ Solution:
JAVA(117 lines)CodeLoading syntax highlighter...
📝 Summary
| Component | Purpose |
|---|---|
| type | URI identifying error type |
| title | Human-readable summary (same for all instances) |
| status | HTTP status code |
| detail | Specific explanation for this occurrence |
| instance | URI of the request |
| extensions | Custom fields (errorCode, traceId, errors[]) |
📅 Review Schedule for This Article
| Day | Task | Time |
|---|---|---|
| Day 1 | Review RFC 7807 structure diagram | 5 min |
| Day 3 | Redo Exercise 1 (Basic RFC 7807 Response) | 15 min |
| Day 7 | Answer interview questions without looking | 10 min |
| Day 14 | Redo Debug This (Leaking Stack Trace) | 15 min |
| Day 30 | Audit your API's error responses for RFC 7807 compliance | 20 min |
Next: [Part 17: Bean Validation]