41 lines
1.4 KiB
Java
41 lines
1.4 KiB
Java
package com.goi.erp.common.exception;
|
|
|
|
import java.time.LocalDateTime;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
|
|
@RestControllerAdvice
|
|
public class GlobalExceptionHandler {
|
|
|
|
@ExceptionHandler(RuntimeException.class)
|
|
public ResponseEntity<?> handleRuntimeException(RuntimeException ex) {
|
|
|
|
Map<String, Object> body = new HashMap<>();
|
|
body.put("error", ex.getMessage()); // ex.getMessage() → "Invalid password"
|
|
body.put("timestamp", LocalDateTime.now());
|
|
body.put("status", HttpStatus.BAD_REQUEST.value());
|
|
|
|
return ResponseEntity.badRequest().body(body);
|
|
}
|
|
|
|
@ExceptionHandler(UserNotFoundException.class)
|
|
public ResponseEntity<?> handleUserNotFound(UserNotFoundException ex) {
|
|
return ResponseEntity
|
|
.status(HttpStatus.NOT_FOUND)
|
|
.body(Map.of("error", ex.getMessage()));
|
|
}
|
|
|
|
@ExceptionHandler(InvalidPasswordException.class)
|
|
public ResponseEntity<?> handleInvalidPassword(InvalidPasswordException ex) {
|
|
return ResponseEntity
|
|
.status(HttpStatus.UNAUTHORIZED)
|
|
.body(Map.of("error", ex.getMessage()));
|
|
}
|
|
}
|
|
|