-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
userprofile-microservice/src/main/java/com/bilgeadam/exception/ErrorMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String> fields; | ||
} |
21 changes: 21 additions & 0 deletions
21
userprofile-microservice/src/main/java/com/bilgeadam/exception/ErrorType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
112 changes: 112 additions & 0 deletions
112
userprofile-microservice/src/main/java/com/bilgeadam/exception/GlobalExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<ErrorMessage> 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<ErrorMessage> handlerAuthMicroseviceException(UserprofileExecption exception){ | ||
return new ResponseEntity<>(createErrorMessage(exception,exception.getErrorType()), HttpStatus.BAD_REQUEST); | ||
} | ||
|
||
@ExceptionHandler(HttpMessageNotReadableException.class) | ||
@ResponseBody | ||
public final ResponseEntity<ErrorMessage> handleMessageNotReadableException( | ||
HttpMessageNotReadableException exception) { | ||
ErrorType errorType = BAD_REQUEST_ERROR; | ||
return new ResponseEntity<>(createErrorMessage(exception,errorType), errorType.getHttpStatus()); | ||
} | ||
|
||
@ExceptionHandler(InvalidFormatException.class) | ||
@ResponseBody | ||
public final ResponseEntity<ErrorMessage> handleInvalidFormatException( | ||
InvalidFormatException exception) { | ||
ErrorType errorType = BAD_REQUEST_ERROR; | ||
return new ResponseEntity<>(createErrorMessage(exception,errorType), errorType.getHttpStatus()); | ||
} | ||
|
||
@ExceptionHandler(DataIntegrityViolationException.class) | ||
@ResponseBody | ||
public final ResponseEntity<ErrorMessage> handlePSQLException( | ||
DataIntegrityViolationException exception) { | ||
ErrorType errorType = REGISTER_KULLANICIADI_KAYITLI; | ||
return new ResponseEntity<>(createErrorMessage(exception,errorType), errorType.getHttpStatus()); | ||
} | ||
|
||
|
||
@ExceptionHandler(MethodArgumentTypeMismatchException.class) | ||
@ResponseBody | ||
public final ResponseEntity<ErrorMessage> handleMethodArgumentMisMatchException( | ||
MethodArgumentTypeMismatchException exception) { | ||
|
||
ErrorType errorType = BAD_REQUEST_ERROR; | ||
return new ResponseEntity<>(createErrorMessage(exception,errorType), errorType.getHttpStatus()); | ||
} | ||
|
||
@ExceptionHandler(MissingPathVariableException.class) | ||
@ResponseBody | ||
public final ResponseEntity<ErrorMessage> handleMethodArgumentMisMatchException( | ||
MissingPathVariableException exception) { | ||
|
||
ErrorType errorType = BAD_REQUEST_ERROR; | ||
return new ResponseEntity<>(createErrorMessage(exception,errorType), errorType.getHttpStatus()); | ||
} | ||
|
||
|
||
@ExceptionHandler(MethodArgumentNotValidException.class) | ||
@ResponseBody | ||
public final ResponseEntity<ErrorMessage> handleMethodArgumentNotValidException( | ||
MethodArgumentNotValidException exception) { | ||
|
||
ErrorType errorType = BAD_REQUEST_ERROR; | ||
List<String> 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(); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
userprofile-microservice/src/main/java/com/bilgeadam/exception/UserprofileExecption.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |