From 2aa618f59504bc83d35e4e4e8c49db1973953dec Mon Sep 17 00:00:00 2001 From: Jo Kyung Hyeon Date: Fri, 9 Dec 2022 20:07:07 +0900 Subject: [PATCH 1/8] =?UTF-8?q?refactor:=20(#202)=20=ED=8C=8C=EC=9D=BC=20?= =?UTF-8?q?=ED=99=95=EC=9E=A5=EC=9E=90=20=EC=9C=A0=ED=8B=B8=EB=A6=AC?= =?UTF-8?q?=ED=8B=B0=20&=20=EC=98=88=EC=99=B8=20=ED=8C=A8=ED=82=A4?= =?UTF-8?q?=EC=A7=80=20=EC=9D=B4=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/team/comit/simtong/domain/file/FileExtensionUtils.kt | 0 .../domain/file/exception/FileInvalidExtensionException.kt | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename {simtong-domain => simtong-application}/src/main/kotlin/team/comit/simtong/domain/file/FileExtensionUtils.kt (100%) rename {simtong-domain => simtong-application}/src/main/kotlin/team/comit/simtong/domain/file/exception/FileInvalidExtensionException.kt (100%) diff --git a/simtong-domain/src/main/kotlin/team/comit/simtong/domain/file/FileExtensionUtils.kt b/simtong-application/src/main/kotlin/team/comit/simtong/domain/file/FileExtensionUtils.kt similarity index 100% rename from simtong-domain/src/main/kotlin/team/comit/simtong/domain/file/FileExtensionUtils.kt rename to simtong-application/src/main/kotlin/team/comit/simtong/domain/file/FileExtensionUtils.kt diff --git a/simtong-domain/src/main/kotlin/team/comit/simtong/domain/file/exception/FileInvalidExtensionException.kt b/simtong-application/src/main/kotlin/team/comit/simtong/domain/file/exception/FileInvalidExtensionException.kt similarity index 100% rename from simtong-domain/src/main/kotlin/team/comit/simtong/domain/file/exception/FileInvalidExtensionException.kt rename to simtong-application/src/main/kotlin/team/comit/simtong/domain/file/exception/FileInvalidExtensionException.kt From b0fa570239317b41273a99f1d564b3dd3b66fa81 Mon Sep 17 00:00:00 2001 From: Jo Kyung Hyeon Date: Fri, 9 Dec 2022 20:07:37 +0900 Subject: [PATCH 2/8] add: (#202) File Convertor --- .../file/transfer/ExcelFileConvertor.kt | 24 +++++++++ .../domain/file/transfer/FileConvertor.kt | 53 +++++++++++++++++++ .../file/transfer/ImageFileConvertor.kt | 26 +++++++++ 3 files changed, 103 insertions(+) create mode 100644 simtong-presentation/src/main/kotlin/team/comit/simtong/domain/file/transfer/ExcelFileConvertor.kt create mode 100644 simtong-presentation/src/main/kotlin/team/comit/simtong/domain/file/transfer/FileConvertor.kt create mode 100644 simtong-presentation/src/main/kotlin/team/comit/simtong/domain/file/transfer/ImageFileConvertor.kt diff --git a/simtong-presentation/src/main/kotlin/team/comit/simtong/domain/file/transfer/ExcelFileConvertor.kt b/simtong-presentation/src/main/kotlin/team/comit/simtong/domain/file/transfer/ExcelFileConvertor.kt new file mode 100644 index 00000000..d01cfdc0 --- /dev/null +++ b/simtong-presentation/src/main/kotlin/team/comit/simtong/domain/file/transfer/ExcelFileConvertor.kt @@ -0,0 +1,24 @@ +package team.comit.simtong.domain.file.transfer + +import org.springframework.web.multipart.MultipartFile +import team.comit.simtong.domain.file.FileExtensionUtils.XLS +import team.comit.simtong.domain.file.FileExtensionUtils.XLSX + +/** + * + * Excel File의 변환을 담당하는 ExcelFileConvertor + * + * @author Chokyunghyeon + * @date 2022/12/09 + * @version 1.0.0 + **/ +object ExcelFileConvertor : FileConvertor { + + override fun isCorrectExtension(multipartFile: MultipartFile): Boolean { + return when (multipartFile.extension) { + XLS, XLSX -> true + else -> false + } + } + +} \ No newline at end of file diff --git a/simtong-presentation/src/main/kotlin/team/comit/simtong/domain/file/transfer/FileConvertor.kt b/simtong-presentation/src/main/kotlin/team/comit/simtong/domain/file/transfer/FileConvertor.kt new file mode 100644 index 00000000..56f678d9 --- /dev/null +++ b/simtong-presentation/src/main/kotlin/team/comit/simtong/domain/file/transfer/FileConvertor.kt @@ -0,0 +1,53 @@ +package team.comit.simtong.domain.file.transfer + +import org.springframework.web.multipart.MultipartFile +import team.comit.simtong.domain.file.exception.FileInvalidExtensionException +import java.io.File +import java.io.FileOutputStream +import java.util.UUID + +/** + * + * MultipartFile에서 File의 변환을 담당하는 FileConvertor + * + * @author Chokyunghyeon + * @date 2022/12/09 + * @version 1.0.0 + **/ +interface FileConvertor { + + val MultipartFile.extension: String + get() = originalFilename?.substringAfterLast(".", "")?.uppercase() ?: "" + + + fun isCorrectExtension(multipartFile: MultipartFile): Boolean + + fun transferTo(multipartFile: MultipartFile): File { + if (!isCorrectExtension(multipartFile)) { + throw FileInvalidExtensionException.EXCEPTION + } + + return transferFile(multipartFile) + } + + fun transferToList(multipartFiles: List): List { + multipartFiles.forEach { + if (!isCorrectExtension(it)) { + throw FileInvalidExtensionException.EXCEPTION + } + } + + return multipartFiles.map(this::transferFile) + } + + private fun transferFile(multipartFile: MultipartFile): File { + return File("${UUID.randomUUID()}_${multipartFile.originalFilename}") + .apply { + FileOutputStream(this).run { + write(multipartFile.bytes) + close() + } + } + } + +} \ No newline at end of file diff --git a/simtong-presentation/src/main/kotlin/team/comit/simtong/domain/file/transfer/ImageFileConvertor.kt b/simtong-presentation/src/main/kotlin/team/comit/simtong/domain/file/transfer/ImageFileConvertor.kt new file mode 100644 index 00000000..e7f704ee --- /dev/null +++ b/simtong-presentation/src/main/kotlin/team/comit/simtong/domain/file/transfer/ImageFileConvertor.kt @@ -0,0 +1,26 @@ +package team.comit.simtong.domain.file.transfer + +import org.springframework.web.multipart.MultipartFile +import team.comit.simtong.domain.file.FileExtensionUtils.HEIC +import team.comit.simtong.domain.file.FileExtensionUtils.JPEG +import team.comit.simtong.domain.file.FileExtensionUtils.JPG +import team.comit.simtong.domain.file.FileExtensionUtils.PNG + +/** + * + * Image File의 변환을 담당하는 ImageFileConvertor + * + * @author Chokyunghyeon + * @date 2022/12/09 + * @version 1.0.0 + **/ +object ImageFileConvertor : FileConvertor { + + override fun isCorrectExtension(multipartFile: MultipartFile): Boolean { + return when (multipartFile.extension) { + JPG, JPEG, PNG, HEIC -> true + else -> false + } + } + +} \ No newline at end of file From fe2c5b585e2ea92bd68c2f18698ff8b27b190dd2 Mon Sep 17 00:00:00 2001 From: Jo Kyung Hyeon Date: Fri, 9 Dec 2022 20:08:24 +0900 Subject: [PATCH 3/8] =?UTF-8?q?refactor:=20(#202)=20=EA=B8=B0=EC=A1=B4=20?= =?UTF-8?q?=EB=B3=80=ED=99=98=20=EB=A1=9C=EC=A7=81=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/file/usecase/UploadImageUseCase.kt | 16 ------ .../file/usecase/UploadImageUseCaseTests.kt | 55 +++++-------------- .../simtong/domain/file/WebFileAdapter.kt | 26 ++++----- 3 files changed, 26 insertions(+), 71 deletions(-) diff --git a/simtong-application/src/main/kotlin/team/comit/simtong/domain/file/usecase/UploadImageUseCase.kt b/simtong-application/src/main/kotlin/team/comit/simtong/domain/file/usecase/UploadImageUseCase.kt index 893545e2..ec697dbb 100644 --- a/simtong-application/src/main/kotlin/team/comit/simtong/domain/file/usecase/UploadImageUseCase.kt +++ b/simtong-application/src/main/kotlin/team/comit/simtong/domain/file/usecase/UploadImageUseCase.kt @@ -1,7 +1,5 @@ package team.comit.simtong.domain.file.usecase -import team.comit.simtong.domain.file.FileExtensionUtils.isImageExtension -import team.comit.simtong.domain.file.exception.FileInvalidExtensionException import team.comit.simtong.domain.file.spi.UploadFilePort import team.comit.simtong.global.annotation.UseCase import java.io.File @@ -20,25 +18,11 @@ class UploadImageUseCase( ) { fun execute(file: File): String { - if (!isImageExtension(file)) { - file.delete() - throw FileInvalidExtensionException.EXCEPTION - } - return uploadFilePort.upload(file) } fun execute(files: List): List { - files.forEach { - if (!isImageExtension(it)) { - files.deleteAll() - throw FileInvalidExtensionException.EXCEPTION - } - } - return uploadFilePort.upload(files) } - private fun List.deleteAll() = this.forEach(File::delete) - } \ No newline at end of file diff --git a/simtong-application/src/test/kotlin/team/comit/simtong/domain/file/usecase/UploadImageUseCaseTests.kt b/simtong-application/src/test/kotlin/team/comit/simtong/domain/file/usecase/UploadImageUseCaseTests.kt index f88784cd..1642d217 100644 --- a/simtong-application/src/test/kotlin/team/comit/simtong/domain/file/usecase/UploadImageUseCaseTests.kt +++ b/simtong-application/src/test/kotlin/team/comit/simtong/domain/file/usecase/UploadImageUseCaseTests.kt @@ -3,10 +3,8 @@ package team.comit.simtong.domain.file.usecase import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test -import org.junit.jupiter.api.assertThrows import org.mockito.BDDMockito.given import org.springframework.boot.test.mock.mockito.MockBean -import team.comit.simtong.domain.file.exception.FileInvalidExtensionException import team.comit.simtong.domain.file.spi.UploadFilePort import team.comit.simtong.global.annotation.SimtongTest import java.io.File @@ -21,15 +19,13 @@ class UploadImageUseCaseTests { private val filePathStub = "test path" - private val filePathListStub = listOf(filePathStub) - - private val jpgFileStub by lazy { File("test.jpg") } - - private val jpegFileStub by lazy { File("test.jpeg") } - - private val pngFileStub by lazy { File("test.png") } + private val fileStub: File by lazy { + File("test.jpg") + } - private val svgFileStub by lazy { File("test.svg") } + private val filesStub: List by lazy { + listOf(fileStub) + } @BeforeEach fun setUp() { @@ -39,50 +35,29 @@ class UploadImageUseCaseTests { @Test fun `단일 이미지 업로드`() { // given - given(managerFilePort.upload(jpgFileStub)) + given(managerFilePort.upload(fileStub)) .willReturn(filePathStub) - given(managerFilePort.upload(jpegFileStub)) - .willReturn(filePathStub) + // when + val response = uploadImageUseCase.execute(fileStub) - given(managerFilePort.upload(pngFileStub)) - .willReturn(filePathStub) + // then + assertEquals(response, filePathStub) - // when & then - assertEquals(uploadImageUseCase.execute(jpgFileStub), filePathStub) - assertEquals(uploadImageUseCase.execute(jpegFileStub), filePathStub) - assertEquals(uploadImageUseCase.execute(pngFileStub), filePathStub) } @Test fun `다중 이미지 업로드`() { // given - val filesStub = listOf(jpgFileStub, jpegFileStub, pngFileStub) + val filePathListStub = listOf(filePathStub) given(managerFilePort.upload(filesStub)) .willReturn(filePathListStub) - // when & then - assertEquals(uploadImageUseCase.execute(filesStub), filePathListStub) - } - - @Test - fun `단일 파일 확장자 오류`() { - // when & then - assertThrows { - uploadImageUseCase.execute(svgFileStub) - } - } - - @Test - fun `다중 파일 확장자 오류`() { - // given - val files = listOf(jpgFileStub, jpegFileStub, pngFileStub, svgFileStub) + // when + val response = uploadImageUseCase.execute(filesStub) - // when & then - assertThrows { - uploadImageUseCase.execute(files) - } + assertEquals(response, filePathListStub) } } \ No newline at end of file diff --git a/simtong-presentation/src/main/kotlin/team/comit/simtong/domain/file/WebFileAdapter.kt b/simtong-presentation/src/main/kotlin/team/comit/simtong/domain/file/WebFileAdapter.kt index 0806383c..4f4e2299 100644 --- a/simtong-presentation/src/main/kotlin/team/comit/simtong/domain/file/WebFileAdapter.kt +++ b/simtong-presentation/src/main/kotlin/team/comit/simtong/domain/file/WebFileAdapter.kt @@ -8,11 +8,10 @@ import org.springframework.web.bind.annotation.RestController import org.springframework.web.multipart.MultipartFile import team.comit.simtong.domain.file.dto.response.UploadImageListWebResponse import team.comit.simtong.domain.file.dto.response.UploadImageWebResponse +import team.comit.simtong.domain.file.transfer.ExcelFileConvertor +import team.comit.simtong.domain.file.transfer.ImageFileConvertor import team.comit.simtong.domain.file.usecase.RegisterEmployeeCertificateUseCase import team.comit.simtong.domain.file.usecase.UploadImageUseCase -import java.io.File -import java.io.FileOutputStream -import java.util.UUID /** * @@ -33,7 +32,9 @@ class WebFileAdapter( @ResponseStatus(HttpStatus.CREATED) fun uploadSingleImage(file: MultipartFile): UploadImageWebResponse { return UploadImageWebResponse( - uploadImageUseCase.execute(file.let(transferFile)) + uploadImageUseCase.execute( + file.let(ImageFileConvertor::transferTo) + ) ) } @@ -41,23 +42,18 @@ class WebFileAdapter( @ResponseStatus(HttpStatus.CREATED) fun uploadMultipleImage(files: List): UploadImageListWebResponse { return UploadImageListWebResponse( - uploadImageUseCase.execute(files.map(transferFile)) + uploadImageUseCase.execute( + files.let(ImageFileConvertor::transferToList) + ) ) } @PostMapping("/employee") @ResponseStatus(HttpStatus.CREATED) fun importEmployeeCertificate(file: MultipartFile) { - registerEmployeeCertificateUseCase.execute(file.let(transferFile)) - } - - private val transferFile = { multipartFile: MultipartFile -> - File("${UUID.randomUUID()}_${multipartFile.originalFilename}").apply { - FileOutputStream(this).run { - write(multipartFile.bytes) - close() - } - } + registerEmployeeCertificateUseCase.execute( + file.let(ExcelFileConvertor::transferTo) + ) } } \ No newline at end of file From 3b8f6bfa8e617ac15bfb41749f1d747c0d15ceb7 Mon Sep 17 00:00:00 2001 From: Jo Kyung Hyeon Date: Fri, 9 Dec 2022 20:08:40 +0900 Subject: [PATCH 4/8] chore: (#202) .gitkeep --- .../main/kotlin/team/comit/simtong/domain/file/exception/.gitkeep | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 simtong-application/src/main/kotlin/team/comit/simtong/domain/file/exception/.gitkeep diff --git a/simtong-application/src/main/kotlin/team/comit/simtong/domain/file/exception/.gitkeep b/simtong-application/src/main/kotlin/team/comit/simtong/domain/file/exception/.gitkeep deleted file mode 100644 index e69de29b..00000000 From 12c6c9b90cbed9a4490e33ad13d9e0ff33376241 Mon Sep 17 00:00:00 2001 From: Jo Kyung Hyeon Date: Fri, 9 Dec 2022 21:24:37 +0900 Subject: [PATCH 5/8] add: (#202) Web Exception --- .../simtong/global/error/WebErrorProperty.kt | 17 +++++++++++++++++ .../comit/simtong/global/error/WebException.kt | 13 +++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 simtong-presentation/src/main/kotlin/team/comit/simtong/global/error/WebErrorProperty.kt create mode 100644 simtong-presentation/src/main/kotlin/team/comit/simtong/global/error/WebException.kt diff --git a/simtong-presentation/src/main/kotlin/team/comit/simtong/global/error/WebErrorProperty.kt b/simtong-presentation/src/main/kotlin/team/comit/simtong/global/error/WebErrorProperty.kt new file mode 100644 index 00000000..f908480b --- /dev/null +++ b/simtong-presentation/src/main/kotlin/team/comit/simtong/global/error/WebErrorProperty.kt @@ -0,0 +1,17 @@ +package team.comit.simtong.global.error + +/** + * + * 표현 계층의 ErrorCode 형식을 맞추기 위해 사용하는 WebErrorProperty + * + * @author Chokyunghyeon + * @date 2022/12/09 + * @version 1.0.0 + **/ +interface WebErrorProperty { + + fun status(): Int + + fun message(): String + +} \ No newline at end of file diff --git a/simtong-presentation/src/main/kotlin/team/comit/simtong/global/error/WebException.kt b/simtong-presentation/src/main/kotlin/team/comit/simtong/global/error/WebException.kt new file mode 100644 index 00000000..bb4b460e --- /dev/null +++ b/simtong-presentation/src/main/kotlin/team/comit/simtong/global/error/WebException.kt @@ -0,0 +1,13 @@ +package team.comit.simtong.global.error + +/** + * + * 표현 계층에서 상황에 따라 예외를 처리하도록 Runtime Exception을 상속받은 WebException + * + * @author Chokyunghyeon + * @date 2022/12/09 + * @version 1.0.0 + **/ +abstract class WebException( + val exceptionProperty: WebErrorProperty +) : RuntimeException() \ No newline at end of file From 79d3096c31faef9d24a6417d0c6cf69b6c651d0b Mon Sep 17 00:00:00 2001 From: Jo Kyung Hyeon Date: Fri, 9 Dec 2022 21:29:44 +0900 Subject: [PATCH 6/8] =?UTF-8?q?refactor:=20(#202)=20=ED=8C=8C=EC=9D=BC=20?= =?UTF-8?q?=ED=99=95=EC=9E=A5=EC=9E=90=20=EC=A0=9C=ED=95=9C=20=EC=98=88?= =?UTF-8?q?=EC=99=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../simtong/domain/file/exception/.gitkeep | 0 .../FileInvalidExtensionException.kt | 21 ------------------- .../domain/file/error/FileErrorCode.kt | 2 -- .../FileInvalidExtensionException.kt | 21 +++++++++++++++++++ 4 files changed, 21 insertions(+), 23 deletions(-) create mode 100644 simtong-application/src/main/kotlin/team/comit/simtong/domain/file/exception/.gitkeep delete mode 100644 simtong-application/src/main/kotlin/team/comit/simtong/domain/file/exception/FileInvalidExtensionException.kt create mode 100644 simtong-presentation/src/main/kotlin/team/comit/simtong/domain/file/exception/FileInvalidExtensionException.kt diff --git a/simtong-application/src/main/kotlin/team/comit/simtong/domain/file/exception/.gitkeep b/simtong-application/src/main/kotlin/team/comit/simtong/domain/file/exception/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/simtong-application/src/main/kotlin/team/comit/simtong/domain/file/exception/FileInvalidExtensionException.kt b/simtong-application/src/main/kotlin/team/comit/simtong/domain/file/exception/FileInvalidExtensionException.kt deleted file mode 100644 index 7a0e98d9..00000000 --- a/simtong-application/src/main/kotlin/team/comit/simtong/domain/file/exception/FileInvalidExtensionException.kt +++ /dev/null @@ -1,21 +0,0 @@ -package team.comit.simtong.domain.file.exception - -import team.comit.simtong.domain.file.error.FileErrorCode -import team.comit.simtong.global.error.BusinessException - -/** - * - * 확장자 제한 에러를 발생시키는 FileExtensionInvalidException - * - * @author Chokyunghyeon - * @date 2022/09/07 - * @version 1.0.0 - **/ -class FileInvalidExtensionException private constructor() : BusinessException(FileErrorCode.INVALID_EXTENSION) { - - companion object { - @JvmField - val EXCEPTION = FileInvalidExtensionException() - } - -} \ No newline at end of file diff --git a/simtong-domain/src/main/kotlin/team/comit/simtong/domain/file/error/FileErrorCode.kt b/simtong-domain/src/main/kotlin/team/comit/simtong/domain/file/error/FileErrorCode.kt index 1fac392d..22f7a420 100644 --- a/simtong-domain/src/main/kotlin/team/comit/simtong/domain/file/error/FileErrorCode.kt +++ b/simtong-domain/src/main/kotlin/team/comit/simtong/domain/file/error/FileErrorCode.kt @@ -15,8 +15,6 @@ enum class FileErrorCode( private val message: String ) : ErrorProperty { - INVALID_EXTENSION(400, "제한된 확장자"), - NOT_VALID_CONTENT(400, "파일의 내용이 올바르지 않음"), INVALID_EMPLOYEE(401, "일치하는 사원 정보가 존재하지 않음"), diff --git a/simtong-presentation/src/main/kotlin/team/comit/simtong/domain/file/exception/FileInvalidExtensionException.kt b/simtong-presentation/src/main/kotlin/team/comit/simtong/domain/file/exception/FileInvalidExtensionException.kt new file mode 100644 index 00000000..5cabc0e8 --- /dev/null +++ b/simtong-presentation/src/main/kotlin/team/comit/simtong/domain/file/exception/FileInvalidExtensionException.kt @@ -0,0 +1,21 @@ +package team.comit.simtong.domain.file.exception + +import team.comit.simtong.domain.file.error.WebFileErrorCode +import team.comit.simtong.global.error.WebException + +/** + * + * 파일 확장자 제한 에러를 발생시키는 FileInvalidExtensionException + * + * @author Chokyunghyeon + * @date 2022/12/09 + * @version 1.0.0 + **/ +class FileInvalidExtensionException private constructor() : WebException(WebFileErrorCode.INVALID_EXTENSION) { + + companion object { + @JvmField + val EXCEPTION = FileInvalidExtensionException() + } + +} \ No newline at end of file From 1bd0897b83384cefa87aac7d0e2c6d0a011538b7 Mon Sep 17 00:00:00 2001 From: Jo Kyung Hyeon Date: Fri, 9 Dec 2022 21:30:37 +0900 Subject: [PATCH 7/8] =?UTF-8?q?feat:=20(#202)=20Web=20Exception=20?= =?UTF-8?q?=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../simtong/global/error/dto/ErrorResponse.kt | 7 ++++ .../simtong/global/filter/ExceptionFilter.kt | 32 +++++++++++++++---- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/simtong-infrastructure/src/main/kotlin/team/comit/simtong/global/error/dto/ErrorResponse.kt b/simtong-infrastructure/src/main/kotlin/team/comit/simtong/global/error/dto/ErrorResponse.kt index 523ea192..ee125165 100644 --- a/simtong-infrastructure/src/main/kotlin/team/comit/simtong/global/error/dto/ErrorResponse.kt +++ b/simtong-infrastructure/src/main/kotlin/team/comit/simtong/global/error/dto/ErrorResponse.kt @@ -6,6 +6,7 @@ import org.springframework.web.bind.MissingServletRequestParameterException import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException import team.comit.simtong.global.error.ErrorProperty import team.comit.simtong.global.error.GlobalErrorCode +import team.comit.simtong.global.error.WebErrorProperty import javax.validation.ConstraintViolationException /** @@ -29,6 +30,12 @@ class ErrorResponse( fieldErrors = emptyList() ) + fun of(exception: WebErrorProperty) = ErrorResponse( + status = exception.status(), + message = exception.message(), + fieldErrors = emptyList() + ) + fun of(bindingResult: BindingResult): ErrorResponse = of( exception = GlobalErrorCode.BAD_REQUEST, fieldErrors = CustomFieldError.of(bindingResult) diff --git a/simtong-infrastructure/src/main/kotlin/team/comit/simtong/global/filter/ExceptionFilter.kt b/simtong-infrastructure/src/main/kotlin/team/comit/simtong/global/filter/ExceptionFilter.kt index 9a3a3993..18cf18d1 100644 --- a/simtong-infrastructure/src/main/kotlin/team/comit/simtong/global/filter/ExceptionFilter.kt +++ b/simtong-infrastructure/src/main/kotlin/team/comit/simtong/global/filter/ExceptionFilter.kt @@ -5,6 +5,8 @@ import org.springframework.http.MediaType import org.springframework.web.filter.OncePerRequestFilter import team.comit.simtong.global.error.BusinessException import team.comit.simtong.global.error.ErrorProperty +import team.comit.simtong.global.error.WebErrorProperty +import team.comit.simtong.global.error.WebException import team.comit.simtong.global.error.dto.ErrorResponse import team.comit.simtong.global.exception.InternalServerErrorException import java.nio.charset.StandardCharsets @@ -36,6 +38,7 @@ class ExceptionFilter( } catch (e: Exception) { when (e.cause) { is BusinessException -> writeErrorCode((e.cause as BusinessException).exceptionProperty, response) + is WebException -> writeErrorCode((e.cause as WebException).exceptionProperty, response) else -> { e.printStackTrace() writeErrorCode(InternalServerErrorException.EXCEPTION.exceptionProperty, response) @@ -45,13 +48,28 @@ class ExceptionFilter( } private fun writeErrorCode(exception: ErrorProperty, response: HttpServletResponse) { - response.characterEncoding = StandardCharsets.UTF_8.name() - response.status = exception.status() - response.contentType = MediaType.APPLICATION_JSON_VALUE - response.writer.write( - objectMapper.writeValueAsString( - ErrorResponse.of(exception) + response.run { + characterEncoding = StandardCharsets.UTF_8.name() + status = exception.status() + contentType = MediaType.APPLICATION_JSON_VALUE + writer.write( + objectMapper.writeValueAsString( + ErrorResponse.of(exception) + ) ) - ) + } + } + + private fun writeErrorCode(exception: WebErrorProperty, response: HttpServletResponse) { + response.run { + characterEncoding = StandardCharsets.UTF_8.name() + status = exception.status() + contentType = MediaType.APPLICATION_JSON_VALUE + writer.write( + objectMapper.writeValueAsString( + ErrorResponse.of(exception) + ) + ) + } } } \ No newline at end of file From a80ba4228cfbc9bd799ce3ffe85fc9c3fb8af368 Mon Sep 17 00:00:00 2001 From: Jo Kyung Hyeon Date: Fri, 9 Dec 2022 21:31:51 +0900 Subject: [PATCH 8/8] =?UTF-8?q?refactor:=20(#202)=20internal=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Exception 처리 시 서버 오류 --- .../domain/file/error/WebFileErrorCode.kt | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 simtong-presentation/src/main/kotlin/team/comit/simtong/domain/file/error/WebFileErrorCode.kt diff --git a/simtong-presentation/src/main/kotlin/team/comit/simtong/domain/file/error/WebFileErrorCode.kt b/simtong-presentation/src/main/kotlin/team/comit/simtong/domain/file/error/WebFileErrorCode.kt new file mode 100644 index 00000000..43c8d9f5 --- /dev/null +++ b/simtong-presentation/src/main/kotlin/team/comit/simtong/domain/file/error/WebFileErrorCode.kt @@ -0,0 +1,24 @@ +package team.comit.simtong.domain.file.error + +import team.comit.simtong.global.error.WebErrorProperty + +/** + * + * 표현 계층의 File Error를 관리하는 WebFileErrorCode + * + * @author Chokyunghyeon + * @date 2022/12/09 + * @version 1.0.0 + **/ +enum class WebFileErrorCode( + private val status: Int, + private val message: String +) : WebErrorProperty { + + INVALID_EXTENSION(400, "제한된 확장자"); + + override fun status(): Int = status + + override fun message(): String = message + +} \ No newline at end of file