Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
GayeongKimm committed Jul 23, 2024
2 parents a57c66e + 04e5ad8 commit aaee598
Show file tree
Hide file tree
Showing 15 changed files with 298 additions and 0 deletions.
Binary file modified .DS_Store
Binary file not shown.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
## Git Convention

- feat: 새로운 기능에 대한 커밋
- fix: 버그 수정에 대한 커밋
- build: 빌드 관련 파일 수정에 대한 커밋
- chore: 그 외 자잘한 수정에 대한 커밋
- ci: CI 관련 설정 수정에 대한 커밋
- docs: 문서 수정에 대한 커밋
- style: 코드 스타일 혹은 포맷 등에 관한 커밋
- refactor: 코드 리팩토링에 대한 커밋
- test: 테스트 코드 수정에 대한 커밋
- remove: 코드 삭제에 대한 커밋
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package kr.hs.dgsw.SOPO_server_v2.domain.member.entity;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;

@Entity
public class MemberEntity {
@Id
@Column(name = "member_id")
private String memberId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package kr.hs.dgsw.SOPO_server_v2.domain.member.presentation.controller;

public class MemberController {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package kr.hs.dgsw.SOPO_server_v2.domain.member.repository;

public class MemberRepository {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package kr.hs.dgsw.SOPO_server_v2.domain.member.service;

public class MemberService {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package kr.hs.dgsw.SOPO_server_v2.global.common.entity;

import jakarta.persistence.EntityListeners;
import jakarta.persistence.MappedSuperclass;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;

@Getter
@SuperBuilder
@MappedSuperclass
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@EntityListeners(AuditingEntityListener.class)
public abstract class BaseTimeEntity {

@CreatedDate
private LocalDateTime createdAt;

@LastModifiedDate
private LocalDateTime modifiedAt;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package kr.hs.dgsw.SOPO_server_v2.global.error;

import kr.hs.dgsw.SOPO_server_v2.global.error.exception.StatusEnum;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Builder
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class ErrorResponse {
private String message;
private Integer status;
private LocalDateTime timestamp;
private String description;

public static ErrorResponse of(StatusEnum errorCode, String description) {
return ErrorResponse.builder()
.message(errorCode.getMessage())
.status(errorCode.getStatusCode())
.timestamp(LocalDateTime.now())
.description(description)
.build();
}

public static ErrorResponse of(int statusCode, String description) {
return ErrorResponse.builder()
.message(description)
.status(statusCode)
.timestamp(LocalDateTime.now())
.description(description)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package kr.hs.dgsw.SOPO_server_v2.global.error;

import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.FilterChain;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import kr.hs.dgsw.SOPO_server_v2.global.error.exception.BusinessException;
import kr.hs.dgsw.SOPO_server_v2.global.error.exception.StatusEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.filter.OncePerRequestFilter;

import java.io.IOException;

@RequiredArgsConstructor
public class GlobalExceptionFilter extends OncePerRequestFilter {

private final ObjectMapper objectMapper;

@Override
protected void doFilterInternal(
HttpServletRequest request,
HttpServletResponse response,
FilterChain filterChain
)throws IOException {

try {
filterChain.doFilter(request,response);
} catch (BusinessException e){
StatusEnum errorCode = e.getErrorCode();
writerErrorResponse(response, ErrorResponse.of(errorCode, errorCode.getMessage()));
} catch (Exception e){
writerErrorResponse(response, ErrorResponse.of(response.getStatus(),e.getMessage()));
}
}

private void writerErrorResponse(HttpServletResponse response, ErrorResponse errorResponse) throws IOException{
response.setStatus(errorResponse.getStatus());
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
response.setCharacterEncoding("UTF-8");
objectMapper.writeValue(response.getWriter(), errorResponse);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package kr.hs.dgsw.SOPO_server_v2.global.error;

import kr.hs.dgsw.SOPO_server_v2.global.error.exception.BusinessException;
import kr.hs.dgsw.SOPO_server_v2.global.error.exception.StatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.exception.ConstraintViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

//비즈니스 로직에서의 에러
@ExceptionHandler(BusinessException.class)
public ResponseEntity<ErrorResponse> handleBusinessException(BusinessException e) {
log.error("CustomException message : {}", e.getMessage());

StatusEnum errorCode = e.getErrorCode();
ErrorResponse response = ErrorResponse.of(errorCode, errorCode.getMessage());
e.printStackTrace();

return new ResponseEntity<>(response, HttpStatus.valueOf(errorCode.getStatusCode()));
}

// validation 에러
@ExceptionHandler(ConstraintViolationException.class)
public ResponseEntity<ErrorResponse> handleConstraintViolationException(ConstraintViolationException e) {

StatusEnum errorCode = StatusEnum.BAD_REQUEST;
ErrorResponse response = ErrorResponse.of(errorCode, errorCode.getMessage());
e.printStackTrace();

return new ResponseEntity<>(response, HttpStatus.valueOf(errorCode.getStatusCode()));
}


//그 외 에러들
@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorResponse> handleException(Exception e) {
log.error("에러", e);

StatusEnum errorCode = StatusEnum.INTERNAL_SERVER_ERROR;
ErrorResponse response = ErrorResponse.of(errorCode, e.getMessage());

return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package kr.hs.dgsw.SOPO_server_v2.global.error.custom.auth;

import kr.hs.dgsw.SOPO_server_v2.global.error.exception.BusinessException;
import kr.hs.dgsw.SOPO_server_v2.global.error.exception.StatusEnum;

public class ExpiredTokenException extends BusinessException {

public static final BusinessException EXCEPTION = new ExpiredTokenException();

public ExpiredTokenException(){
super(StatusEnum.EXPIRED_TOKEN);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package kr.hs.dgsw.SOPO_server_v2.global.error.exception;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class BusinessException extends RuntimeException {
private final StatusEnum errorCode;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package kr.hs.dgsw.SOPO_server_v2.global.error.exception;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum StatusEnum {
//smtp
NOT_DUPLICATED_AUTH_CODE(400, "Not Duplicated AuthCode"),
EXPIRED_TOKEN(401 , "Expired token"),
INVALID_TOKEN(401, "Invalid token"),
REFRESH_TOKEN_NOT_FOUND(401,"RefreshToken not found"),
MALFORMED_JWT(400, "Jwt is malformed"),
UNSUPPORTED_JWT(400, "Jwt is unsupported"),
ILLEGAL_ARGUMENT(400, "IllegalArgumentException occurred"),

// general
OK(200,"OK"),
CREATED(201,"Created"),
BAD_REQUEST(400, "Bad request"),
INTERNAL_SERVER_ERROR(500, "Internal server error"),

//user
MEMBER_NOT_EXISTS(403, "Member Not exists"),
MEMBER_EXISTS(403, "Member exists"),
USER_NOT_COINCIDE(401, "User not coincide"),
USER_NOT_FOUND(404, "User not found"),
NOT_AUTHENTICATED(401, "NotAuthenticated"),
PERMISSION_DENIED(403, "Permission denied"),
WITHDRAWAL_MEMBER(400, "Withdrawal member"),

//file
FILE_NOT_FOUND(404,"File not found"),
FILE_EXISTS(403,"File exists"),

//email
UNABLE_TO_SEND_EMAIL(403,"Unable to send email"),

CHILD_CODE_OVER_USE_TWO(400, "CHILD_CODE_OVER_USE_TWO"),
CHILD_CODE_NOT_FOUND(404, "Child code not found"),
CATEGORY_NOT_FOUND(404,"Category not found"),
IS_NOT_PARENT(400, "is not parent account"),

//fcm
MESSAGE_SEND_FAILED(403,"Message send failed"),
TOKEN_NOT_PROVIDED(400, "잘못된 토큰"),
CLOUD_EXCEPTION(500,"클라우드 에러")
;

private final int statusCode;
private final String message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package kr.hs.dgsw.SOPO_server_v2.global.response;

import org.springframework.http.HttpStatus;

public record Response(int status, String message) {
public static Response of(HttpStatus status, String message) {
return new Response(status.value(), message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package kr.hs.dgsw.SOPO_server_v2.global.response;

import org.springframework.http.HttpStatus;

public record ResponseData<T>(
int status,
String message,
T data
) {
public static <T> ResponseData<T> of(HttpStatus status, String message, T data) {
return new ResponseData<>(status.value(), message, data);
}
}

0 comments on commit aaee598

Please sign in to comment.