diff --git a/.gitignore b/.gitignore index e48b6be..ca66177 100644 --- a/.gitignore +++ b/.gitignore @@ -34,4 +34,7 @@ out/ /.nb-gradle/ ### VS Code ### -.vscode/ \ No newline at end of file +.vscode/ + +### env ### +.env \ No newline at end of file diff --git a/src/main/java/com/ftm/server/common/exception/.gitkeep b/src/main/java/com/ftm/server/common/exception/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/src/main/java/com/ftm/server/common/exception/CustomException.java b/src/main/java/com/ftm/server/common/exception/CustomException.java new file mode 100644 index 0000000..796ea57 --- /dev/null +++ b/src/main/java/com/ftm/server/common/exception/CustomException.java @@ -0,0 +1,21 @@ +package com.ftm.server.common.exception; + +import com.ftm.server.common.response.enums.ErrorResponseCode; +import lombok.Getter; + +@Getter +public class CustomException extends RuntimeException { + + private final ErrorResponseCode errorResponseCode; + + public CustomException(ErrorResponseCode errorResponseCode) { + super(errorResponseCode.getMessage()); // 예외 메시지 설정 + this.errorResponseCode = errorResponseCode; + } + + public static CustomException USER_NOT_FOUND = + new CustomException(ErrorResponseCode.USER_NOT_FOUND); + + public static CustomException USER_ALREADY_EXISTS = + new CustomException(ErrorResponseCode.USER_ALREADY_EXISTS); +} diff --git a/src/main/java/com/ftm/server/common/response/.gitkeep b/src/main/java/com/ftm/server/common/response/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/src/main/java/com/ftm/server/common/response/ApiResponse.java b/src/main/java/com/ftm/server/common/response/ApiResponse.java new file mode 100644 index 0000000..7879eb6 --- /dev/null +++ b/src/main/java/com/ftm/server/common/response/ApiResponse.java @@ -0,0 +1,40 @@ +package com.ftm.server.common.response; + +import com.ftm.server.common.response.enums.ErrorResponseCode; +import com.ftm.server.common.response.enums.SuccessResponseCode; +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public class ApiResponse { + + private final Integer status; + private final String code; + private final String message; + private final T data; + + public static ApiResponse success(SuccessResponseCode responseCode) { + return success(responseCode, null); + } + + public static ApiResponse success(SuccessResponseCode responseCode, T data) { + return new ApiResponse<>( + responseCode.getHttpStatus().value(), + responseCode.getCode(), + responseCode.getMessage(), + data); + } + + public static ApiResponse fail(ErrorResponseCode responseCode) { + return fail(responseCode, null); + } + + public static ApiResponse fail(ErrorResponseCode responseCode, T data) { + return new ApiResponse<>( + responseCode.getHttpStatus().value(), + responseCode.getCode(), + responseCode.getMessage(), + data); + } +} diff --git a/src/main/java/com/ftm/server/common/response/enums/ErrorResponseCode.java b/src/main/java/com/ftm/server/common/response/enums/ErrorResponseCode.java new file mode 100644 index 0000000..4bf381a --- /dev/null +++ b/src/main/java/com/ftm/server/common/response/enums/ErrorResponseCode.java @@ -0,0 +1,38 @@ +package com.ftm.server.common.response.enums; + +import lombok.Getter; +import org.springframework.http.HttpStatus; + +@Getter +public enum ErrorResponseCode { + + // 400번 + INVALID_REQUEST_ARGUMENT( + HttpStatus.BAD_REQUEST, "E400_001", "클라이언트 요청이 잘못된 형식이거나, 필수 데이터가 누락되었습니다."), + + // 401번 + NOT_AUTHENTICATED(HttpStatus.UNAUTHORIZED, "E401_001", "인증되지 않은 사용자입니다."), + + // 403번 + NOT_AUTHORIZATION(HttpStatus.FORBIDDEN, "E403_001", "인증된 사용자이나 해당 자원에 대한 접근 권한이 없습니다."), + + // 404번 + USER_NOT_FOUND(HttpStatus.NOT_FOUND, "E404_001", "요청된 사용자를 찾을 수 없습니다."), + + // 409번 + USER_ALREADY_EXISTS(HttpStatus.CONFLICT, "E409_001", "이미 존재하는 사용자입니다."), + PASSWORD_NOT_MATCHED(HttpStatus.CONFLICT, "E409_002", "비밀번호가 일치하지 않습니다."), + + // 500번 + UNKNOWN_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "E500_001", "알 수 없는 서버 에러가 발생했습니다."); + + private final HttpStatus httpStatus; + private final String code; + private final String message; + + ErrorResponseCode(HttpStatus httpStatus, String code, String message) { + this.httpStatus = httpStatus; + this.code = code; + this.message = message; + } +} diff --git a/src/main/java/com/ftm/server/common/response/enums/SuccessResponseCode.java b/src/main/java/com/ftm/server/common/response/enums/SuccessResponseCode.java new file mode 100644 index 0000000..ff0c646 --- /dev/null +++ b/src/main/java/com/ftm/server/common/response/enums/SuccessResponseCode.java @@ -0,0 +1,22 @@ +package com.ftm.server.common.response.enums; + +import lombok.Getter; +import org.springframework.http.HttpStatus; + +@Getter +public enum SuccessResponseCode { + OK(HttpStatus.OK, "S001", "클라이언트 요청이 정상적으로 처리됨."), + CREATED(HttpStatus.CREATED, "S002", "새로운 리소스가 성공적으로 생성됨."), + ACCEPTED(HttpStatus.ACCEPTED, "S003", "요청은 수락되었지만 아직 처리가 완료되지 않음."), + NO_CONTENT(HttpStatus.NO_CONTENT, "S004", "요청이 성공적으로 처리되었지만, 응답 본문이 필요 없음."); + + private final HttpStatus httpStatus; + private final String code; + private final String message; + + SuccessResponseCode(HttpStatus httpStatus, String code, String message) { + this.httpStatus = httpStatus; + this.code = code; + this.message = message; + } +} diff --git a/src/main/resources/application-datasource.yml b/src/main/resources/application-datasource.yml index a2ad700..b3ede19 100644 --- a/src/main/resources/application-datasource.yml +++ b/src/main/resources/application-datasource.yml @@ -10,6 +10,7 @@ spring: password: ${POSTGRES_PASSWORD} jpa: + database-platform: org.hibernate.dialect.PostgreSQLDialect properties: hibernate: default_batch_fetch_size: ${JPA_BATCH_FETCH_SIZE:100} \ No newline at end of file