Skip to content

Commit

Permalink
[GLOBAL EXCEPTION HANDLER] Created global exception handler
Browse files Browse the repository at this point in the history
* created car controller

* fixed checkstyle

* added global exception handler

* deleted empty line in CarController
  • Loading branch information
DmytroV95 authored Oct 3, 2023
1 parent 6eda93a commit 32f836b
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
@RestController
@RequestMapping(value = "/cars")
public class CarController {

@PostMapping
@ResponseStatus(HttpStatus.CREATED)
@Operation(summary = "Save new car",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.project.carsharingapp.exception;

import java.time.LocalDateTime;
import java.util.List;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

@ControllerAdvice
public class CustomGlobalExceptionHandler extends ResponseEntityExceptionHandler {
@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(
MethodArgumentNotValidException ex,
HttpHeaders headers,
HttpStatusCode status,
WebRequest request
) {
List<String> errors = ex.getBindingResult().getAllErrors()
.stream()
.map(this::getErrorMessage)
.toList();
ErrorResponseDto response = new ErrorResponseDto(
LocalDateTime.now(),
HttpStatus.BAD_REQUEST,
errors.toArray(new String[0])
);
return new ResponseEntity<>(response, headers, HttpStatus.BAD_REQUEST);
}

@ExceptionHandler(RegistrationException.class)
protected ResponseEntity<Object> handleRegistrationException(
RegistrationException exception) {
ErrorResponseDto response = new ErrorResponseDto(
LocalDateTime.now(),
HttpStatus.BAD_REQUEST,
new String[]{exception.getMessage()}
);
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}

@ExceptionHandler(RuntimeException.class)
protected ResponseEntity<Object> handleAllErrors(
RuntimeException exception) {
ErrorResponseDto response = new ErrorResponseDto(
LocalDateTime.now(),
HttpStatus.INTERNAL_SERVER_ERROR,
new String[]{exception.getMessage()}
);
return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
}

@ExceptionHandler(EntityNotFoundException.class)
protected ResponseEntity<Object> handleAllErrors(
EntityNotFoundException exception) {
ErrorResponseDto response = new ErrorResponseDto(
LocalDateTime.now(),
HttpStatus.NOT_FOUND,
new String[]{exception.getMessage()}
);
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
}

private String getErrorMessage(ObjectError e) {
if (e instanceof FieldError) {
String field = ((FieldError) e).getField();
String message = e.getDefaultMessage();
return field + " " + message;
}
return e.getDefaultMessage();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.project.carsharingapp.exception;

public class EntityNotFoundException extends RuntimeException {
public EntityNotFoundException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.project.carsharingapp.exception;

import java.time.LocalDateTime;
import org.springframework.http.HttpStatus;

public record ErrorResponseDto(LocalDateTime timestamp,
HttpStatus status,
String[] errors){
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.project.carsharingapp.exception;

public class RegistrationException extends RuntimeException {
public RegistrationException(String message) {
super(message);
}
}

0 comments on commit 32f836b

Please sign in to comment.