Gradle Script (Kotlin)
repositories {
maven { url = uri("https://jitpack.io") } // added
}
dependencies {
implementation("com.github.merge-simpson:letsdev-error-code-api:0.2.0") // added
}
이 라이브러리는 다음 기능을 제공합니다.
하위 섹션에서 더 구체적으로 소개합니다.
(업데이트 예정)
- 에러코드 (인터페이스)
- 커스텀 예외
- 예외 처리 시 연계할 동작을 지정할 수 있습니다.
에러코드 구현 용례
%%{init: {"theme": "forest", "themeVariables": {"fontFamily": "Comic Sans MS"}}}%%
classDiagram
%% class list
class ErrorCode {
<<interface>>
}
class BoardErrorCode {
<<enumeration>>
}
class SignUpErrorCode {
<<enumeration>>
}
%% relationship
ErrorCode <|.. BoardErrorCode : implements
ErrorCode <|.. SignUpErrorCode : implements
BaseErrorCode
<<interface>>
BaseCustomException
<<exception>>
%%{init: {"theme": "forest", "themeVariables": {"fontFamily": "Comic Sans MS"}}}%%
classDiagram
%% 클래스 목록
class BaseErrorCode {
<<interface>>
+name() String
+message() String
+statusCode() int
+exception() RuntimeException
+exception(Throwable) RuntimeException
+exception(Runnable) RuntimeException
+exception(Runnable, Throwable) RuntimeException
+exception(Supplier<Map<...>>) RuntimeException
+exception(Supplier<Map<...>>, Throwable) RuntimeException
}
class BaseCustomException {
<<exception>>
#errorCode: BaseErrorCode
#action: Runnable
#payloadSupplier: Supplier<Map<...>>
$DefaultBaseErrorCodeHolder.INSTANCE : BaseErrorCode
+getErrorCode() BaseErrorCode
+executeOnError() void
+getPayload() Map<...>
+getPayloadOrElse(Map<...>) Map<...>
+getPayloadOrElseGet(() -> Map<...>) Map<...>
}
%% 관계 표현
BaseCustomException ..> BaseErrorCode : uses
BaseErrorCode
는 에러 코드를 대표하는 최상위 인터페이스로 설계하였습니다.
이 인터페이스를 구현하면 오류 메시지, 상태 코드 및 관련 예외, 연계되는 동작 등을 제공합니다.
메서드 목록
name()
: 에러 코드의 이름을 반환합니다.참고 사항:
enum
클래스에서 기본적으로 제공하는 함수이므로,enum
에서 이 인터페이스를 구현 시 생략해도 됩니다.message()
: 오류 메시지를 반환합니다.statusCode()
: 상태 코드를 정수로 반환합니다. (이 클래스는 스프링에 독립적이기 때문에 정수를 반환합니다.)exception()
: 관련성이 높은 기본 예외를 반환합니다.exception(Throwable)
: 관련성이 높은 기본 예외를 반환합니다. 이때, 이 오류를 야기한 오류를 스택에 담습니다.exception(Runnable)
: 이 오류를 처리할 때 함께 수행할 동작을 전달하며, 예외를 반환합니다.exception(Runnable, Throwable)
: 위 메서드와 거의 같은 역할을 수행하며, 동시에 이 오류를 야기한 오류를 스택에 담습니다.exception(Supplier<Map<String, Object>>)
: 이 오류를 처리할 때 함께 수행할 동작이자 확장 멤버를 반환하는 함수를 전달하며, 예외를 반환합니다.exception(Supplier<Map<String, Object>>, Throwable)
: 위 메서드와 거의 같은 역할을 수행하며, 동시에 이 오류를 야기한 오류를 스택에 담습니다.
주요 기능 요약
- 에러 코드를 핸들링합니다.
- 오류 처리 시 동반되는 동작을 관리합니다.
- 오류 처리 시 추가되는 확장 멤버를 관리합니다.
생성자
메서드
ErrorCode
:<<interface>>
CustomException
<<exception>>
위 목록을 사용하려면 다음 의존성을 포함해야 합니다.
버전은 자유롭게 선택하십시오.
Group | Artifact ID | Version |
---|---|---|
org.springframework | spring-web | any |
- 각 의존성 라이브러리에서
org.springframework.http.HttpStatus
를 포함하거나 호환되는 버전이 필요합니다.
Generalization Relationship
%%{init: {"theme": "forest", "themeVariables": {"fontFamily": "Comic Sans MS"}}}%%
classDiagram
%% 클래스 목록
class BaseErrorCode {
<<interface>>
}
class BaseCustomException {
<<exception>>
}
class ErrorCode {
<<interface>>
+httpStatus() HttpStatus
}
class CustomException {
<<exception>>
}
%% 관계 표현
BaseErrorCode --|> ErrorCode
BaseCustomException --|> CustomException