-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
196 additions
and
88 deletions.
There are no files selected for viewing
22 changes: 0 additions & 22 deletions
22
BackEnd/src/main/java/springwebsocket/webchat/error/exception/ApplicationException.java
This file was deleted.
Oops, something went wrong.
14 changes: 0 additions & 14 deletions
14
BackEnd/src/main/java/springwebsocket/webchat/error/exception/InvalidValueException.java
This file was deleted.
Oops, something went wrong.
6 changes: 3 additions & 3 deletions
6
...nd/src/main/java/springwebsocket/webchat/friend/exception/FriendDuplicationException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
BackEnd/src/main/java/springwebsocket/webchat/global/error/BusinessException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
BackEnd/src/main/java/springwebsocket/webchat/global/error/exception/ExceptionAdvice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
BackEnd/src/main/java/springwebsocket/webchat/global/response/ApiResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
BackEnd/src/main/java/springwebsocket/webchat/global/response/ErrorCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
BackEnd/src/main/java/springwebsocket/webchat/global/response/ResultCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
BackEnd/src/main/java/springwebsocket/webchat/member/dto/response/TokenMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
14 changes: 0 additions & 14 deletions
14
BackEnd/src/main/java/springwebsocket/webchat/member/dto/response/loginMessage.java
This file was deleted.
Oops, something went wrong.
6 changes: 3 additions & 3 deletions
6
BackEnd/src/main/java/springwebsocket/webchat/member/exception/EmailDuplicatedException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.