-
Notifications
You must be signed in to change notification settings - Fork 1
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
4932f0b
commit bdcaa81
Showing
4 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
src/main/java/com/baro/common/exception/CommonRequestExceptionType.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,24 @@ | ||
package com.baro.common.exception; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@RequiredArgsConstructor | ||
public enum CommonRequestExceptionType implements RequestExceptionType { | ||
|
||
MISSING_PARAMETER_EXCEPTION("Request parameter is empty", HttpStatus.BAD_REQUEST), | ||
; | ||
|
||
private final String errorMessage; | ||
private final HttpStatus httpStatus; | ||
|
||
@Override | ||
public String errorMessage() { | ||
return errorMessage; | ||
} | ||
|
||
@Override | ||
public HttpStatus httpStatus() { | ||
return httpStatus; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/baro/common/exception/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,25 @@ | ||
package com.baro.common.exception; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
@Slf4j | ||
@RestControllerAdvice | ||
class GlobalExceptionHandler { | ||
|
||
@ExceptionHandler(value = {RequestException.class}) | ||
ResponseEntity<String> handleBadRequestException(RequestException e) { | ||
log.warn("[handleRequestException throw RequestException : {}", e.getMessage()); | ||
RequestExceptionType exceptionType = e.exceptionType(); | ||
return ResponseEntity.status(exceptionType.httpStatus()).body(exceptionType.errorMessage()); | ||
} | ||
|
||
@ExceptionHandler(Exception.class) | ||
ResponseEntity<String> handleException(HttpServletRequest request, Exception e) { | ||
log.error("[ERROR] Unexpected error occurred. FROM URI: {}, ", request.getRequestURI(), e); | ||
return ResponseEntity.internalServerError().body("Unknown error occurred. Please try later"); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/com/baro/common/exception/RequestException.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,5 @@ | ||
package com.baro.common.exception; | ||
|
||
abstract class RequestException extends RuntimeException{ | ||
abstract RequestExceptionType exceptionType(); | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/baro/common/exception/RequestExceptionType.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.baro.common.exception; | ||
|
||
import org.springframework.http.HttpStatus; | ||
|
||
public interface RequestExceptionType { | ||
|
||
String errorMessage(); | ||
|
||
HttpStatus httpStatus(); | ||
} |