-
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
146 additions
and
25 deletions.
There are no files selected for viewing
22 changes: 12 additions & 10 deletions
22
...ar-application/src/main/java/com/threedollar/config/advice/ControllerExceptionAdvice.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 |
---|---|---|
@@ -1,32 +1,34 @@ | ||
package com.threedollar.config.advice; | ||
|
||
import com.threedollar.common.exception.dto.response.ApiResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import com.threedollar.common.exception.exception.NotFoundException; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.context.support.DefaultMessageSourceResolvable; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.validation.BindException; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
|
||
|
||
import static com.threedollar.common.exception.ErrorCode.E400_INVALID; | ||
import static java.util.stream.Collectors.joining; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
@RestControllerAdvice | ||
public class ControllerExceptionAdvice { | ||
|
||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
@ExceptionHandler(BindException.class) | ||
private ApiResponse<Object> handleBadRequest(BindException exception) { | ||
String errorMessage = exception.getBindingResult().getFieldErrors().stream() | ||
.map(DefaultMessageSourceResolvable::getDefaultMessage) | ||
.collect(joining("\n")); | ||
log.error("BindException: {}", errorMessage); | ||
return ApiResponse.error(E400_INVALID, errorMessage); | ||
private ApiResponse<Object> handleBadRequest(BindException e) { | ||
log.error(e.getMessage(), e); | ||
return ApiResponse.error(E400_INVALID, e.getMessage()); | ||
} | ||
|
||
@ResponseStatus(HttpStatus.NOT_FOUND) | ||
@ExceptionHandler(NotFoundException.class) | ||
private ApiResponse<Object> handleNotFound(NotFoundException e) { | ||
log.error(e.getErrorCode().getMessage(), e); | ||
return ApiResponse.error(e.getErrorCode(), e.getMessage()); | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
threedollar-application/src/main/java/com/threedollar/config/advice/WebDataBinderConfig.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,14 @@ | ||
package com.threedollar.config.advice; | ||
|
||
import org.springframework.web.bind.WebDataBinder; | ||
import org.springframework.web.bind.annotation.InitBinder; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
@RestControllerAdvice | ||
public class WebDataBinderConfig { | ||
|
||
@InitBinder | ||
public void initBinder(WebDataBinder binder) { | ||
binder.initDirectFieldAccess(); | ||
} | ||
} |
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
20 changes: 20 additions & 0 deletions
20
...lar-application/src/main/java/com/threedollar/service/poll/service/PollServiceHelper.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.threedollar.service.poll.service; | ||
|
||
import com.threedollar.common.exception.exception.NotFoundException; | ||
import com.threedollar.domain.AccountType; | ||
import com.threedollar.domain.poll.Poll; | ||
import com.threedollar.domain.poll.repository.PollRepository; | ||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
|
||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class PollServiceHelper { | ||
|
||
public static Poll getPollByIdAndAccountType(PollRepository pollRepository, Long pollId, AccountType accountType, String accountId) { | ||
Poll poll = pollRepository.findByPollIdAndAccount(pollId, accountType, accountId); | ||
if(poll == null) { | ||
throw new NotFoundException(String.format("(%s)에 해당하는 투표는 존재하지 않습니다.", pollId)); | ||
} | ||
return poll; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
public enum HttpStatusCode { | ||
|
||
BAD_REQUEST(400), | ||
NOT_FOUND(404), | ||
; | ||
|
||
private final int status; | ||
|
22 changes: 22 additions & 0 deletions
22
...ar-common/src/main/java/com/threedollar/common/exception/exception/NotFoundException.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 com.threedollar.common.exception.exception; | ||
|
||
import com.threedollar.common.exception.ErrorCode; | ||
import com.threedollar.common.exception.model.BaseException; | ||
|
||
public class NotFoundException extends BaseException { | ||
|
||
private static final ErrorCode DEFAULT_ERROR_CODE = ErrorCode.E404_NOT_FOUND; | ||
|
||
public NotFoundException(String message) { | ||
super(message, DEFAULT_ERROR_CODE); | ||
} | ||
|
||
public NotFoundException(String message, ErrorCode errorCode) { | ||
super(message, errorCode); | ||
} | ||
|
||
public NotFoundException(String message, ErrorCode errorCode, Throwable cause) { | ||
super(message, errorCode, cause); | ||
} | ||
|
||
} |
4 changes: 0 additions & 4 deletions
4
...dollar-common/src/main/java/com/threedollar/common/exception/model/NotFoundException.java
This file was deleted.
Oops, something went wrong.
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
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