From f2ea2b8dd3f9f928e78607c6da377bee8ec30fa7 Mon Sep 17 00:00:00 2001 From: Cho jiwon <77895305+Jiwon-cho@users.noreply.github.com> Date: Fri, 1 Mar 2024 00:50:34 +0900 Subject: [PATCH] =?UTF-8?q?MARA-88=20:=20=ED=99=98=EA=B2=BD=EB=B3=84=20?= =?UTF-8?q?=EB=A6=AC=EB=8B=A4=EC=9D=B4=EB=A0=89=ED=8A=B8=20uri=20=EA=B5=AC?= =?UTF-8?q?=EB=B6=84=EC=9D=84=20=EC=9C=84=ED=95=9C=20status=20=EA=B0=92=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80,=20=EB=82=98=EB=88=94=20=EB=B3=80=EC=88=98?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80=20=EB=B0=8F=20=EC=88=98=EC=A0=95,=20?= =?UTF-8?q?=EC=A0=84=EC=97=AD=20=EC=98=88=EC=99=B8=20=EC=B2=98=EB=A6=AC=20?= =?UTF-8?q?=EB=B0=8F=20=EC=BB=A4=EC=8A=A4=ED=85=80=20=EC=98=88=EC=99=B8=20?= =?UTF-8?q?=ED=81=B4=EB=9E=98=EC=8A=A4=20=EC=B6=94=EA=B0=80=20(#49)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: redirect Uri 변경을 위한 status 값 추가 * refactor: status enum 추가 , customException 추가, 나눔 변수 수정,추가 * refactor: ex message 변경 * refactor: error response 이름 변경, ex message 변경, status enum 공통화 * refactor: formatting * refactor: add exception data * refactor: add exception data * 예외 처리 코드 리팩토링 * exception 폴더 구조 변경 및 custom exception 추가 * S3 Custom Exception 추가 * Http 403 status Handling 예제 추가 * refactor: exception 처리 공통 클래스로 처리 --------- Co-authored-by: blackswitch Co-authored-by: gwon188 --- .../kotlin/mara/server/auth/ClientConfig.kt | 6 ++++ .../server/auth/google/GoogleApiClient.kt | 26 ++++++++++------- .../mara/server/auth/kakao/KakaoApiClient.kt | 27 ++++++++++------- .../mara/server/common/CommonResponse.kt | 11 +++++++ .../ingredient/IngredientDetailService.kt | 5 ++-- .../refrigerator/RefrigeratorService.kt | 11 ++++--- .../kotlin/mara/server/domain/s3/S3Service.kt | 4 ++- .../mara/server/domain/share/ShareDto.kt | 14 +++++---- .../mara/server/domain/share/ShareService.kt | 29 ++++++++++++------- .../mara/server/domain/user/UserController.kt | 8 ++--- .../mara/server/domain/user/UserService.kt | 8 ++--- .../exception/GlobalExceptionHandler.kt | 26 +++++++++++++++++ .../server/exception/RefrigeratorException.kt | 7 +++++ .../mara/server/exception/S3Exception.kt | 9 ++++++ .../mara/server/exception/ShareException.kt | 17 +++++++++++ .../mara/server/exception/UserException.kt | 13 +++++++++ 16 files changed, 170 insertions(+), 51 deletions(-) create mode 100644 src/main/kotlin/mara/server/exception/GlobalExceptionHandler.kt create mode 100644 src/main/kotlin/mara/server/exception/RefrigeratorException.kt create mode 100644 src/main/kotlin/mara/server/exception/S3Exception.kt create mode 100644 src/main/kotlin/mara/server/exception/ShareException.kt create mode 100644 src/main/kotlin/mara/server/exception/UserException.kt diff --git a/src/main/kotlin/mara/server/auth/ClientConfig.kt b/src/main/kotlin/mara/server/auth/ClientConfig.kt index 7ae779d..83bc507 100644 --- a/src/main/kotlin/mara/server/auth/ClientConfig.kt +++ b/src/main/kotlin/mara/server/auth/ClientConfig.kt @@ -11,3 +11,9 @@ class ClientConfig { return RestTemplate() } } + +enum class DeployStatus(val uri: String) { + LOCAL(""), + DEV("http://localhost:3000/login"), + PROD("https://fridgelink.site/login") +} diff --git a/src/main/kotlin/mara/server/auth/google/GoogleApiClient.kt b/src/main/kotlin/mara/server/auth/google/GoogleApiClient.kt index 9978a2f..eec56d9 100644 --- a/src/main/kotlin/mara/server/auth/google/GoogleApiClient.kt +++ b/src/main/kotlin/mara/server/auth/google/GoogleApiClient.kt @@ -1,6 +1,8 @@ package mara.server.auth.google -import mara.server.util.logger +import mara.server.auth.DeployStatus +import mara.server.exception.InvalidDeployStatusException +import mara.server.exception.InvalidDeployStatusException.Companion.INVALID_DEPLOY_STATUS import org.springframework.beans.factory.annotation.Value import org.springframework.http.HttpEntity import org.springframework.http.HttpHeaders @@ -10,6 +12,7 @@ import org.springframework.stereotype.Component import org.springframework.util.LinkedMultiValueMap import org.springframework.util.MultiValueMap import org.springframework.web.client.RestTemplate +import java.util.Locale @Component class GoogleApiClient( @@ -28,16 +31,19 @@ class GoogleApiClient( ) { - val log = logger() - - fun getRedirectUri(): String { - val os = System.getProperty("os.name") - log.info("OS : {}", os) - if (os.contains("Mac") || os.contains("Windows")) return "http://localhost:8080/users/google-login" - return "http://localhost:3000/login" + fun getRedirectUri(status: String): String { + val deployStatus = try { + DeployStatus.valueOf(status.uppercase(Locale.getDefault())) + } catch (e: IllegalArgumentException) { + throw InvalidDeployStatusException("$INVALID_DEPLOY_STATUS Status: $status") + } + return when (deployStatus) { + DeployStatus.LOCAL -> "http://localhost:8080/users/google-login" + else -> deployStatus.uri + } } - fun requestAccessToken(authorizedCode: String): String { + fun requestAccessToken(authorizedCode: String, status: String): String { val url = "$authUrl/token" val httpHeaders = HttpHeaders() httpHeaders.contentType = MediaType.APPLICATION_FORM_URLENCODED @@ -46,7 +52,7 @@ class GoogleApiClient( body.add("grant_type", "authorization_code") body.add("client_id", clientId) body.add("client_secret", secret) - body.add("redirect_uri", getRedirectUri()) + body.add("redirect_uri", getRedirectUri(status)) val request = HttpEntity(body, httpHeaders) diff --git a/src/main/kotlin/mara/server/auth/kakao/KakaoApiClient.kt b/src/main/kotlin/mara/server/auth/kakao/KakaoApiClient.kt index debe8f4..c8f4699 100644 --- a/src/main/kotlin/mara/server/auth/kakao/KakaoApiClient.kt +++ b/src/main/kotlin/mara/server/auth/kakao/KakaoApiClient.kt @@ -1,6 +1,8 @@ package mara.server.auth.kakao -import mara.server.util.logger +import mara.server.auth.DeployStatus +import mara.server.exception.InvalidDeployStatusException +import mara.server.exception.InvalidDeployStatusException.Companion.INVALID_DEPLOY_STATUS import org.springframework.beans.factory.annotation.Value import org.springframework.http.HttpEntity import org.springframework.http.HttpHeaders @@ -9,6 +11,7 @@ import org.springframework.stereotype.Component import org.springframework.util.LinkedMultiValueMap import org.springframework.util.MultiValueMap import org.springframework.web.client.RestTemplate +import java.util.Locale @Component class KakaoApiClient( @@ -29,16 +32,20 @@ class KakaoApiClient( private val appAdminKey: String, ) { - val log = logger() - - fun getRedirectUri(): String { - val os = System.getProperty("os.name") - log.info("OS : {}", os) - if (os.contains("Mac") || os.contains("Windows")) return "http://localhost:8080/users/kakao-login" - return "http://localhost:3000/login" + fun getRedirectUri(status: String): String { + val deployStatus = try { + DeployStatus.valueOf(status.uppercase(Locale.getDefault())) + } catch (e: IllegalArgumentException) { + throw InvalidDeployStatusException("$INVALID_DEPLOY_STATUS Status: $status") + } + + return when (deployStatus) { + DeployStatus.LOCAL -> "http://localhost:8080/users/kakao-login" + else -> deployStatus.uri + } } - fun requestAccessToken(authorizedCode: String): String { + fun requestAccessToken(authorizedCode: String, status: String): String { val url = "$authUrl/oauth/token" val httpHeaders = HttpHeaders() @@ -48,7 +55,7 @@ class KakaoApiClient( body.add("grant_type", "authorization_code") body.add("client_id", clientId) body.add("client_secret", secret) - body.add("redirect_uri", getRedirectUri()) + body.add("redirect_uri", getRedirectUri(status)) val request = HttpEntity(body, httpHeaders) diff --git a/src/main/kotlin/mara/server/common/CommonResponse.kt b/src/main/kotlin/mara/server/common/CommonResponse.kt index 05e32ef..23939a8 100644 --- a/src/main/kotlin/mara/server/common/CommonResponse.kt +++ b/src/main/kotlin/mara/server/common/CommonResponse.kt @@ -1,8 +1,19 @@ package mara.server.common +import org.springframework.http.HttpStatus +import org.springframework.http.ResponseEntity + data class CommonResponse( var message: String = "ok", var data: T? = null, ) +data class ErrorResponse( + var message: String +) + fun success(data: T? = null): CommonResponse = CommonResponse(data = data) + +fun fail(httpStatus: HttpStatus, message: String): ResponseEntity = ResponseEntity + .status(httpStatus) + .body(ErrorResponse(message = message)) diff --git a/src/main/kotlin/mara/server/domain/ingredient/IngredientDetailService.kt b/src/main/kotlin/mara/server/domain/ingredient/IngredientDetailService.kt index c7adc65..90f0701 100644 --- a/src/main/kotlin/mara/server/domain/ingredient/IngredientDetailService.kt +++ b/src/main/kotlin/mara/server/domain/ingredient/IngredientDetailService.kt @@ -2,6 +2,7 @@ package mara.server.domain.ingredient import mara.server.domain.refrigerator.RefrigeratorRepository import mara.server.domain.refrigerator.RefrigeratorService +import mara.server.exception.RefrigeratorException.Companion.NO_SUCH_REFRIGERATOR import org.springframework.data.domain.Page import org.springframework.data.domain.Pageable import org.springframework.stereotype.Service @@ -22,7 +23,7 @@ class IngredientDetailService( fun createIngredientDetail(ingredientDetailRequest: IngredientDetailRequest): Long { val refrigeratorId = ingredientDetailRequest.refrigeratorId val refrigerator = refrigeratorRepository.findById(refrigeratorId) - .orElseThrow { NoSuchElementException("해당 냉장고가 존재하지 않습니다. ID: $refrigeratorId") } + .orElseThrow { NoSuchElementException(NO_SUCH_REFRIGERATOR) } val ingredientId = ingredientDetailRequest.ingredientId val ingredient = ingredientRepository.findById(ingredientId) @@ -55,7 +56,7 @@ class IngredientDetailService( fun getIngredientDetailList(refrigeratorId: Long, location: IngredientLocation, pageable: Pageable): Page { val refrigerator = refrigeratorRepository.findById(refrigeratorId) - .orElseThrow { NoSuchElementException("해당 냉장고가 존재하지 않습니다. ID: $refrigeratorId") } + .orElseThrow { NoSuchElementException(NO_SUCH_REFRIGERATOR) } val ingredientDetailList = ingredientDetailRepository.findByRefrigerator(refrigerator, location, pageable) return ingredientDetailList.toIngredientDetailResponseListPage() diff --git a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorService.kt b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorService.kt index 8c62349..727df39 100644 --- a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorService.kt +++ b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorService.kt @@ -2,6 +2,8 @@ package mara.server.domain.refrigerator import mara.server.domain.user.UserRepository import mara.server.domain.user.UserService +import mara.server.exception.RefrigeratorException.Companion.NO_SUCH_REFRIGERATOR +import mara.server.exception.UserException.Companion.NO_SUCH_USER import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional @@ -13,7 +15,8 @@ class RefrigeratorService( ) { private val deleted = "deleted" - fun getCurrentLoginUserRefrigeratorList(): List = refrigeratorRepository.findByUser(userService.getCurrentLoginUser()) + fun getCurrentLoginUserRefrigeratorList(): List = + refrigeratorRepository.findByUser(userService.getCurrentLoginUser()) @Transactional fun createRefrigerator(refrigeratorRequest: RefrigeratorRequest): Long { @@ -29,12 +32,12 @@ class RefrigeratorService( // TODO : FE API 연동 테스트 이후 삭제 예정 // fun getRefrigerator(id: Long): RefrigeratorResponse { // val refrigerator = -// refrigeratorRepository.findById(id).orElseThrow { NoSuchElementException("해당 냉장고가 존재하지 않습니다. ID: $id") } +// refrigeratorRepository.findById(id).orElseThrow { NO_SUCH_REFRIGERATOR } // return RefrigeratorResponse(refrigerator) // } fun getRefrigeratorList(userId: Long): List { - val user = userRepository.findById(userId).orElseThrow { NoSuchElementException("해당 유저가 존재하지 않습니다. ID: $userId") } + val user = userRepository.findById(userId).orElseThrow { NoSuchElementException(NO_SUCH_USER) } val refrigeratorList = refrigeratorRepository.findByUser(user) return refrigeratorList.toRefrigeratorResponseList() } @@ -47,7 +50,7 @@ class RefrigeratorService( @Transactional fun updateRefrigerator(id: Long, refrigeratorRequest: RefrigeratorRequest): RefrigeratorResponse { val refrigerator = - refrigeratorRepository.findById(id).orElseThrow { NoSuchElementException("해당 냉장고가 존재하지 않습니다. ID: $id") } + refrigeratorRepository.findById(id).orElseThrow { NoSuchElementException(NO_SUCH_REFRIGERATOR) } refrigerator.update(refrigeratorRequest) return RefrigeratorResponse(refrigeratorRepository.save(refrigerator)) } diff --git a/src/main/kotlin/mara/server/domain/s3/S3Service.kt b/src/main/kotlin/mara/server/domain/s3/S3Service.kt index c936211..4b9912b 100644 --- a/src/main/kotlin/mara/server/domain/s3/S3Service.kt +++ b/src/main/kotlin/mara/server/domain/s3/S3Service.kt @@ -5,6 +5,8 @@ import com.amazonaws.services.s3.model.CannedAccessControlList import com.amazonaws.services.s3.model.ObjectMetadata import com.amazonaws.services.s3.model.PutObjectRequest import com.amazonaws.util.IOUtils +import mara.server.exception.InvalidS3PathException +import mara.server.exception.InvalidS3PathException.Companion.INVALID_S3_PATH import org.springframework.beans.factory.annotation.Value import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional @@ -23,7 +25,7 @@ class S3Service( @Transactional fun upload(file: MultipartFile, customDir: String?): String { val resultDir = customDir ?: dir - if (resultDir.startsWith("/")) throw IllegalArgumentException("올바르지 않은 경로입니다. $resultDir") + if (resultDir.startsWith("/")) throw InvalidS3PathException(INVALID_S3_PATH) val fileName = UUID.randomUUID().toString() + "-" + file.originalFilename val objMeta = ObjectMetadata() diff --git a/src/main/kotlin/mara/server/domain/share/ShareDto.kt b/src/main/kotlin/mara/server/domain/share/ShareDto.kt index 05a89dd..027e9a7 100644 --- a/src/main/kotlin/mara/server/domain/share/ShareDto.kt +++ b/src/main/kotlin/mara/server/domain/share/ShareDto.kt @@ -50,7 +50,7 @@ data class ShareResponse( val thumbnailImage: String, val isApplied: Boolean? ) { - constructor(share: Share, isApplied: Boolean?) : this( + constructor(share: Share, isApplied: Boolean) : this( shareId = share.id, title = share.title, shareTime = share.shareTime, @@ -77,12 +77,13 @@ data class ShareDetailResponse( val location: String, val peopleCount: Int, val status: ShareStatus, - val thumbNailImage: String, + val thumbnailImage: String, val itemName: String, val shareId: Long, - val isCreatedCurrentLoginUser: Boolean, + val isCreatedByCurrentLoginUser: Boolean, + val isApplied: Boolean, ) { - constructor(share: Share, isCreatedCurrentLoginUser: Boolean) : this( + constructor(share: Share, isCreatedByCurrentLoginUser: Boolean, isApplied: Boolean) : this( nickname = share.user.nickname, profileImage = share.user.profileImage, title = share.title, @@ -94,10 +95,11 @@ data class ShareDetailResponse( location = share.location, peopleCount = share.peopleCount, status = share.status, - thumbNailImage = share.thumbnailImage, + thumbnailImage = share.thumbnailImage, itemName = share.ingredientDetail.name, shareId = share.id, - isCreatedCurrentLoginUser = isCreatedCurrentLoginUser + isCreatedByCurrentLoginUser = isCreatedByCurrentLoginUser, + isApplied = isApplied, ) } diff --git a/src/main/kotlin/mara/server/domain/share/ShareService.kt b/src/main/kotlin/mara/server/domain/share/ShareService.kt index 22a623d..9315660 100644 --- a/src/main/kotlin/mara/server/domain/share/ShareService.kt +++ b/src/main/kotlin/mara/server/domain/share/ShareService.kt @@ -3,11 +3,17 @@ package mara.server.domain.share import mara.server.auth.security.getCurrentLoginUserId import mara.server.domain.ingredient.IngredientDetailRepository import mara.server.domain.user.UserService +import mara.server.exception.IllegalAccessShareException +import mara.server.exception.IllegalAccessShareException.Companion.CREATED_BY_LOGIN_USER +import mara.server.exception.IllegalAccessShareException.Companion.DIFFERENT_USER +import mara.server.exception.IllegalAccessShareException.Companion.DUPLICATED_APPLY +import mara.server.exception.ShareException.Companion.NO_SUCH_APPLY_SHARE +import mara.server.exception.ShareException.Companion.NO_SUCH_INGREDIENT +import mara.server.exception.ShareException.Companion.NO_SUCH_SHARE import org.springframework.data.domain.Page import org.springframework.data.domain.Pageable import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional -import java.lang.RuntimeException @Service class ShareService( @@ -24,7 +30,7 @@ class ShareService( val ingredientDetailId = shareRequest.ingredientDetailId val ingredientDetail = ingredientDetailRepository.findById(ingredientDetailId) - .orElseThrow { NoSuchElementException("해당 식재료가 존재하지 않습니다. ID: $ingredientDetailId") } + .orElseThrow { NoSuchElementException("$NO_SUCH_INGREDIENT Id: $ingredientDetailId") } val user = userService.getCurrentLoginUser() // 생성 보단 조회가 빈번 할것 같아, 매번 조회 할 때마다, 일자와 시간을 분리하기 보단, 저장 할 때 각각 & 일자+시간 저장 하는 방식으로 진행 val share = Share( @@ -48,8 +54,8 @@ class ShareService( fun applyShare(applyShareRequest: ApplyShareRequest): Long { val share = getShare(applyShareRequest.shareId) val user = userService.getCurrentLoginUser() - if (share.user.userId == user.userId) throw IllegalAccessException("본인이 올린 나눔 글에는 신청을 할 수 없습니다.") - if (applyShareRepository.existsByUserAndShare(user, share)) throw IllegalAccessException("이미 신청한 나눔 입니다.") + if (share.user.userId == user.userId) throw IllegalAccessShareException(CREATED_BY_LOGIN_USER) + if (applyShareRepository.existsByUserAndShare(user, share)) throw IllegalAccessShareException(DUPLICATED_APPLY) val applyShare = ApplyShare( user = user, share = share @@ -66,12 +72,15 @@ class ShareService( fun getShare(shareId: Long): Share { return shareRepository.findById(shareId) - .orElseThrow { NoSuchElementException("해당 나눔 게시물이 존재하지 않습니다. ID: $shareId") } + .orElseThrow { NoSuchElementException("$NO_SUCH_SHARE Id: $shareId") } } fun getShareInfo(shareId: Long): ShareDetailResponse { val share = getShare(shareId) - return ShareDetailResponse(share, getCurrentLoginUserId() == share.user.userId) + val hasMatchingUser = share.applyShareList.any { applyShare -> + applyShare.user.userId == getCurrentLoginUserId() + } + return ShareDetailResponse(share, getCurrentLoginUserId() == share.user.userId, hasMatchingUser) } fun getAllShareList(pageable: Pageable, status: String): Page { @@ -114,7 +123,7 @@ class ShareService( ingredientDetailRepository.findById( ingredientDetailId ) - .orElseThrow { NoSuchElementException("해당 식재료가 존재하지 않습니다. ID: $ingredientDetailId") } + .orElseThrow { NoSuchElementException("$NO_SUCH_INGREDIENT Id: $ingredientDetailId") } share.updateIngredientDetail(ingredientDetail) } @@ -136,7 +145,7 @@ class ShareService( @Transactional fun deleteShare(shareId: Long): String { val user = userService.getCurrentLoginUser() - if (user.userId != getShare(shareId).user.userId) throw RuntimeException("잘못된 사용자로부터 전달된 요청입니다.") + if (user.userId != getShare(shareId).user.userId) throw IllegalAccessShareException(DIFFERENT_USER) shareRepository.deleteById(shareId) return deleted } @@ -145,8 +154,8 @@ class ShareService( fun deleteApplyShare(applyId: Long): String { val user = userService.getCurrentLoginUser() val applyShare = applyShareRepository.findById(applyId) - .orElseThrow { NoSuchElementException("해당 나눔 신청이 존재 하지 않습니다. ID: $applyId") } - if (user.userId != applyShare.user.userId) throw RuntimeException("잘못된 사용자로부터 전달된 요청입니다.") + .orElseThrow { NoSuchElementException("$NO_SUCH_APPLY_SHARE Id: $applyId") } + if (user.userId != applyShare.user.userId) throw IllegalAccessShareException(DIFFERENT_USER) /** 신청을 취소하면 사람 수 차감 **/ diff --git a/src/main/kotlin/mara/server/domain/user/UserController.kt b/src/main/kotlin/mara/server/domain/user/UserController.kt index 9f56704..5157c42 100644 --- a/src/main/kotlin/mara/server/domain/user/UserController.kt +++ b/src/main/kotlin/mara/server/domain/user/UserController.kt @@ -36,14 +36,14 @@ class UserController( @GetMapping("/kakao-login") @Operation(summary = "카카오 로그인 API") - fun kakaoLogin(@RequestParam(value = "code") authorizedCode: String): CommonResponse { - return success(userService.kakaoLogin(authorizedCode)) + fun kakaoLogin(@RequestParam(value = "code") authorizedCode: String, @RequestParam(value = "status") status: String): CommonResponse { + return success(userService.kakaoLogin(authorizedCode, status)) } @GetMapping("/google-login") @Operation(summary = "구글 로그인 API") - fun googleLogin(@RequestParam(value = "code") authorizedCode: String): CommonResponse { - return success(userService.googleLogin(authorizedCode)) + fun googleLogin(@RequestParam(value = "code") authorizedCode: String, @RequestParam(value = "status") status: String): CommonResponse { + return success(userService.googleLogin(authorizedCode, status)) } @GetMapping("/me") diff --git a/src/main/kotlin/mara/server/domain/user/UserService.kt b/src/main/kotlin/mara/server/domain/user/UserService.kt index 9b86cd2..ba3d414 100644 --- a/src/main/kotlin/mara/server/domain/user/UserService.kt +++ b/src/main/kotlin/mara/server/domain/user/UserService.kt @@ -74,9 +74,9 @@ class UserService( fun checkNickname(nickname: String): CheckDuplicateResponse = CheckDuplicateResponse(userRepository.existsByNickname(nickname)) - fun kakaoLogin(authorizedCode: String): AuthDto { + fun kakaoLogin(authorizedCode: String, status: String): AuthDto { // 리다이랙트 url 환경 따라 다르게 전달하기 위한 구분 값 - val accessToken = kakaoApiClient.requestAccessToken(authorizedCode) + val accessToken = kakaoApiClient.requestAccessToken(authorizedCode, status) val oauthInfoResponse = kakaoApiClient.requestOauthInfo(accessToken) log.info("kakaoId ? " + oauthInfoResponse.id) val user = userRepository.findByKakaoId(oauthInfoResponse.id) @@ -100,8 +100,8 @@ class UserService( ) } - fun googleLogin(authorizedCode: String): AuthDto { - val accessToken = googleApiClient.requestAccessToken(authorizedCode) + fun googleLogin(authorizedCode: String, status: String): AuthDto { + val accessToken = googleApiClient.requestAccessToken(authorizedCode, status) val oauthInfoResponse = googleApiClient.requestOauthInfo(accessToken) log.info(oauthInfoResponse.email) diff --git a/src/main/kotlin/mara/server/exception/GlobalExceptionHandler.kt b/src/main/kotlin/mara/server/exception/GlobalExceptionHandler.kt new file mode 100644 index 0000000..1221d2f --- /dev/null +++ b/src/main/kotlin/mara/server/exception/GlobalExceptionHandler.kt @@ -0,0 +1,26 @@ +package mara.server.exception + +import mara.server.common.ErrorResponse +import mara.server.common.fail +import mara.server.util.logger +import org.springframework.http.HttpStatus +import org.springframework.http.ResponseEntity +import org.springframework.web.bind.annotation.ControllerAdvice +import org.springframework.web.bind.annotation.ExceptionHandler + +@ControllerAdvice +class GlobalExceptionHandler { + val log = logger() + + @ExceptionHandler(InvalidDeployStatusException::class, InvalidS3PathException::class, NoSuchElementException::class) + fun handleBadRequestException(ex: Exception): ResponseEntity { + log.warn("Custom Exception [{}] was handled: {}", ex.javaClass.simpleName, ex.message) + return fail(HttpStatus.BAD_REQUEST, ex.message ?: "BadRequestException occurred") + } + + @ExceptionHandler(IllegalAccessShareException::class) + fun handleForbiddenException(ex: Exception): ResponseEntity { + log.warn("Custom Exception [{}] was handled: {}", ex.javaClass.simpleName, ex.message) + return fail(HttpStatus.FORBIDDEN, ex.message ?: "ForbiddenException occurred") + } +} diff --git a/src/main/kotlin/mara/server/exception/RefrigeratorException.kt b/src/main/kotlin/mara/server/exception/RefrigeratorException.kt new file mode 100644 index 0000000..134518e --- /dev/null +++ b/src/main/kotlin/mara/server/exception/RefrigeratorException.kt @@ -0,0 +1,7 @@ +package mara.server.exception + +class RefrigeratorException { + companion object { + const val NO_SUCH_REFRIGERATOR = "해당 냉장고가 존재하지 않습니다" + } +} diff --git a/src/main/kotlin/mara/server/exception/S3Exception.kt b/src/main/kotlin/mara/server/exception/S3Exception.kt new file mode 100644 index 0000000..55bccd0 --- /dev/null +++ b/src/main/kotlin/mara/server/exception/S3Exception.kt @@ -0,0 +1,9 @@ +package mara.server.exception + +class S3Exception + +class InvalidS3PathException(message: String) : RuntimeException(message) { + companion object { + const val INVALID_S3_PATH = "올바르지 않은 S3 경로입니다." + } +} diff --git a/src/main/kotlin/mara/server/exception/ShareException.kt b/src/main/kotlin/mara/server/exception/ShareException.kt new file mode 100644 index 0000000..95b1dad --- /dev/null +++ b/src/main/kotlin/mara/server/exception/ShareException.kt @@ -0,0 +1,17 @@ +package mara.server.exception + +class ShareException() { + companion object { + const val NO_SUCH_INGREDIENT = "해당 식재료가 존재하지 않습니다" + const val NO_SUCH_SHARE = "해당 나눔 게시물이 존재하지 않습니다" + const val NO_SUCH_APPLY_SHARE = "해당 나눔 신청이 존재하지 않습니다" + } +} + +class IllegalAccessShareException(message: String) : RuntimeException(message) { + companion object { + const val CREATED_BY_LOGIN_USER = "본인이 올린 나눔 글에는 신청을 할 수 없습니다." + const val DUPLICATED_APPLY = "이미 신청한 나눔 입니다." + const val DIFFERENT_USER = "잘못된 사용자로부터 전달된 요청입니다." + } +} diff --git a/src/main/kotlin/mara/server/exception/UserException.kt b/src/main/kotlin/mara/server/exception/UserException.kt new file mode 100644 index 0000000..90db0cd --- /dev/null +++ b/src/main/kotlin/mara/server/exception/UserException.kt @@ -0,0 +1,13 @@ +package mara.server.exception + +class UserException() { + companion object { + const val NO_SUCH_USER = "해당 유저가 존재하지 않습니다" + } +} + +class InvalidDeployStatusException(message: String) : RuntimeException(message) { + companion object { + const val INVALID_DEPLOY_STATUS = "유효하지 않은 배포 상태 값 입니다." + } +}