feat: add exception handling
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
package com.example.bankcards.exception;
|
||||
|
||||
public class AccessDeniedException extends RuntimeException {
|
||||
|
||||
public AccessDeniedException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.example.bankcards.exception;
|
||||
|
||||
public class BadRequestException extends RuntimeException {
|
||||
|
||||
public BadRequestException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package com.example.bankcards.exception;
|
||||
|
||||
import com.example.bankcards.dto.ErrorResponse;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.validation.FieldError;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
@RestControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
@ExceptionHandler(ResourceNotFoundException.class)
|
||||
public ResponseEntity<ErrorResponse> handleNotFound(
|
||||
ResourceNotFoundException ex
|
||||
) {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(
|
||||
new ErrorResponse(404, ex.getMessage())
|
||||
);
|
||||
}
|
||||
|
||||
@ExceptionHandler(BadRequestException.class)
|
||||
public ResponseEntity<ErrorResponse> handleBadRequest(
|
||||
BadRequestException ex
|
||||
) {
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(
|
||||
new ErrorResponse(400, ex.getMessage())
|
||||
);
|
||||
}
|
||||
|
||||
@ExceptionHandler(AccessDeniedException.class)
|
||||
public ResponseEntity<ErrorResponse> handleAccessDenied(
|
||||
AccessDeniedException ex
|
||||
) {
|
||||
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(
|
||||
new ErrorResponse(403, ex.getMessage())
|
||||
);
|
||||
}
|
||||
|
||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
public ResponseEntity<ErrorResponse> handleValidation(
|
||||
MethodArgumentNotValidException ex
|
||||
) {
|
||||
Map<String, String> errors = new HashMap<>();
|
||||
ex
|
||||
.getBindingResult()
|
||||
.getAllErrors()
|
||||
.forEach(error -> {
|
||||
String fieldName = ((FieldError) error).getField();
|
||||
errors.put(fieldName, error.getDefaultMessage());
|
||||
});
|
||||
ErrorResponse response = new ErrorResponse(400, "Validation failed");
|
||||
response.setErrors(errors);
|
||||
return ResponseEntity.badRequest().body(response);
|
||||
}
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
public ResponseEntity<ErrorResponse> handleGeneral(Exception ex) {
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(
|
||||
new ErrorResponse(500, "Internal server error: " + ex.getMessage())
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.example.bankcards.exception;
|
||||
|
||||
public class ResourceNotFoundException extends RuntimeException {
|
||||
|
||||
public ResourceNotFoundException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user