Skip to main content

MODULE 10 - Exception Handling and Logging

Welcome to Module 10! πŸ“‹β€‹

Your application encounters an error... what does the user see? An unreadable Java stack trace? This module teaches you how to handle errors properly and trace what happens in your application.


πŸŽ“ Learning Objectives​

By the end of this module, you will be able to:

  1. βœ… Create custom exceptions
  2. βœ… Use @ControllerAdvice for global handling
  3. βœ… Return structured error responses
  4. βœ… Log effectively with SLF4J
  5. βœ… Configure Logback for different environments

πŸ“š Prerequisites​

Before starting this module, you should have:

  • βœ… Completed Modules 1 to 9
  • βœ… Created REST controllers
  • βœ… Understood HTTP codes (4xx, 5xx)
  • βœ… Be familiar with Java exceptions

πŸ—ΊοΈ Module Outline​

Theoretical Part (4 chapters)​

Chapter 1: Exception Handling​

Duration: 45 minutes

Content:

  • Custom exceptions
  • Exception hierarchy
  • Best practices
  • Checked vs Unchecked

Why important? The foundation of error handling


Chapter 2: @ControllerAdvice​

Duration: 1 hour

Content:

  • @ControllerAdvice and @RestControllerAdvice
  • @ExceptionHandler
  • Standardized error responses
  • ProblemDetail (RFC 7807)
  • ResponseStatusException

Why important? Centralized error handling


Chapter 3: Logging with SLF4J​

Duration: 45 minutes

Content:

  • SLF4J and logging facades
  • Log levels (TRACE β†’ ERROR)
  • Logger patterns
  • MDC (Mapped Diagnostic Context)
  • Lombok @Slf4j

Why important? Diagnose issues


Chapter 4: Logback Configuration​

Duration: 45 minutes

Content:

  • logback-spring.xml
  • Appenders (Console, File, Rolling)
  • Patterns and formatting
  • Spring profiles
  • JSON logs for production

Why important? Professional configuration


Practical Part (2 Labs)​

Lab 10.1: Centralized Error Management​

Duration: 1h30

Objective: Implement a complete error handling system

You will:

  • Create business exceptions
  • Implement a GlobalExceptionHandler
  • Standardize error responses
  • Test with different scenarios

Lab 10.2: Logging System​

Duration: 1h30

Objective: Configure professional logging

You will:

  • Configure Logback
  • Create environment-specific logs
  • Implement MDC for traceability
  • File logs with rotation

πŸ“Š Total Duration​

TypeDuration
Theory3h15 (4 chapters)
Practice3h (2 Labs)
Total6h15 (~2 sessions)

🎯 What You Will Understand​

Before this Module​

// Unhandled error 😰
@GetMapping("/{id}")
public User getUser(@PathVariable Long id) {
return userRepository.findById(id).orElseThrow();
// β†’ 500 Internal Server Error with full stack trace!
}

// No logs
public void processOrder(Order order) {
// Does it work? Does it fail? No idea...
}

After this Module​

// Clean error handling βœ…
@GetMapping("/{id}")
public User getUser(@PathVariable Long id) {
return userService.findById(id);
// β†’ 404 {"type": "user-not-found", "title": "User not found", ...}
}

// Structured logging
@Slf4j
public class OrderService {
public void processOrder(Order order) {
log.info("Processing order {} for user {}", order.getId(), order.getUserId());
// ...
log.debug("Order total calculated: {}", total);
}
}

Clean error response:

{
"type": "https://api.example.com/errors/user-not-found",
"title": "User Not Found",
"status": 404,
"detail": "User with ID 123 was not found",
"instance": "/api/users/123",
"timestamp": "2024-01-15T10:30:00Z"
}

πŸš€ Why This Module is Essential​

1. User Experience​

Without management:
- Stack trace exposed to client 😱
- Cryptic error messages
- Hard to debug on the frontend

With management:
- Clear and localized messages
- Documented error codes
- Standardized JSON format

2. Debugging in Production​

Without logs:
- "The app crashed last night"
- "No idea what happened"
- Impossible to reproduce

With logs:
- Complete event trace
- User context (MDC)
- Filtering by level

3. Compliance with Standards​

RFC 7807 - Problem Details for HTTP APIs
β”œβ”€β”€ Standardized format
β”œβ”€β”€ Interoperability
└── Automatic documentation

πŸ—ΊοΈ Training Progression​

Module 10 = Production-ready application!


πŸ“– How to Approach This Module​

  1. Chapter 1 β†’ Understand Java exceptions
  2. Chapter 2 + Lab 10.1 β†’ Implement global management
  3. Chapter 3 β†’ Learn logging
  4. Chapter 4 + Lab 10.2 β†’ Configure Logback

βœ… Self-Assessment​

Before starting, answer:

  1. Do you know how to create a custom exception? ☐ Yes ☐ No
  2. Are you familiar with @ControllerAdvice? ☐ Yes ☐ No
  3. Do you use System.out.println for debugging? ☐ Yes ☐ No
  4. Do you know how to configure Logback? ☐ Yes ☐ No

If you answered "No" anywhere, this module is for you!


🎯 Next Step​

Ready to professionalize your error handling?

➑️ Start with Chapter 1: Exception Handling


⬅️ Back to Course Outline