Skip to content

Commit

Permalink
fix: 실패 응답 객체 Map으로 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
maiload committed Sep 6, 2024
1 parent f398a9f commit 04aa029
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,31 @@
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

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

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler
public ResponseEntity<Object> handleBlockedTokenException(MethodArgumentNotValidException e) {
log.error(e.getMessage(), e);
return FailureResponse.badRequest(Error.INVALID_INPUT);
Error error = Error.INVALID_INPUT;
return FailureResponse.badRequest(error.getHttpStatus(), alterEnumToResponse(error));
}

@ExceptionHandler
public ResponseEntity<Object> handleBlockedTokenException(JwtTokenException e) {
log.error(e.getError().name(), e);
return FailureResponse.unAuthorized(e.getError());
Error error = e.getError();
log.error(error.name(), e);
return FailureResponse.unAuthorized(error.getHttpStatus(), alterEnumToResponse(error));
}

private Map<String, Object> alterEnumToResponse(Error error) {
Map<String, Object> response = new HashMap<>();
response.put("detailCode", error.getDetailCode());
response.put("message", error.name());
return response;
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package ktb.hackathon.ktbgratitudediary.response;

import ktb.hackathon.ktbgratitudediary.exception.Error;
import org.springframework.http.ResponseEntity;

public class FailureResponse {
public static ResponseEntity<Object> badRequest(Error error){
return ResponseEntity.status(error.getHttpStatus()).body(error);
public static ResponseEntity<Object> badRequest(int httpStatus, Object o){
return ResponseEntity.status(httpStatus).body(o);
}

public static ResponseEntity<Object> unAuthorized(Error error){
return ResponseEntity.status(error.getHttpStatus()).body(error);
public static ResponseEntity<Object> unAuthorized(int httpStatus, Object o){
return ResponseEntity.status(httpStatus).body(o);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ public CorsConfigurationSource corsConfigurationSource() {
configuration.setAllowedOrigins(List.of("*")); // 허용할 출처 설정
configuration.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE", "OPTIONS")); // 허용할 메서드 설정
configuration.setAllowedHeaders(List.of("Authorization", "Content-Type")); // 허용할 헤더 설정
// configuration.setAllowCredentials(true); // 쿠키 등 자격 증명 허용
configuration.setAllowCredentials(false);
configuration.setAllowCredentials(true); // 쿠키 등 자격 증명 허용
// configuration.setAllowCredentials(false);

UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration); // 모든 경로에 대해 CORS 설정 적용
Expand Down

0 comments on commit 04aa029

Please sign in to comment.