Skip to content

Commit

Permalink
Merge pull request #80 from TRIP-Side-Project/feature/#77-exception
Browse files Browse the repository at this point in the history
예외 전역 처리 구조 세팅
  • Loading branch information
gkfktkrh153 authored Dec 18, 2023
2 parents f6ac140 + a031537 commit 549f716
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/main/java/com/api/trip/common/exception/CustomException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.api.trip.common.exception;

import lombok.Getter;

@Getter
public abstract class CustomException extends RuntimeException {

protected CustomException(ErrorCode errorCode){
this.errorCode = errorCode;
}
private ErrorCode errorCode;


}
20 changes: 20 additions & 0 deletions src/main/java/com/api/trip/common/exception/ErrorCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.api.trip.common.exception;

import lombok.Getter;
import org.springframework.http.HttpStatus;

@Getter
public enum ErrorCode {

NOT_FOUND_MEMBER("회원이 존재하지 않습니다.", HttpStatus.NOT_FOUND);

private final String message;
private final HttpStatus status;

ErrorCode(String message, HttpStatus status) {
this.message = message;
this.status = status;
}


}
12 changes: 12 additions & 0 deletions src/main/java/com/api/trip/common/exception/ErrorResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.api.trip.common.exception;

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class ErrorResponse{
private String errorMessage;


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.api.trip.common.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
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;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

/**
* 서비스 로직 도중 발생하는 에러들을 커스텀하여 응답값을 내려줍니다.
*/
@ExceptionHandler(CustomException.class)
public ResponseEntity<ErrorResponse> handleCustomException(CustomException e){
ErrorCode errorCode = e.getErrorCode();
log.error(errorCode.getMessage());
return ResponseEntity.status(errorCode.getStatus()).body( new ErrorResponse(errorCode.getMessage()));
}

/**
* 바인딩 에러 json형식으로 에러가 있는 파라미터 값에 메시지를 담아 넘겨줍니다.
* itemPrice : 아이템 가격을 100원 이상 1,000,000,000원 이하여야 합니다.
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<Map<String, String>> handleValidationExceptions(MethodArgumentNotValidException ex){
Map<String, String> errors = new HashMap<>();
ex.getBindingResult().getAllErrors()
.forEach(c -> errors.put(((FieldError) c).getField(), c.getDefaultMessage()));
log.error(ex.getMessage());
return ResponseEntity.badRequest().body(errors);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.api.trip.common.exception.custom_exception;

import com.api.trip.common.exception.CustomException;
import com.api.trip.common.exception.ErrorCode;

public class NotFoundException extends CustomException {
public NotFoundException(ErrorCode errorCode) {
super(errorCode);
}
}

0 comments on commit 549f716

Please sign in to comment.