Skip to content

Commit

Permalink
user-profile exception handler
Browse files Browse the repository at this point in the history
  • Loading branch information
mertycom committed Jan 31, 2023
1 parent 13ffccd commit 77a355f
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 0 deletions.
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;
}
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;
}
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();
}
}
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;
}

}

0 comments on commit 77a355f

Please sign in to comment.