-
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.
Merge pull request #10 from Central-MakeUs/feature/3
Feature/3: api ์๋ต ํต์ผ, global exception ์ฒ๋ฆฌ
- Loading branch information
Showing
20 changed files
with
469 additions
and
8 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
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
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,34 @@ | ||
package com.cmc.suppin.global.config; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.cors.CorsConfiguration; | ||
import org.springframework.web.cors.UrlBasedCorsConfigurationSource; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
import java.util.Arrays; | ||
|
||
|
||
@RequiredArgsConstructor | ||
@Configuration | ||
public class WebConfig implements WebMvcConfigurer { | ||
|
||
|
||
@Bean | ||
public UrlBasedCorsConfigurationSource corsConfigurationSource() { | ||
UrlBasedCorsConfigurationSource corsConfigSource = new UrlBasedCorsConfigurationSource(); | ||
|
||
|
||
CorsConfiguration configuration = new CorsConfiguration(); | ||
|
||
configuration.addAllowedOriginPattern("*"); | ||
configuration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS")); | ||
configuration.setAllowedHeaders(Arrays.asList("*")); | ||
configuration.setAllowCredentials(true); | ||
configuration.setMaxAge(3600L); | ||
|
||
corsConfigSource.registerCorsConfiguration("/**", configuration); | ||
return corsConfigSource; | ||
} | ||
} |
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,7 @@ | ||
package com.cmc.suppin.global.exception; | ||
|
||
public interface BaseCode { | ||
public ReasonDTO getReason(); | ||
|
||
public ReasonDTO getReasonHttpStatus(); | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/cmc/suppin/global/exception/BaseErrorCode.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,8 @@ | ||
package com.cmc.suppin.global.exception; | ||
|
||
public interface BaseErrorCode { | ||
|
||
public ErrorReasonDTO getReason(); | ||
|
||
public ErrorReasonDTO getReasonHttpStatus(); | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/com/cmc/suppin/global/exception/ErrorReasonDTO.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,23 @@ | ||
package com.cmc.suppin.global.exception; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
@Builder | ||
public class ErrorReasonDTO { | ||
|
||
private final HttpStatus httpStatus; | ||
|
||
private final boolean isSuccess; | ||
private final String code; | ||
private final String message; | ||
|
||
private final Integer status; | ||
private final String reason; | ||
|
||
public boolean getIsSuccess() { | ||
return isSuccess; | ||
} | ||
} |
115 changes: 115 additions & 0 deletions
115
src/main/java/com/cmc/suppin/global/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,115 @@ | ||
package com.cmc.suppin.global.exception; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.MethodArgumentNotValidException; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
import org.springframework.web.context.request.ServletWebRequest; | ||
import org.springframework.web.context.request.WebRequest; | ||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; | ||
import com.cmc.suppin.global.exception.status.ErrorStatus; | ||
import com.cmc.suppin.global.presentation.ApiResponse; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.validation.ConstraintViolationException; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
@Slf4j | ||
@RestControllerAdvice(annotations = {RestController.class}) | ||
public class ExceptionAdvice extends ResponseEntityExceptionHandler { | ||
|
||
@org.springframework.web.bind.annotation.ExceptionHandler | ||
public ResponseEntity<Object> validation(ConstraintViolationException e, WebRequest request) { | ||
String errorMessage = e.getConstraintViolations().stream() | ||
.map(constraintViolation -> constraintViolation.getMessage()) | ||
.findFirst() | ||
.orElseThrow(() -> new RuntimeException("ConstraintViolationException ์ถ์ถ ๋์ค ์๋ฌ ๋ฐ์")); | ||
|
||
return handleExceptionInternalConstraint(e, ErrorStatus.valueOf(errorMessage), HttpHeaders.EMPTY, request); | ||
} | ||
|
||
@Override | ||
public ResponseEntity<Object> handleMethodArgumentNotValid( | ||
MethodArgumentNotValidException e, HttpHeaders headers, HttpStatus status, WebRequest request) { | ||
|
||
Map<String, String> errors = new LinkedHashMap<>(); | ||
|
||
e.getBindingResult().getFieldErrors().stream() | ||
.forEach(fieldError -> { | ||
String fieldName = fieldError.getField(); | ||
String errorMessage = Optional.ofNullable(fieldError.getDefaultMessage()).orElse(""); | ||
errors.merge(fieldName, errorMessage, (existingErrorMessage, newErrorMessage) -> existingErrorMessage + ", " + newErrorMessage); | ||
}); | ||
|
||
return handleExceptionInternalArgs(e, HttpHeaders.EMPTY, ErrorStatus.valueOf("_BAD_REQUEST"), request, errors); | ||
} | ||
|
||
@org.springframework.web.bind.annotation.ExceptionHandler | ||
public ResponseEntity<Object> exception(Exception e, WebRequest request) { | ||
e.printStackTrace(); | ||
|
||
return handleExceptionInternalFalse(e, ErrorStatus._INTERNAL_SERVER_ERROR, HttpHeaders.EMPTY, ErrorStatus._INTERNAL_SERVER_ERROR.getHttpStatus(), request, e.getMessage()); | ||
} | ||
|
||
@ExceptionHandler(value = GeneralException.class) | ||
public ResponseEntity onThrowException(GeneralException generalException, HttpServletRequest request) { | ||
ErrorReasonDTO errorReasonHttpStatus = generalException.getErrorReasonHttpStatus(); | ||
return handleExceptionInternal(generalException, errorReasonHttpStatus, null, request); | ||
} | ||
|
||
private ResponseEntity<Object> handleExceptionInternal(Exception e, ErrorReasonDTO reason, HttpHeaders headers, HttpServletRequest request) { | ||
ApiResponse<Object> body = ApiResponse.onFailure(reason.getCode(), reason.getMessage(), null); | ||
// e.printStackTrace(); | ||
|
||
WebRequest webRequest = new ServletWebRequest(request); | ||
return super.handleExceptionInternal( | ||
e, | ||
body, | ||
headers, | ||
reason.getHttpStatus(), | ||
webRequest | ||
); | ||
} | ||
|
||
private ResponseEntity<Object> handleExceptionInternalFalse(Exception e, ErrorStatus errorCommonStatus, | ||
HttpHeaders headers, HttpStatus status, WebRequest request, String errorPoint) { | ||
ApiResponse<Object> body = ApiResponse.onFailure(errorCommonStatus.getCode(), errorCommonStatus.getMessage(), errorPoint); | ||
return super.handleExceptionInternal( | ||
e, | ||
body, | ||
headers, | ||
status, | ||
request | ||
); | ||
} | ||
|
||
private ResponseEntity<Object> handleExceptionInternalArgs(Exception e, HttpHeaders headers, ErrorStatus errorCommonStatus, | ||
WebRequest request, Map<String, String> errorArgs) { | ||
ApiResponse<Object> body = ApiResponse.onFailure(errorCommonStatus.getCode(), errorCommonStatus.getMessage(), errorArgs); | ||
return super.handleExceptionInternal( | ||
e, | ||
body, | ||
headers, | ||
errorCommonStatus.getHttpStatus(), | ||
request | ||
); | ||
} | ||
|
||
private ResponseEntity<Object> handleExceptionInternalConstraint(Exception e, ErrorStatus errorCommonStatus, | ||
HttpHeaders headers, WebRequest request) { | ||
ApiResponse<Object> body = ApiResponse.onFailure(errorCommonStatus.getCode(), errorCommonStatus.getMessage(), null); | ||
return super.handleExceptionInternal( | ||
e, | ||
body, | ||
headers, | ||
errorCommonStatus.getHttpStatus(), | ||
request | ||
); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/cmc/suppin/global/exception/GeneralException.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,19 @@ | ||
package com.cmc.suppin.global.exception; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class GeneralException extends RuntimeException { | ||
|
||
private BaseErrorCode code; | ||
|
||
public ErrorReasonDTO getErrorReason() { | ||
return this.code.getReason(); | ||
} | ||
|
||
public ErrorReasonDTO getErrorReasonHttpStatus() { | ||
return this.code.getReasonHttpStatus(); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/cmc/suppin/global/exception/ReasonDTO.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,20 @@ | ||
package com.cmc.suppin.global.exception; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
@Builder | ||
public class ReasonDTO { | ||
|
||
private HttpStatus httpStatus; | ||
|
||
private final boolean isSuccess; | ||
private final String code; | ||
private final String message; | ||
|
||
public boolean getIsSuccess() { | ||
return isSuccess; | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/com/cmc/suppin/global/exception/handler/GlobalExceptionHandler.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,4 @@ | ||
package com.cmc.suppin.global.exception.handler; | ||
|
||
public class GlobalExceptionHandler { | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/cmc/suppin/global/exception/handler/JwtHandler.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,10 @@ | ||
package com.cmc.suppin.global.exception.handler; | ||
|
||
import com.cmc.suppin.global.exception.BaseErrorCode; | ||
import com.cmc.suppin.global.exception.GeneralException; | ||
|
||
public class JwtHandler extends GeneralException { | ||
public JwtHandler(BaseErrorCode code) { | ||
super(code); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/com/cmc/suppin/global/exception/status/ErrorStatus.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,60 @@ | ||
package com.cmc.suppin.global.exception.status; | ||
|
||
import com.cmc.suppin.global.exception.BaseErrorCode; | ||
import com.cmc.suppin.global.exception.ErrorReasonDTO; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public enum ErrorStatus implements BaseErrorCode { | ||
|
||
// ๊ฐ์ฅ ์ผ๋ฐ์ ์ธ ์๋ฌ | ||
_INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "COMMON500", "์๋ฒ ์๋ฌ, ๊ด๋ฆฌ์์๊ฒ ๋ฌธ์ ๋ฐ๋๋๋ค."), | ||
_BAD_REQUEST(HttpStatus.BAD_REQUEST, "COMMON400", "์๋ชป๋ ์์ฒญ์ ๋๋ค."), | ||
_UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "COMMON401", "์ธ์ฆ์ด ํ์ํฉ๋๋ค."), | ||
_FORBIDDEN(HttpStatus.FORBIDDEN, "COMMON403", "๊ธ์ง๋ ์์ฒญ์ ๋๋ค."), | ||
|
||
|
||
// test | ||
TEMP_EXCEPTION(HttpStatus.BAD_REQUEST, "TEMP4001", "ํ ์คํธ"), | ||
|
||
// Member | ||
MEMBER_NICKNAME_DUPLICATED(HttpStatus.BAD_REQUEST, "MEMBER4001", "์ค๋ณต๋ ๋๋ค์ ์ ๋๋ค."), | ||
MEMBER_PASSWORD_ERROR(HttpStatus.BAD_REQUEST, "MEMBER4002", "๋น๋ฐ๋ฒํธ๊ฐ ์๋ชป๋์์ต๋๋ค."), | ||
|
||
//JWT | ||
JWT_BAD_REQUEST(HttpStatus.UNAUTHORIZED, "JWT4001", "์๋ชป๋ JWT ์๋ช ์ ๋๋ค."), | ||
JWT_ACCESS_TOKEN_EXPIRED(HttpStatus.UNAUTHORIZED, "JWT4002", "์ก์ธ์ค ํ ํฐ์ด ๋ง๋ฃ๋์์ต๋๋ค."), | ||
JWT_REFRESH_TOKEN_EXPIRED(HttpStatus.UNAUTHORIZED, "JWT4003", "๋ฆฌํ๋ ์ ํ ํฐ์ด ๋ง๋ฃ๋์์ต๋๋ค. ๋ค์ ๋ก๊ทธ์ธํ์๊ธฐ ๋ฐ๋๋๋ค."), | ||
JWT_UNSUPPORTED_TOKEN(HttpStatus.UNAUTHORIZED, "JWT4004", "์ง์ํ์ง ์๋ JWT ํ ํฐ์ ๋๋ค."), | ||
JWT_TOKEN_NOT_FOUND(HttpStatus.UNAUTHORIZED, "JWT4005", "์ ํจํ JWT ํ ํฐ์ด ์์ต๋๋ค."), | ||
|
||
// ํ์ด์ง ๊ด๋ จ ์๋ฌ | ||
PAGE_NEGATIVE_INPUT(HttpStatus.BAD_REQUEST, "PAGE4001", "ํ์ด์ง ๋ฒํธ๋ 1์ด์์ ์ซ์์ฌ์ผ ํฉ๋๋ค."), | ||
; | ||
|
||
private final HttpStatus httpStatus; | ||
private final String code; | ||
private final String message; | ||
|
||
@Override | ||
public ErrorReasonDTO getReason() { | ||
return ErrorReasonDTO.builder() | ||
.message(message) | ||
.code(code) | ||
.isSuccess(false) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public ErrorReasonDTO getReasonHttpStatus() { | ||
return ErrorReasonDTO.builder() | ||
.message(message) | ||
.code(code) | ||
.isSuccess(false) | ||
.httpStatus(httpStatus) | ||
.build(); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/main/java/com/cmc/suppin/global/exception/status/SuccessStatus.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,40 @@ | ||
package com.cmc.suppin.global.exception.status; | ||
|
||
import com.cmc.suppin.global.exception.BaseCode; | ||
import com.cmc.suppin.global.exception.ReasonDTO; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public enum SuccessStatus implements BaseCode { | ||
_OK(HttpStatus.OK, "COMMON200", "์ฑ๊ณต์ ๋๋ค."), | ||
|
||
//Member | ||
MEMBER_DELETE_SUCCESS(HttpStatus.OK, "MEMBER2001", "ํ์ ํํด ์ฑ๊ณต์ ๋๋ค."), | ||
; | ||
|
||
private final HttpStatus httpStatus; | ||
private final String code; | ||
private final String message; | ||
|
||
@Override | ||
public ReasonDTO getReason() { | ||
return ReasonDTO.builder() | ||
.message(message) | ||
.code(code) | ||
.isSuccess(true) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public ReasonDTO getReasonHttpStatus() { | ||
return ReasonDTO.builder() | ||
.message(message) | ||
.code(code) | ||
.isSuccess(true) | ||
.httpStatus(httpStatus) | ||
.build(); | ||
} | ||
} |
Oops, something went wrong.