-
Notifications
You must be signed in to change notification settings - Fork 106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[5기 이유정] Shorten-URL 과제 제출합니다. #73
base: letskuku
Are you sure you want to change the base?
Changes from 1 commit
6d407d1
3c793ad
d517cd3
b814cf1
5210ee1
6f18b92
5d60d96
97730f4
a5e45ba
e0b4b3d
c60993b
5000fd3
9b2a411
3747840
a118f9e
9805d36
43ed06e
8719af4
6bfab5f
587c537
cbc13d5
52234fe
802b322
49ca73a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package com.example.urlmanagement.domain; | ||
|
||
import com.example.urlmanagement.exception.EncodeTypeNotFoundException; | ||
import com.example.urlmanagement.exception.UrlErrorCode; | ||
import com.example.urlmanagement.exception.UrlException; | ||
|
||
import java.util.Arrays; | ||
|
||
|
@@ -12,7 +13,8 @@ public enum EncodeType { | |
public static EncodeType getEncodeTypeByName(String name) { | ||
return Arrays.stream(EncodeType.values()) | ||
.filter(encodeType -> encodeType.getName().equalsIgnoreCase(name)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 케이스 고려 좋네요 👍 |
||
.findAny().orElseThrow(() -> new EncodeTypeNotFoundException(name)); | ||
.findAny() | ||
.orElseThrow(() -> new UrlException(UrlErrorCode.ENCODE_TYPE_NOT_FOUND, name)); | ||
} | ||
|
||
private String getName() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package com.example.urlmanagement.domain; | ||
|
||
import com.example.urlmanagement.exception.InvalidUrlException; | ||
import com.example.urlmanagement.exception.UrlErrorCode; | ||
import com.example.urlmanagement.exception.UrlException; | ||
import com.example.urlmanagement.global.common.BaseEntity; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
|
@@ -48,7 +49,8 @@ private void validateOriginalUrl(String originalUrl) { | |
try { | ||
new URL(originalUrl); | ||
} catch (MalformedURLException e) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 다른 Exception의 경우 그대로 던져질 것 같아서 Exception에 대해선 UrlErrorCode에 별도의 INTERNAL_SERVER_ERROR와 같이 정의해서 던져보는 것도 좋을 것 같아요. |
||
throw new InvalidUrlException(originalUrl); | ||
// throw new InvalidUrlException(originalUrl); | ||
throw new UrlException(UrlErrorCode.INVALID_URL, originalUrl); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.example.urlmanagement.exception; | ||
|
||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
public enum UrlErrorCode { | ||
ENCODE_TYPE_NOT_FOUND(HttpStatus.NOT_FOUND, "해당하는 EncodeType이 존재하지 않습니다."), | ||
URL_NOT_FOUND(HttpStatus.NOT_FOUND, "해당하는 url 정보가 존재하지 않습니다."), | ||
INVALID_URL(HttpStatus.BAD_REQUEST, "올바르지 않은 형식의 URL입니다."); | ||
Comment on lines
+8
to
+10
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 나중에 ErrorCode가 HttpStatus에 너무 강결합되지 않는 구조를 고려해봐도 좋을 것 같아요. |
||
|
||
private final HttpStatus errorHttpStatus; | ||
private String errorMessage; | ||
|
||
UrlErrorCode(HttpStatus errorHttpStatus, String errorMessage) { | ||
this.errorHttpStatus = errorHttpStatus; | ||
this.errorMessage = errorMessage; | ||
} | ||
|
||
public void updateErrorMessage(String wrongInput) { | ||
this.errorMessage = this.errorMessage + " : " + wrongInput; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.example.urlmanagement.exception; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class UrlException extends RuntimeException { | ||
|
||
private final UrlErrorCode errorCode; | ||
|
||
public UrlException(UrlErrorCode urlErrorCode, String wrongInput) { | ||
Comment on lines
+8
to
+10
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wrongInput보단 message라고 표현하는게 좋을 것 같네요. |
||
this.errorCode = urlErrorCode; | ||
errorCode.updateErrorMessage(wrongInput); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
package com.example.urlmanagement.presentation; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 요건 presentation보단 exception에 대한 설정에 가까운 것 같아요. |
||
|
||
import com.example.urlmanagement.exception.EncodeTypeNotFoundException; | ||
import com.example.urlmanagement.exception.InvalidUrlException; | ||
import com.example.urlmanagement.exception.UrlNotFoundException; | ||
import com.example.urlmanagement.exception.UrlException; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
|
@@ -13,22 +11,10 @@ | |
@RestControllerAdvice | ||
public class ExceptionController { | ||
|
||
@ExceptionHandler(EncodeTypeNotFoundException.class) | ||
public ResponseEntity<String> catchEncodeTypeNotFoundException(EncodeTypeNotFoundException e) { | ||
log.error(e.getMessage(), e); | ||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage()); | ||
} | ||
|
||
@ExceptionHandler(UrlNotFoundException.class) | ||
public ResponseEntity<String> catchUrlNotFoundException(UrlNotFoundException e) { | ||
log.error(e.getMessage(), e); | ||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage()); | ||
} | ||
|
||
@ExceptionHandler(InvalidUrlException.class) | ||
public ResponseEntity<String> catchInvalidUrlException(InvalidUrlException e) { | ||
log.error(e.getMessage(), e); | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage()); | ||
@ExceptionHandler(UrlException.class) | ||
public ResponseEntity<String> catchCustomUrlException(UrlException e) { | ||
log.error(e.getErrorCode().getErrorMessage(), e); | ||
return ResponseEntity.status(e.getErrorCode().getErrorHttpStatus()).body(e.getErrorCode().getErrorMessage()); | ||
} | ||
|
||
@ExceptionHandler(Exception.class) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
조회는 빈번하게 일어남으로 동시에 요청이 들어오면 업데이트시 동시성 이슈가 발생할 수 있을 것 같아요.
어떻게 해결할 수 있을 것 같은지 간단한 생각 남겨봐주시면 좋을 것 같아요.