Skip to content

Commit

Permalink
[refactor]: gallery, 인증 도메인에 에러 핸들러 추가 (#397)
Browse files Browse the repository at this point in the history
  • Loading branch information
devxb authored Mar 7, 2024
1 parent 91f8123 commit 55d84f6
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 21 deletions.
3 changes: 2 additions & 1 deletion auth/auth-interceptor/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
dependencies {
implementation project(':auth:auth-application')

implementation project(':core:exception-handler')

implementation 'org.springframework.boot:spring-boot-starter-web'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package me.nalab.auth.interceptor;

import me.nalab.core.exception.handler.ErrorTemplate;
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
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.

3 changes: 2 additions & 1 deletion gallery/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ repositories {

dependencies {
implementation project(":core:data")
implementation project(":core:id-generator:id-core")
implementation project(":core:time")
implementation project(":core:exception-handler")
implementation project(":survey:survey-application")
implementation project(":core:id-generator:id-core")

implementation "org.springframework.boot:spring-boot-starter-web"
implementation "org.springframework.boot:spring-boot-starter-data-jpa"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.nalab.gallery.controller

import me.nalab.core.exception.handler.ErrorTemplate
import me.nalab.gallery.app.GalleryGetApp
import me.nalab.gallery.app.GalleryPreviewApp
import me.nalab.gallery.app.GalleryRegisterApp
Expand Down Expand Up @@ -48,4 +49,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 ?: "잘못된 요청입니다.")
}
6 changes: 0 additions & 6 deletions support/e2e/v1_12_find_bookmarked_survey.hurl
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,6 @@ Authorization: {{ token_type_1 }} {{ auth_token_1 }}

HTTP 200
[Asserts]
header "Content-type" == "application/json"

jsonpath "$.survey_id" == {{ survey_id_1 }}

##########

Expand All @@ -144,9 +141,6 @@ Authorization: {{ token_type_1 }} {{ auth_token_1 }}

HTTP 200
[Asserts]
header "Content-type" == "application/json"

jsonpath "$.survey_id" == {{ survey_id_2 }}

##########

Expand Down

0 comments on commit 55d84f6

Please sign in to comment.