Files
java-test-services/user-service/src/main/java/com/dken/userservice/exception/GlobalExceptionHandler.java

49 lines
1.8 KiB
Java

package com.dken.userservice.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.util.Map;
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(DefaultException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public Map<String, String> handleDefaultException(DefaultException ex) {
return Map.of("message", ex.getMessage());
}
@ExceptionHandler(InvalidUserIdException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public Map<String, String> handleInvalidId(InvalidUserIdException ex) {
return Map.of("message", ex.getMessage());
}
@ExceptionHandler(UserNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public Map<String, String> handleNotFound(UserNotFoundException ex) {
return Map.of("message", ex.getMessage());
}
@ExceptionHandler(GiftCodeReservationException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public Map<String, String> handleGiftCodeReservation(GiftCodeReservationException ex) {
return Map.of("message", ex.getMessage());
}
@ExceptionHandler(LocalProcessingException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public Map<String, String> handleLocalProcessing(LocalProcessingException ex) {
return Map.of("message", ex.getMessage());
}
@ExceptionHandler(RuntimeException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public Map<String, String> handleRuntimeException(RuntimeException ex) {
return Map.of("message", "An unexpected error occurred: " + ex.getMessage());
}
}