-
Notifications
You must be signed in to change notification settings - Fork 2
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
1 parent
a483521
commit 37aebaf
Showing
13 changed files
with
170 additions
and
23 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
src/main/java/gdsc/pointer/controller/ControllerAdvice.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,53 @@ | ||
package gdsc.pointer.controller; | ||
|
||
import gdsc.pointer.dto.response.error.ErrorResponse; | ||
import gdsc.pointer.exception.PointerException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.validation.FieldError; | ||
import org.springframework.web.bind.MethodArgumentNotValidException; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
import java.sql.SQLException; | ||
import java.util.Objects; | ||
|
||
@Slf4j | ||
@RestControllerAdvice | ||
public class ControllerAdvice { | ||
private static final int FIELD_ERROR_CODE_INDEX = 0; | ||
private static final int FIELD_ERROR_MESSAGE_INDEX = 1; | ||
|
||
@ExceptionHandler(MethodArgumentNotValidException.class) | ||
public ResponseEntity<ErrorResponse> handleInputFieldException(MethodArgumentNotValidException e) { | ||
FieldError mainError = e.getFieldErrors().get(0); | ||
String[] errorInfo = Objects.requireNonNull(mainError.getDefaultMessage()).split(":"); | ||
|
||
int code = Integer.parseInt(errorInfo[FIELD_ERROR_CODE_INDEX]); | ||
String message = errorInfo[FIELD_ERROR_MESSAGE_INDEX]; | ||
|
||
return ResponseEntity.badRequest().body(new gdsc.pointer.dto.response.error.ErrorResponse(code, message)); | ||
} | ||
|
||
@ExceptionHandler(SQLException.class) | ||
public ResponseEntity<ErrorResponse> sqlExceptionHandle(PointerException e) { | ||
return ResponseEntity.status(e.getHttpStatus()).body(new ErrorResponse(e.getCode(), e.getMessage())); | ||
} | ||
|
||
@ExceptionHandler(PointerException.class) | ||
public ResponseEntity<ErrorResponse> handleSubwayException(PointerException e) { | ||
return ResponseEntity.status(e.getHttpStatus()).body(new ErrorResponse(e.getCode(), e.getMessage())); | ||
} | ||
|
||
@ExceptionHandler(Exception.class) | ||
public ResponseEntity<ErrorResponse> unhandledException(Exception e, HttpServletRequest request) { | ||
log.error("UnhandledException: {} {} errMessage={}\n", | ||
request.getMethod(), | ||
request.getRequestURI(), | ||
e.getMessage() | ||
); | ||
return ResponseEntity.internalServerError() | ||
.body(new ErrorResponse(9999, "일시적으로 접속이 원활하지 않습니다. 지하철 서비스 팀에 문의 부탁드립니다.")); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ | |
public class User { | ||
|
||
private String id; | ||
private String name; | ||
private String email; | ||
private String nickname; | ||
|
||
} |
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
14 changes: 14 additions & 0 deletions
14
src/main/java/gdsc/pointer/dto/response/error/ErrorResponse.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 gdsc.pointer.dto.response.error; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@AllArgsConstructor | ||
public class ErrorResponse { | ||
private int code; | ||
private 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
11 changes: 11 additions & 0 deletions
11
src/main/java/gdsc/pointer/exception/notfound/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,11 @@ | ||
package gdsc.pointer.exception.notfound; | ||
|
||
import gdsc.pointer.exception.ExceptionContext; | ||
import gdsc.pointer.exception.PointerException; | ||
import org.springframework.http.HttpStatus; | ||
|
||
public class NotFoundException extends PointerException { | ||
public NotFoundException(ExceptionContext context) { | ||
super(HttpStatus.NOT_FOUND, context.getMessage(), context.getCode()); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/gdsc/pointer/exception/notfound/user/UserNotFoundException.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,11 @@ | ||
package gdsc.pointer.exception.notfound.user; | ||
|
||
import gdsc.pointer.exception.notfound.NotFoundException; | ||
|
||
import static gdsc.pointer.exception.CustomExceptionContext.USER_NOT_FOUND_ERROR; | ||
|
||
public class UserNotFoundException extends NotFoundException { | ||
public UserNotFoundException() { | ||
super(USER_NOT_FOUND_ERROR); | ||
} | ||
} |
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