From 77a355f3a28e0f134d11ab3bc994eef2a40b5ab6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Mert=20C=C3=B6merto=C4=9Flu?= Date: Tue, 31 Jan 2023 10:43:42 +0300 Subject: [PATCH] user-profile exception handler --- .../com/bilgeadam/exception/ErrorMessage.java | 20 ++++ .../com/bilgeadam/exception/ErrorType.java | 21 ++++ .../exception/GlobalExceptionHandler.java | 112 ++++++++++++++++++ .../exception/UserprofileExecption.java | 20 ++++ 4 files changed, 173 insertions(+) create mode 100644 userprofile-microservice/src/main/java/com/bilgeadam/exception/ErrorMessage.java create mode 100644 userprofile-microservice/src/main/java/com/bilgeadam/exception/ErrorType.java create mode 100644 userprofile-microservice/src/main/java/com/bilgeadam/exception/GlobalExceptionHandler.java create mode 100644 userprofile-microservice/src/main/java/com/bilgeadam/exception/UserprofileExecption.java diff --git a/userprofile-microservice/src/main/java/com/bilgeadam/exception/ErrorMessage.java b/userprofile-microservice/src/main/java/com/bilgeadam/exception/ErrorMessage.java new file mode 100644 index 0000000..9f4d929 --- /dev/null +++ b/userprofile-microservice/src/main/java/com/bilgeadam/exception/ErrorMessage.java @@ -0,0 +1,20 @@ +package com.bilgeadam.exception; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.springframework.stereotype.Component; + +import java.util.List; + +@AllArgsConstructor +@NoArgsConstructor +@Data +@Builder +@Component +public class ErrorMessage { + int code; + String message; + List fields; +} diff --git a/userprofile-microservice/src/main/java/com/bilgeadam/exception/ErrorType.java b/userprofile-microservice/src/main/java/com/bilgeadam/exception/ErrorType.java new file mode 100644 index 0000000..2d4fa96 --- /dev/null +++ b/userprofile-microservice/src/main/java/com/bilgeadam/exception/ErrorType.java @@ -0,0 +1,21 @@ +package com.bilgeadam.exception; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import org.springframework.http.HttpStatus; + +@AllArgsConstructor +@NoArgsConstructor +@Getter +public enum ErrorType { + INTERNAL_ERROR(5100,"Sunucuda beklenmeyen hata oluştu",HttpStatus.INTERNAL_SERVER_ERROR), + BAD_REQUEST_ERROR(4100,"Parametre eksik yada hatalı",HttpStatus.BAD_REQUEST), + LOGIN_ERROR(4110,"Kullanıcı adı yada şifre hatalı",HttpStatus.BAD_REQUEST), + REGISTER_KULLANICIADI_KAYITLI(4112,"Kullanıcı adı zaten kayıtlı",HttpStatus.BAD_REQUEST), + REGISTER_REPASSWORD_ERROR(4111,"Şifreler uyuşmuyor" , HttpStatus.BAD_REQUEST); + + int code; + String message; + HttpStatus httpStatus; +} diff --git a/userprofile-microservice/src/main/java/com/bilgeadam/exception/GlobalExceptionHandler.java b/userprofile-microservice/src/main/java/com/bilgeadam/exception/GlobalExceptionHandler.java new file mode 100644 index 0000000..82cce07 --- /dev/null +++ b/userprofile-microservice/src/main/java/com/bilgeadam/exception/GlobalExceptionHandler.java @@ -0,0 +1,112 @@ +package com.bilgeadam.exception; + +import com.fasterxml.jackson.databind.exc.InvalidFormatException; +import org.springframework.dao.DataIntegrityViolationException; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.http.converter.HttpMessageNotReadableException; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.MissingPathVariableException; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; + +import java.util.ArrayList; +import java.util.List; + +import static com.bilgeadam.exception.ErrorType.*; + +@ControllerAdvice +public class GlobalExceptionHandler { + + @ExceptionHandler(Exception.class) + @ResponseBody + public ResponseEntity handlerRuntimeException(Exception exception){ + System.out.println("Hata oluştu"); + ErrorType errorType = INTERNAL_ERROR; + return new ResponseEntity<>(createErrorMessage(exception,errorType),HttpStatus.INTERNAL_SERVER_ERROR); + } + + @ExceptionHandler(UserprofileExecption.class) + @ResponseBody + public ResponseEntity handlerAuthMicroseviceException(UserprofileExecption exception){ + return new ResponseEntity<>(createErrorMessage(exception,exception.getErrorType()), HttpStatus.BAD_REQUEST); + } + + @ExceptionHandler(HttpMessageNotReadableException.class) + @ResponseBody + public final ResponseEntity handleMessageNotReadableException( + HttpMessageNotReadableException exception) { + ErrorType errorType = BAD_REQUEST_ERROR; + return new ResponseEntity<>(createErrorMessage(exception,errorType), errorType.getHttpStatus()); + } + + @ExceptionHandler(InvalidFormatException.class) + @ResponseBody + public final ResponseEntity handleInvalidFormatException( + InvalidFormatException exception) { + ErrorType errorType = BAD_REQUEST_ERROR; + return new ResponseEntity<>(createErrorMessage(exception,errorType), errorType.getHttpStatus()); + } + + @ExceptionHandler(DataIntegrityViolationException.class) + @ResponseBody + public final ResponseEntity handlePSQLException( + DataIntegrityViolationException exception) { + ErrorType errorType = REGISTER_KULLANICIADI_KAYITLI; + return new ResponseEntity<>(createErrorMessage(exception,errorType), errorType.getHttpStatus()); + } + + + @ExceptionHandler(MethodArgumentTypeMismatchException.class) + @ResponseBody + public final ResponseEntity handleMethodArgumentMisMatchException( + MethodArgumentTypeMismatchException exception) { + + ErrorType errorType = BAD_REQUEST_ERROR; + return new ResponseEntity<>(createErrorMessage(exception,errorType), errorType.getHttpStatus()); + } + + @ExceptionHandler(MissingPathVariableException.class) + @ResponseBody + public final ResponseEntity handleMethodArgumentMisMatchException( + MissingPathVariableException exception) { + + ErrorType errorType = BAD_REQUEST_ERROR; + return new ResponseEntity<>(createErrorMessage(exception,errorType), errorType.getHttpStatus()); + } + + + @ExceptionHandler(MethodArgumentNotValidException.class) + @ResponseBody + public final ResponseEntity handleMethodArgumentNotValidException( + MethodArgumentNotValidException exception) { + + ErrorType errorType = BAD_REQUEST_ERROR; + List fields = new ArrayList<>(); + exception + .getBindingResult() + .getFieldErrors() + .forEach(e -> fields.add(e.getField() + ": " + e.getDefaultMessage())); + ErrorMessage errorMessage = createErrorMessage(exception,errorType); + errorMessage.setFields(fields); + return new ResponseEntity<>(errorMessage, errorType.getHttpStatus()); + } + + /** + * Hata yakalama işlemleri bir çok hata için ayrı ayrı yapılmalıdır. bu nedenel tüm hataların + * içerisine log alma işlemi yazmak zorunda kalırız. bu işlemleri tekelleştirmek ve hata log kayıtlarını + * toplamak için tekbir method kullanmak daha doğru olacaktır. + * @param exception + * @param errorType + * @return + */ + private ErrorMessage createErrorMessage(Exception exception,ErrorType errorType){ + System.out.println("Tüm hataların geçtiği nokta...: "+ exception.getMessage()); + return ErrorMessage.builder() + .message(errorType.getMessage()) + .code(errorType.getCode()) + .build(); + } +} diff --git a/userprofile-microservice/src/main/java/com/bilgeadam/exception/UserprofileExecption.java b/userprofile-microservice/src/main/java/com/bilgeadam/exception/UserprofileExecption.java new file mode 100644 index 0000000..920239e --- /dev/null +++ b/userprofile-microservice/src/main/java/com/bilgeadam/exception/UserprofileExecption.java @@ -0,0 +1,20 @@ +package com.bilgeadam.exception; + +import lombok.Getter; + +@Getter +public class UserprofileExecption extends RuntimeException{ + + private final ErrorType errorType; + + public UserprofileExecption(ErrorType errorType) { + super(errorType.getMessage()); + this.errorType = errorType; + } + + public UserprofileExecption(ErrorType errorType, String message) { + super(message); + this.errorType = errorType; + } + +}