-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ Refactor : refactoring errorCode by domain (#415)
* ♻️ Refactor : refactoring errorCode by domain * ✨ feat: change reflection to ClassPathScanningCandidateComponentProvider * ✅ test : add errorcode duplication test
- Loading branch information
1 parent
05732d6
commit e469d5e
Showing
56 changed files
with
871 additions
and
331 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,4 +81,4 @@ test { | |
tasks.named('test') { | ||
useJUnitPlatform() | ||
finalizedBy jacocoTestReport | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
backend/streetdrop-api/src/main/java/com/depromeet/common/error/dto/CommonErrorCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.depromeet.common.error.dto; | ||
|
||
import com.depromeet.common.error.dto.interfaces.ErrorCode; | ||
import com.depromeet.common.error.dto.interfaces.ErrorCodeInterface; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public enum CommonErrorCode implements ErrorCodeInterface { | ||
/* | ||
* Basic Client Error | ||
*/ | ||
BAD_REQUEST(HttpStatus.BAD_REQUEST, "COMMON_BAD_REQUEST", "Bad Request", "The request could not be understood or was missing required parameters."), | ||
METHOD_ARGUMENT_NOT_VALID(HttpStatus.BAD_REQUEST, "COMMON_METHOD_ARGUMENT_NOT_VALID", "Method Argument Not Valid", "One or more method arguments are not valid."), | ||
UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "COMMON_UNAUTHORIZED", "Unauthenticated", "Authentication is required and has failed or has not been provided."), | ||
FORBIDDEN(HttpStatus.FORBIDDEN, "COMMON_FORBIDDEN", "Forbidden", "Access to the requested resource is forbidden."), | ||
NOT_FOUND(HttpStatus.NOT_FOUND, "COMMON_NOT_FOUND", "Not Found", "The requested resource could not be found."), | ||
METHOD_NOT_ALLOWED(HttpStatus.METHOD_NOT_ALLOWED, "COMMON_METHOD_NOT_ALLOWED", "Method Not Allowed", "The method received in the request-line is known by the origin server but not supported."), | ||
CONFLICT(HttpStatus.CONFLICT, "COMMON_CONFLICT", "Conflict", "The request could not be completed due to a conflict with the current state of the target resource."), | ||
|
||
/* | ||
* StreetDrop Common Error | ||
*/ | ||
CANNOT_USE_BANNED_WORD(HttpStatus.BAD_REQUEST, "COMMON_CAN_NOT_USE_BANNED_WORD", "Can Not Use Banned Word", "Cannot Use Banned Word"), | ||
|
||
/* | ||
* Basic Server Error | ||
*/ | ||
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "COMMON_INTERNAL_SERVER_ERROR", "Internal Server Error", "An unexpected error occurred"), | ||
NOT_IMPLEMENTED(HttpStatus.NOT_IMPLEMENTED, "COMMON_NOT_IMPLEMENTED", "Not Implemented", "The server does not support the functionality required to fulfill the request."); | ||
|
||
|
||
private final HttpStatus status; | ||
private final String errorResponseCode; | ||
private final String title; | ||
private final String message; | ||
|
||
@Override | ||
public ErrorCode toErrorCode() { | ||
return ErrorCode | ||
.builder() | ||
.status(status) | ||
.errorResponseCode(errorResponseCode) | ||
.title(title) | ||
.message(message) | ||
.build(); | ||
} | ||
} |
59 changes: 0 additions & 59 deletions
59
backend/streetdrop-api/src/main/java/com/depromeet/common/error/dto/ErrorCode.java
This file was deleted.
Oops, something went wrong.
12 changes: 6 additions & 6 deletions
12
backend/streetdrop-api/src/main/java/com/depromeet/common/error/dto/ErrorCodeMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
package com.depromeet.common.error.dto; | ||
|
||
import com.depromeet.common.error.dto.interfaces.ErrorCode; | ||
|
||
import java.util.Optional; | ||
|
||
public class ErrorCodeMapper { | ||
public static Optional<ErrorCode> findByErrorCode(String code) { | ||
for (ErrorCode errorCode : ErrorCode.values()) { | ||
if (errorCode.getCode().equals(code)) { | ||
return Optional.of(errorCode); | ||
} | ||
} | ||
return Optional.empty(); | ||
var result = StreetDropErrorCodeList.getInstance().getStreetDropErrorCodeList(); | ||
return result.stream().filter(streetDropErrorCode -> streetDropErrorCode.getErrorResponseCode().equals(code)) | ||
.findFirst().map(StreetDropErrorCode::getErrorCode); | ||
} | ||
|
||
} |
25 changes: 0 additions & 25 deletions
25
backend/streetdrop-api/src/main/java/com/depromeet/common/error/dto/ErrorResponseDto.java
This file was deleted.
Oops, something went wrong.
15 changes: 15 additions & 0 deletions
15
backend/streetdrop-api/src/main/java/com/depromeet/common/error/dto/StreetDropErrorCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.depromeet.common.error.dto; | ||
|
||
import com.depromeet.common.error.dto.interfaces.ErrorCode; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@AllArgsConstructor | ||
@Getter | ||
public class StreetDropErrorCode { | ||
|
||
private String errorResponseCode; | ||
|
||
private ErrorCode errorCode; | ||
|
||
} |
69 changes: 69 additions & 0 deletions
69
.../streetdrop-api/src/main/java/com/depromeet/common/error/dto/StreetDropErrorCodeList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.depromeet.common.error.dto; | ||
|
||
import com.depromeet.common.error.dto.interfaces.ErrorCode; | ||
import com.depromeet.common.error.dto.interfaces.ErrorCodeInterface; | ||
import lombok.Getter; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.config.BeanDefinition; | ||
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider; | ||
import org.springframework.core.type.filter.AssignableTypeFilter; | ||
import org.springframework.core.type.filter.TypeFilter; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
@Getter | ||
@Slf4j | ||
public class StreetDropErrorCodeList { | ||
|
||
private static volatile StreetDropErrorCodeList instance; | ||
private final List<StreetDropErrorCode> streetDropErrorCodeList; | ||
|
||
private StreetDropErrorCodeList() { | ||
streetDropErrorCodeList = createStreetDropErrorCodeList(); | ||
} | ||
|
||
public static StreetDropErrorCodeList getInstance(){ | ||
if (instance == null) { | ||
synchronized (StreetDropErrorCodeList.class) { | ||
if (instance == null) { | ||
instance = new StreetDropErrorCodeList(); | ||
} | ||
} | ||
} | ||
return instance; | ||
} | ||
|
||
private synchronized List<StreetDropErrorCode> createStreetDropErrorCodeList() { | ||
List<StreetDropErrorCode> streetDropErrorCodeList = new ArrayList<>(); | ||
|
||
try { | ||
ClassPathScanningCandidateComponentProvider s = new ClassPathScanningCandidateComponentProvider(false); | ||
|
||
TypeFilter tf = new AssignableTypeFilter(ErrorCodeInterface.class); | ||
s.addIncludeFilter(tf); | ||
|
||
Set<BeanDefinition> components = s.findCandidateComponents("com.depromeet"); | ||
|
||
for (BeanDefinition component : components) { | ||
Class<?> className = Class.forName(component.getBeanClassName()); | ||
|
||
if (className.isEnum()) { | ||
for (var errorCode : className.getEnumConstants()) { | ||
if (errorCode != null) { | ||
String errorResponseCode = (String) errorCode.getClass().getMethod("getErrorResponseCode").invoke(errorCode); | ||
ErrorCode error = (ErrorCode) errorCode.getClass().getMethod("toErrorCode").invoke(errorCode); | ||
var streetDropError = new StreetDropErrorCode(errorResponseCode, error); | ||
streetDropErrorCodeList.add(streetDropError); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} catch (Exception ignored) { | ||
} | ||
|
||
return streetDropErrorCodeList; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...end/streetdrop-api/src/main/java/com/depromeet/common/error/dto/interfaces/ErrorCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.depromeet.common.error.dto.interfaces; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
@Builder | ||
public class ErrorCode implements ErrorCodeInterface { | ||
private HttpStatus status; | ||
private String errorResponseCode; | ||
private String title; | ||
private String message; | ||
|
||
@Override | ||
public ErrorCode toErrorCode() { | ||
return this; | ||
} | ||
|
||
public void appendMessage(String additionalMessage) { | ||
this.message += " " + additionalMessage; | ||
} | ||
|
||
} |
Oops, something went wrong.