Repository navigation
공통 API 응답 및 JWT 보안 기반 구축 #9
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
052c575
feat :: 공통 API 응답 및 예외 처리 추가
cfcromn d228a27
feat :: JWT 보안 기반 추가
cfcromn 07933d7
refactor :: 도메인별 오류 코드 분리
cfcromn ad442ed
fix :: 리뷰 의견 반영
cfcromn 5151e09
refactor :: domain과 global 패키지 분리
cfcromn 0ad0405
docs :: 패키지 아키텍처 규칙 추가
cfcromn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,55 @@ | ||
| # Architecture | ||
|
|
||
| ## Package Layout | ||
|
|
||
| ```text | ||
| team.cklob.arena | ||
| ├── domain | ||
| │ └── {domainName} | ||
| │ ├── presentation | ||
| │ │ ├── request | ||
| │ │ ├── response | ||
| │ │ └── controller | ||
| │ ├── application | ||
| │ │ └── impl | ||
| │ ├── domain | ||
| │ │ ├── entity | ||
| │ │ ├── type | ||
| │ │ └── repository | ||
| │ └── infrastructure | ||
| └── global | ||
| ├── common | ||
| ├── config | ||
| ├── security | ||
| ├── exception | ||
| ├── response | ||
| ├── annotation | ||
| ├── util | ||
| └── property | ||
| ``` | ||
|
|
||
| `{domainName}` is one of `user`, `market`, `challenge`, `trading`, `portfolio`, `recommendation`, or `learning`. | ||
|
|
||
| Do not create an empty package. Add a package only when it contains production code. | ||
|
|
||
| ## Domain Layers | ||
|
|
||
| - `presentation`: HTTP controllers and request/response DTOs. Controllers validate input and delegate only to application services. | ||
| - `application`: use cases, orchestration, and transaction boundaries. Place concrete service implementations in `application.impl`. | ||
| - `domain`: entities, domain types, and repository interfaces. Keep domain business rules here when they do not require orchestration or external I/O. | ||
| - `infrastructure`: external API clients, persistence adapters, and other technology-specific implementations. | ||
|
|
||
| Use the package form `team.cklob.arena.domain.{domainName}.{layer}`. A domain may depend on `global`, but one domain must not reach into another domain's `presentation` or `application` package. | ||
|
|
||
| ## Global Packages | ||
|
|
||
| - `common`: cross-cutting types that do not belong to another global concern. | ||
| - `config`: Spring configuration and framework integration setup. | ||
| - `security`: JWT, authentication, authorization, and Spring Security components. | ||
| - `exception`: shared exception contracts, error-code contracts, and global exception handlers. | ||
| - `response`: shared API response envelopes and response advice. | ||
| - `annotation`: reusable annotations and their supporting code. | ||
| - `util`: stateless helpers with no domain ownership. | ||
| - `property`: `@ConfigurationProperties` classes. | ||
|
|
||
| `global` must not contain domain business logic, domain entities, or domain repositories. |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
11 changes: 11 additions & 0 deletions
11
src/main/kotlin/team/cklob/arena/domain/challenge/ChallengeErrorCode.kt
This file contains hidden or 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,11 @@ | ||
| package team.cklob.arena.domain.challenge | ||
|
|
||
| import org.springframework.http.HttpStatus | ||
| import team.cklob.arena.global.exception.ErrorCode | ||
|
|
||
| enum class ChallengeErrorCode( | ||
| override val status: HttpStatus, | ||
| override val message: String, | ||
| ) : ErrorCode { | ||
| CHALLENGE_ALREADY_ENDED(HttpStatus.CONFLICT, "이미 종료된 챌린지입니다."), | ||
| } |
2 changes: 1 addition & 1 deletion
2
.../kotlin/team/cklob/arena/market/Market.kt → .../team/cklob/arena/domain/market/Market.kt
This file contains hidden or 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
This file contains hidden or 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
11 changes: 11 additions & 0 deletions
11
src/main/kotlin/team/cklob/arena/domain/recommendation/RecommendationErrorCode.kt
This file contains hidden or 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,11 @@ | ||
| package team.cklob.arena.domain.recommendation | ||
|
|
||
| import org.springframework.http.HttpStatus | ||
| import team.cklob.arena.global.exception.ErrorCode | ||
|
|
||
| enum class RecommendationErrorCode( | ||
| override val status: HttpStatus, | ||
| override val message: String, | ||
| ) : ErrorCode { | ||
| RECOMMENDATION_ALREADY_REACTED(HttpStatus.CONFLICT, "이미 반응한 추천입니다."), | ||
| } |
This file contains hidden or 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
11 changes: 11 additions & 0 deletions
11
src/main/kotlin/team/cklob/arena/domain/trading/TradingErrorCode.kt
This file contains hidden or 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,11 @@ | ||
| package team.cklob.arena.domain.trading | ||
|
|
||
| import org.springframework.http.HttpStatus | ||
| import team.cklob.arena.global.exception.ErrorCode | ||
|
|
||
| enum class TradingErrorCode( | ||
| override val status: HttpStatus, | ||
| override val message: String, | ||
| ) : ErrorCode { | ||
| INSUFFICIENT_BALANCE(HttpStatus.CONFLICT, "잔액이 부족합니다."), | ||
| } |
2 changes: 1 addition & 1 deletion
2
...main/kotlin/team/cklob/arena/user/User.kt → ...tlin/team/cklob/arena/domain/user/User.kt
This file contains hidden or 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
32 changes: 32 additions & 0 deletions
32
src/main/kotlin/team/cklob/arena/global/config/OpenApiConfig.kt
This file contains hidden or 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,32 @@ | ||
| package team.cklob.arena.global.config | ||
|
|
||
| import io.swagger.v3.oas.models.Components | ||
| import io.swagger.v3.oas.models.OpenAPI | ||
| import io.swagger.v3.oas.models.media.ObjectSchema | ||
| import io.swagger.v3.oas.models.media.StringSchema | ||
| import io.swagger.v3.oas.models.security.SecurityScheme | ||
| import org.springframework.context.annotation.Bean | ||
| import org.springframework.context.annotation.Configuration | ||
| import team.cklob.arena.global.exception.CommonErrorCode | ||
|
|
||
| @Configuration | ||
| class OpenApiConfig { | ||
| @Bean | ||
| fun openApi(): OpenAPI = | ||
| OpenAPI().components( | ||
| Components() | ||
| .addSecuritySchemes( | ||
| "bearerAuth", | ||
| SecurityScheme() | ||
| .type(SecurityScheme.Type.HTTP) | ||
| .scheme("bearer") | ||
| .bearerFormat("JWT"), | ||
| ).addSchemas( | ||
| "CommonErrorResponse", | ||
| ObjectSchema() | ||
| .addProperty("code", StringSchema().example(CommonErrorCode.INVALID_REQUEST.code)) | ||
| .addProperty("message", StringSchema().example(CommonErrorCode.INVALID_REQUEST.message)) | ||
| .addProperty("data", ObjectSchema().nullable(true)), | ||
| ), | ||
| ) | ||
| } |
15 changes: 15 additions & 0 deletions
15
src/main/kotlin/team/cklob/arena/global/exception/CommonErrorCode.kt
This file contains hidden or 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 team.cklob.arena.global.exception | ||
|
|
||
| import org.springframework.http.HttpStatus | ||
|
|
||
| enum class CommonErrorCode( | ||
| override val status: HttpStatus, | ||
| override val message: String, | ||
| ) : ErrorCode { | ||
| INVALID_REQUEST(HttpStatus.BAD_REQUEST, "잘못된 요청입니다."), | ||
| MALFORMED_JSON(HttpStatus.BAD_REQUEST, "요청 본문 형식이 올바르지 않습니다."), | ||
| INVALID_TYPE_VALUE(HttpStatus.BAD_REQUEST, "요청 값의 형식이 올바르지 않습니다."), | ||
| METHOD_NOT_ALLOWED(HttpStatus.METHOD_NOT_ALLOWED, "지원하지 않는 HTTP 메서드입니다."), | ||
| RESOURCE_NOT_FOUND(HttpStatus.NOT_FOUND, "요청한 리소스를 찾을 수 없습니다."), | ||
| INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "서버 오류가 발생했습니다."), | ||
| } |
10 changes: 10 additions & 0 deletions
10
src/main/kotlin/team/cklob/arena/global/exception/ErrorCode.kt
This file contains hidden or 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,10 @@ | ||
| package team.cklob.arena.global.exception | ||
|
|
||
| import org.springframework.http.HttpStatus | ||
|
|
||
| interface ErrorCode { | ||
| val status: HttpStatus | ||
| val message: String | ||
| val code: String | ||
| get() = (this as Enum<*>).name | ||
| } |
6 changes: 6 additions & 0 deletions
6
src/main/kotlin/team/cklob/arena/global/exception/ExpectedException.kt
This file contains hidden or 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,6 @@ | ||
| package team.cklob.arena.global.exception | ||
|
|
||
| class ExpectedException( | ||
| val errorCode: ErrorCode, | ||
| messageOverride: String? = null, | ||
| ) : RuntimeException(messageOverride ?: errorCode.message) |
90 changes: 90 additions & 0 deletions
90
src/main/kotlin/team/cklob/arena/global/exception/GlobalExceptionHandler.kt
This file contains hidden or 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,90 @@ | ||
| package team.cklob.arena.global.exception | ||
|
|
||
| import com.fasterxml.jackson.databind.exc.InvalidFormatException | ||
| import jakarta.validation.ConstraintViolationException | ||
| import org.slf4j.LoggerFactory | ||
| import org.springframework.http.ResponseEntity | ||
| import org.springframework.http.converter.HttpMessageNotReadableException | ||
| import org.springframework.validation.BindException | ||
| import org.springframework.web.HttpRequestMethodNotSupportedException | ||
| import org.springframework.web.bind.MethodArgumentNotValidException | ||
| import org.springframework.web.bind.annotation.ExceptionHandler | ||
| import org.springframework.web.bind.annotation.RestControllerAdvice | ||
| import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException | ||
| import org.springframework.web.servlet.NoHandlerFoundException | ||
| import org.springframework.web.servlet.resource.NoResourceFoundException | ||
| import team.cklob.arena.global.response.CommonApiResponse | ||
| import team.cklob.arena.global.response.FieldErrorDetail | ||
| import team.cklob.arena.global.response.ValidationErrorData | ||
|
|
||
| @RestControllerAdvice | ||
| class GlobalExceptionHandler { | ||
| private val log = LoggerFactory.getLogger(javaClass) | ||
|
|
||
| @ExceptionHandler(ExpectedException::class) | ||
| fun handleExpectedException(exception: ExpectedException): ResponseEntity<CommonApiResponse<Any>> = | ||
| response(exception.errorCode, exception.message ?: exception.errorCode.message) | ||
|
|
||
| @ExceptionHandler(MethodArgumentNotValidException::class, BindException::class) | ||
| fun handleValidationException(exception: BindException): ResponseEntity<CommonApiResponse<Any>> { | ||
| val fieldErrors = | ||
| exception.bindingResult.fieldErrors.map { | ||
| FieldErrorDetail(field = it.field, reason = it.defaultMessage ?: "유효하지 않은 값입니다.") | ||
| } | ||
|
|
||
| return response( | ||
| errorCode = CommonErrorCode.INVALID_REQUEST, | ||
| data = ValidationErrorData(fieldErrors), | ||
| ) | ||
| } | ||
|
|
||
| @ExceptionHandler(ConstraintViolationException::class) | ||
| fun handleConstraintViolation(exception: ConstraintViolationException): ResponseEntity<CommonApiResponse<Any>> = | ||
| response( | ||
| errorCode = CommonErrorCode.INVALID_REQUEST, | ||
| data = | ||
| ValidationErrorData( | ||
| exception.constraintViolations.map { | ||
| FieldErrorDetail(field = it.propertyPath.toString(), reason = it.message) | ||
| }, | ||
| ), | ||
| ) | ||
|
|
||
| @ExceptionHandler(HttpMessageNotReadableException::class) | ||
| fun handleUnreadableMessage(exception: HttpMessageNotReadableException): ResponseEntity<CommonApiResponse<Any>> = | ||
| response( | ||
| if (exception.cause is InvalidFormatException) { | ||
| CommonErrorCode.INVALID_TYPE_VALUE | ||
| } else { | ||
| CommonErrorCode.MALFORMED_JSON | ||
| }, | ||
| ) | ||
|
|
||
| @ExceptionHandler(MethodArgumentTypeMismatchException::class) | ||
| fun handleTypeMismatch(exception: MethodArgumentTypeMismatchException): ResponseEntity<CommonApiResponse<Any>> = | ||
| response(CommonErrorCode.INVALID_TYPE_VALUE) | ||
|
|
||
| @ExceptionHandler(HttpRequestMethodNotSupportedException::class) | ||
| fun handleMethodNotSupported(exception: HttpRequestMethodNotSupportedException): ResponseEntity<CommonApiResponse<Any>> = | ||
| response(CommonErrorCode.METHOD_NOT_ALLOWED) | ||
|
|
||
| @ExceptionHandler(NoHandlerFoundException::class, NoResourceFoundException::class) | ||
| fun handleNotFound(exception: Exception): ResponseEntity<CommonApiResponse<Any>> { | ||
| return response(CommonErrorCode.RESOURCE_NOT_FOUND) | ||
| } | ||
|
|
||
| @ExceptionHandler(Exception::class) | ||
| fun handleUnexpectedException(exception: Exception): ResponseEntity<CommonApiResponse<Any>> { | ||
| log.error("Unhandled exception", exception) | ||
| return response(CommonErrorCode.INTERNAL_SERVER_ERROR) | ||
| } | ||
|
|
||
| private fun response( | ||
| errorCode: ErrorCode, | ||
| message: String = errorCode.message, | ||
| data: Any? = null, | ||
| ): ResponseEntity<CommonApiResponse<Any>> = | ||
| ResponseEntity | ||
| .status(errorCode.status) | ||
| .body(CommonApiResponse.error(errorCode, message, data)) | ||
| } | ||
38 changes: 38 additions & 0 deletions
38
src/main/kotlin/team/cklob/arena/global/response/ApiResponse.kt
This file contains hidden or 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,38 @@ | ||
| package team.cklob.arena.global.response | ||
|
|
||
| import team.cklob.arena.global.exception.ErrorCode | ||
|
|
||
| data class CommonApiResponse<T>( | ||
| val code: String, | ||
| val message: String, | ||
| val data: T? = null, | ||
| ) { | ||
| companion object { | ||
| fun <T> success(data: T): CommonApiResponse<T> = | ||
| CommonApiResponse( | ||
| code = "SUCCESS", | ||
| message = "성공했습니다.", | ||
| data = data, | ||
| ) | ||
|
|
||
| fun error( | ||
| errorCode: ErrorCode, | ||
| message: String = errorCode.message, | ||
| data: Any? = null, | ||
| ): CommonApiResponse<Any> = | ||
| CommonApiResponse( | ||
| code = errorCode.code, | ||
| message = message, | ||
| data = data, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| data class FieldErrorDetail( | ||
| val field: String, | ||
| val reason: String, | ||
| ) | ||
|
|
||
| data class ValidationErrorData( | ||
| val fieldErrors: List<FieldErrorDetail>, | ||
| ) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.