Skip to content

Commit

Permalink
fix:#91 에러디코더 관련 버그 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
joohyun1996 committed May 9, 2024
1 parent 0b9c458 commit 44a8739
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ public Exception decode(String s, Response response) {
Response.Body responseBody = response.body();
HttpStatus responseStatus = HttpStatus.valueOf(response.status());

if(responseStatus.is4xxClientError()){
return new RestApiClientException(response);
}else{
return new Exception(responseBody.toString());
if(responseStatus.value()==HttpStatus.UNAUTHORIZED.value()){
return new RestApiClientException("Login again");
}
return new Exception(response.reason());
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,10 @@
package com.t3t.frontserver.auth.exception;

import feign.Response;
import lombok.Getter;

import java.io.IOException;

@Getter
public class RestApiClientException extends RuntimeException{
private String responseBody;
private int status;
private String message;

public RestApiClientException(Response response) {
super();
this.status = response.status();
this.message = response.reason();

try{
this.responseBody = response.body() != null ? new String(response.body().asInputStream().readAllBytes()) : null;
} catch (IOException e){
this.responseBody = null;
}

public RestApiClientException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.t3t.frontserver.common.exception;

import com.t3t.frontserver.auth.exception.RestApiClientException;
import feign.FeignException;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
Expand All @@ -21,23 +20,23 @@ public String handleException(RedirectAttributes redirectAttributes, Exception e
return "redirect:/message";
}

@ExceptionHandler(RestApiClientException.class)
public String handleRestApiClientException(Model model, RestApiClientException e, HttpServletResponse response) {
JSONObject json = new JSONObject(e.getResponseBody());
String message = json.getString("message");

if (e.getStatus() == 401 && "Login again".equals(message)) {
Cookie cookie = new Cookie("t3t", null);
cookie.setMaxAge(0);
cookie.setPath("/");
SecurityContextHolder.clearContext();
response.addCookie(cookie);

return "redirect:/";
} else {
model.addAttribute("message", e.getResponseBody());
return "main/page/message";
}
/* @ExceptionHandler(RestApiClientException.class)
public String handleUnAuthorizedException(Model model, RestApiClientException e, HttpServletResponse response) {
Cookie cookie = new Cookie("t3t", null);
cookie.setMaxAge(0);
cookie.setPath("/");
SecurityContextHolder.clearContext();
response.addCookie(cookie);
return "redirect:/login";
}*/

@ExceptionHandler(FeignException.Unauthorized.class)
public String handleLogoutException(Model model, FeignException.Unauthorized e, HttpServletResponse response) {
Cookie cookie = new Cookie("t3t", null);
cookie.setMaxAge(0);
cookie.setPath("/");
SecurityContextHolder.clearContext();
response.addCookie(cookie);
return "redirect:/login";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public RequestInterceptor requestInterceptor() {
}
};
}
@Bean
// @Bean
public ErrorDecoder errorDecoder(){
return new CustomErrorDecoder();
}
Expand Down

0 comments on commit 44a8739

Please sign in to comment.