Skip to content

Commit

Permalink
Merge pull request #21 from 2023-FINAL-PROJECT/fix-http-status
Browse files Browse the repository at this point in the history
fix: fix error code not working issue
  • Loading branch information
chaerin00 authored Aug 18, 2023
2 parents f06599b + 1750d71 commit 99e3ab4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.finalproject.kdiary.common.dto.ApiResponse;
import com.finalproject.kdiary.exception.model.CustomException;
import com.finalproject.kdiary.exception.ErrorStatus;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
Expand All @@ -11,13 +13,13 @@
class ControllerExceptionAdvice {

@ExceptionHandler(CustomException.class)
protected ApiResponse handleCustomException(final CustomException error) {
return ApiResponse.error(error.getErrorCode());
protected ResponseEntity<ApiResponse> handleCustomException(final CustomException error) {
return ResponseEntity.status(error.getErrorCode().getHttpStatus()).body(ApiResponse.error(error.getErrorCode()));
}

@ExceptionHandler(MethodArgumentNotValidException.class)
protected ApiResponse handleMethodArgumentNotValidException(final MethodArgumentNotValidException e) {
return ApiResponse.error(ErrorStatus.BAD_REQUEST);
protected ResponseEntity<ApiResponse> handleMethodArgumentNotValidException(final MethodArgumentNotValidException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ApiResponse.error(ErrorStatus.BAD_REQUEST));
}

// @ExceptionHandler(RuntimeException.class)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.finalproject.kdiary.config.resolver;

import com.finalproject.kdiary.exception.ErrorStatus;
import com.finalproject.kdiary.exception.model.CustomException;
import com.finalproject.kdiary.exception.model.UnauthorizedException;
import lombok.RequiredArgsConstructor;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
Expand All @@ -26,7 +29,12 @@ public boolean supportsParameter(MethodParameter parameter) {
@Override
public Object resolveArgument(@NotNull MethodParameter parameter, ModelAndViewContainer modelAndViewContainer, @NotNull NativeWebRequest webRequest, WebDataBinderFactory binderFactory) {
final HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest();
final String token = request.getHeader("Authorization").split(" ")[1];
String token;
try {
token = request.getHeader("Authorization").split(" ")[1];
} catch (NullPointerException e) {
throw new CustomException(ErrorStatus.NOT_LOGIN);
}

System.out.println(token);
// 토큰 검증
Expand Down

0 comments on commit 99e3ab4

Please sign in to comment.