Skip to content

Commit

Permalink
Merge branch 'RIngo-145'
Browse files Browse the repository at this point in the history
  • Loading branch information
KangJiSseok committed May 15, 2024
2 parents 11b9f6d + d79e383 commit 2c7b8b9
Show file tree
Hide file tree
Showing 15 changed files with 196 additions and 88 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package springwebsocket.webchat.friend.exception;

import springwebsocket.webchat.error.ErrorCode;
import springwebsocket.webchat.error.exception.InvalidValueException;
import springwebsocket.webchat.global.error.BusinessException;
import springwebsocket.webchat.global.response.ErrorCode;

public class FriendDuplicationException extends InvalidValueException {
public class FriendDuplicationException extends BusinessException {
public FriendDuplicationException() {
super(ErrorCode.FRIEND_ALREADT_TAKEN);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package springwebsocket.webchat.global.error;

import springwebsocket.webchat.global.response.ErrorCode;

public class BusinessException extends RuntimeException {

private final ErrorCode errorCode;

protected BusinessException(String message, ErrorCode errorCode) {
super(message);
this.errorCode = errorCode;
}

public BusinessException(ErrorCode errorCode) {
super(errorCode.getMessage());
this.errorCode = errorCode;
}

public ErrorCode getErrorCode() {
return errorCode;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package springwebsocket.webchat.global.error.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import springwebsocket.webchat.global.error.BusinessException;
import springwebsocket.webchat.global.response.ApiResponse;
import springwebsocket.webchat.global.response.ErrorCode;


@Slf4j
@RestControllerAdvice
public class ExceptionAdvice {

@ExceptionHandler(RuntimeException.class)
public ResponseEntity<ApiResponse<?>> handleDuplicatedUserException(BusinessException e) {
final ErrorCode errorCode = e.getErrorCode();

return ResponseEntity.status(HttpStatus.CONFLICT).body(ApiResponse.createError(errorCode.getMessage()));
}

@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ApiResponse<?>> handleValidationExceptions(BindingResult bindingResult) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ApiResponse.createFail(bindingResult));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
log.info("JWTFilter doFilterInternal");

//request에서 access 헤더 찾기
String accessToken = request.getHeader("Access-Token");
String accessToken = request.getHeader("AccessToken");

//토큰이 없다면 다음 필터로 넘김
if (accessToken == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package springwebsocket.webchat.global.response;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class ApiResponse<T> {

private static final String SUCCESS_STATUS = "success";
private static final String FAIL_STATUS = "fail";
private static final String ERROR_STATUS = "error";

private String status;
private T data;
private String message;

public static <T> ApiResponse<T> createSuccess(T data) {
return new ApiResponse<>(SUCCESS_STATUS, data, null);
}

public static ApiResponse<?> createSuccessWithNoContent() {
return new ApiResponse<>(SUCCESS_STATUS, null, null);
}

// Hibernate Validator에 의해 유효하지 않은 데이터로 인해 API 호출이 거부될때 반환
public static ApiResponse<?> createFail(BindingResult bindingResult) {
Map<String, String> errors = new HashMap<>();

List<ObjectError> allErrors = bindingResult.getAllErrors();
for (ObjectError error : allErrors) {
if (error instanceof FieldError) {
errors.put(((FieldError) error).getField(), error.getDefaultMessage());
} else {
errors.put( error.getObjectName(), error.getDefaultMessage());
}
}
return new ApiResponse<>(FAIL_STATUS, errors, null);
}

// 예외 발생으로 API 호출 실패시 반환
public static ApiResponse<?> createError(String message) {
return new ApiResponse<>(ERROR_STATUS, null, message);
}

private ApiResponse(String status, T data, String message) {
this.status = status;
this.data = data;
this.message = message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package springwebsocket.webchat.global.response;

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

@JsonFormat(shape = JsonFormat.Shape.OBJECT)
@Getter
@AllArgsConstructor
public enum ErrorCode {
// -----예외(Http 상태, 메시지)------ //

// Member
EMAIL_ALREADY_TAKEN(400, "이미 존재하는 이메일입니다."),


//Friend
FRIEND_ALREADT_TAKEN(400,"이미 친구 요청을 보낸 상대입니다.");



private final int status;
private final String message;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package springwebsocket.webchat.global.response;

import lombok.AllArgsConstructor;
import lombok.Getter;

/** Enum Naming Format : {행위}_{목적어}_{성공여부} message format: 동사 명사형으로 마무리 */
@Getter
@AllArgsConstructor
public enum ResultCode {
// member
UPDATE_MEMBER_SUCCESS(200,"멤버 업데이트 성공"),

;
private final int status;
private final String message;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import springwebsocket.webchat.global.response.ApiResponse;
import springwebsocket.webchat.member.dto.request.LoginRequest;
import springwebsocket.webchat.member.dto.request.SignUpRequest;
import springwebsocket.webchat.member.dto.response.UserResponse;
import springwebsocket.webchat.member.dto.response.loginMessage;
import springwebsocket.webchat.member.dto.response.TokenMessage;
import springwebsocket.webchat.member.entity.Member;
import springwebsocket.webchat.member.dto.MemberUpdataDto;
import springwebsocket.webchat.member.service.MemberService;
Expand All @@ -28,9 +26,10 @@ public class MemberController {

private final MemberService userService;
@PostMapping("/signup")
public ResponseEntity<UserResponse> register(final @RequestBody SignUpRequest request) {
UserResponse userResponse = userService.signUp(request);
return ResponseEntity.ok().body(userResponse);
public ApiResponse<?> register(final @RequestBody SignUpRequest request) {

userService.signUp(request);
return ApiResponse.createSuccessWithNoContent();
}

@PostMapping("/update")
Expand All @@ -49,9 +48,9 @@ public void delete(Long id) {
}

@PostMapping("/login")
public ResponseEntity<loginMessage> login(@RequestBody LoginRequest request) {
log.info("/member/login");
return userService.login(request.getLoginEmail(), request.getPassword());
public ApiResponse<?> login(@RequestBody LoginRequest request) {
TokenMessage message = userService.login(request.getLoginEmail(), request.getPassword());
return ApiResponse.createSuccess(message);
}

@GetMapping("/logout")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package springwebsocket.webchat.member.dto.response;


import lombok.Getter;

@Getter
public class TokenMessage {
String AccessToken;
String RefreshToken;

public TokenMessage(String accessToken, String refreshToken) {
AccessToken = accessToken;
RefreshToken = refreshToken;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package springwebsocket.webchat.member.exception;

import springwebsocket.webchat.error.ErrorCode;
import springwebsocket.webchat.error.exception.InvalidValueException;
import springwebsocket.webchat.global.error.BusinessException;
import springwebsocket.webchat.global.response.ErrorCode;

public class EmailDuplicatedException extends InvalidValueException {
public class EmailDuplicatedException extends BusinessException {

public EmailDuplicatedException(String email) {
super(email, ErrorCode.EMAIL_ALREADY_TAKEN);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.ResponseEntity;
import springwebsocket.webchat.global.response.ApiResponse;
import springwebsocket.webchat.member.dto.MemberUpdataDto;
import springwebsocket.webchat.member.dto.request.SignUpRequest;
import springwebsocket.webchat.member.dto.response.TokenMessage;
import springwebsocket.webchat.member.dto.response.UserResponse;
import springwebsocket.webchat.member.dto.response.loginMessage;
import springwebsocket.webchat.member.entity.Member;

import java.util.Optional;
Expand All @@ -20,7 +21,7 @@ public interface MemberService {

void delete(Long id);

ResponseEntity<loginMessage> login(String loginEmail, String password);
TokenMessage login(String loginEmail, String password);

ResponseEntity<?> reissue(HttpServletRequest request, HttpServletResponse response);

Expand Down
Loading

0 comments on commit 2c7b8b9

Please sign in to comment.