Skip to content

Commit

Permalink
refactor: gallery도메인에 예외 핸들러를 추가하고, 인증 실패시 예외 메시지를 던지도록 수정한다
Browse files Browse the repository at this point in the history
  • Loading branch information
devxb committed Mar 7, 2024
1 parent 91f8123 commit db5f3bc
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package me.nalab.auth.interceptor;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class AuthExceptionAdvice {

@ExceptionHandler(CannotValidTokenException.class)
@ResponseStatus(HttpStatus.UNAUTHORIZED)
public ErrorTemplate handleCannotValidTokenException(CannotValidTokenException exception) {
return ErrorTemplate.of(exception.getMessage());
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package me.nalab.auth.interceptor;

public class CannotValidTokenException extends RuntimeException {

public CannotValidTokenException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package me.nalab.auth.interceptor;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;

@Getter
public class ErrorTemplate {

@JsonProperty("response_messages")
private final String message;

private ErrorTemplate(String message) {
this.message = message;
}

public static ErrorTemplate of(String message) {
return new ErrorTemplate(message);
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,20 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
if (!isExcludedURI(request)) {
String token = request.getHeader("Authorization");
throwIfCannotValidToken(token);
Long targetId = targetIdGetPort.getTargetId(token.split(" ")[1]);
Long targetId = getTargetId(token);
request.setAttribute("logined", targetId);
}
return true;
}

private Long getTargetId(String token) {
try {
return targetIdGetPort.getTargetId(token.split(" ")[1]);
} catch (Exception exception) {
throw new CannotValidTokenException(exception.getMessage());
}
}

private boolean isPreflight(HttpServletRequest request) {
return request.getMethod().equals("OPTIONS");
}
Expand Down Expand Up @@ -63,7 +71,7 @@ private boolean isExcludedURI(HttpServletRequest httpServletRequest) {

private void throwIfCannotValidToken(String token) {
if (token == null) {
throw new CannotValidMockTokenException();
throw new CannotValidTokenException("Null token");
}
}

Expand Down
7 changes: 0 additions & 7 deletions auth/auth-interceptor/src/main/java/module-info.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package me.nalab.gallery.controller

import com.fasterxml.jackson.annotation.JsonProperty

class ErrorTemplate(
@JsonProperty("response_messages")
private val message: String,
) {
companion object {
fun of(message: String): ErrorTemplate {
return ErrorTemplate(message)
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,8 @@ class GalleryController(
return galleryGetApp.getGalleries(job, page, count, orderType)
}

@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(IllegalArgumentException::class)
fun handleIllegalArgumentException(exception: IllegalArgumentException): ErrorTemplate =
ErrorTemplate.of(exception.message ?: "잘못된 요청입니다.")
}

0 comments on commit db5f3bc

Please sign in to comment.