⚠️
Web & REST

Global Exception Handling

@RestControllerAdvice
💡 @RestControllerAdvice ek CENTRAL COMPLAINT DESK hai MALL mein — HAR SHOP (controller) mein SEPARATE complaint desk (try-catch) rakhne ke bajaye, EK CENTRAL DESK SAARI COMPLAINTS (exceptions) HANDLE karता hai, CONSISTENT tareeke se, POORE MALL ke liye.

@RestControllerAdvice EK CENTRALIZED CLASS DEFINE karta hai jo SAARE @RestController classes ke EXCEPTIONS ko HANDLE karta hai — HAR CONTROLLER mein DUPLICATE try-catch LIKHNE ke bajaye, EK JAGAH, @ExceptionHandler methods se, DIFFERENT exception TYPES ko DIFFERENT RESPONSES mein CONVERT kiya jaata hai.

Spring Boot 3.x mein, ProblemDetail class RFC 7807 STANDARD FORMAT provide karta hai — CONSISTENT, INDUSTRY-STANDARD error RESPONSE structure (type, title, status, detail) — HAR API mein CUSTOM error format INVENT karne ke bajaye, EK ESTABLISHED STANDARD follow kiya jaata hai.

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(UserNotFoundException.class)
    public ProblemDetail handleNotFound(UserNotFoundException ex) {
        ProblemDetail problem = ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND, ex.getMessage());
        problem.setTitle("User Not Found");
        return problem;
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ProblemDetail handleValidation(MethodArgumentNotValidException ex) {
        ProblemDetail problem = ProblemDetail.forStatus(HttpStatus.BAD_REQUEST);
        problem.setDetail("Validation failed: " + ex.getMessage());
        return problem;
    }

    @ExceptionHandler(Exception.class)   // catch-all, LAST resort
    public ProblemDetail handleGeneric(Exception ex) {
        return ProblemDetail.forStatusAndDetail(HttpStatus.INTERNAL_SERVER_ERROR, "Unexpected error occurred");
    }
}
⚠️
@RestControllerAdvice ek CENTRAL COMPLAINT DESK hai MALL mein — HAR SHOP (controller) mein SEPARATE complaint desk (try-catch) rakhne ke bajaye, EK CENTRAL DESK SAARI COMPLAINTS (exceptions) HANDLE karता hai, CONSISTENT tareeke se, POORE MALL ke liye.
1 / 6
⚡ झट से Recap
  • @RestControllerAdvice = CENTRALIZED exception handling, saare controllers ke liye
  • ProblemDetail (Spring Boot 3.x) = RFC 7807 STANDARD error format
  • Generic catch-all handler HAMESHA LAST — specific exceptions PEHLE
इस page में (2 subtopics)

@RestControllerAdvice ek CLASS ko MARK karta hai jo SAARE @RestController classes ke liye GLOBAL EXCEPTION HANDLING PROVIDE karta hai — @ExceptionHandler methods is CLASS mein DEFINE hote hain, HAR CONTROLLER mein try-catch DUPLICATE karne ki zaroorat NAHI.

@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(UserNotFoundException.class)
    public ResponseEntity<ErrorResponse> handleNotFound(UserNotFoundException ex) {
        return ResponseEntity.status(HttpStatus.NOT_FOUND)
            .body(new ErrorResponse(ex.getMessage()));
    }
}

Spring Boot 3.x mein, ProblemDetail class RFC 7807 STANDARD FORMAT follow karta hai ERROR RESPONSES ke liye — type, title, status, detail, instance FIELDS ke saath, EK CONSISTENT, INDUSTRY-STANDARD error format DETA hai, HAR API mein CUSTOM error format INVENT karne ke bajaye.

@ExceptionHandler(UserNotFoundException.class)
public ProblemDetail handleNotFound(UserNotFoundException ex) {
    ProblemDetail problem = ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND, ex.getMessage());
    problem.setTitle("User Not Found");
    return problem;
}