-
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.
Merge pull request #55 from YAPP-Github/MAFOO-69
[MAFOO-69] feat: ์ ์ ๋๋ฉ์ธ(user-service) ์ฌ๋ ์๋ฆผ, ์ค๋ฅ ๋ฉ์์ง ๋ณด๋ด๊ธฐ
- Loading branch information
Showing
3 changed files
with
173 additions
and
81 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
user-service/src/main/java/kr/mafoo/user/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,56 @@ | ||
package kr.mafoo.user.handler; | ||
|
||
import kr.mafoo.user.service.SlackService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.server.ResponseStatusException; | ||
import org.springframework.web.server.ServerWebExchange; | ||
import reactor.core.publisher.Mono; | ||
|
||
import java.net.InetSocketAddress; | ||
|
||
@ControllerAdvice | ||
@RequiredArgsConstructor | ||
public class GlobalExceptionHandler { | ||
private final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class); | ||
private final SlackService slackService; | ||
|
||
@ExceptionHandler(ResponseStatusException.class) | ||
public Mono<ResponseEntity<String>> handleResponseStatusException(ServerWebExchange exchange, ResponseStatusException ex) { | ||
return handleExceptionInternal(exchange, ex, (HttpStatus) ex.getStatusCode()); | ||
} | ||
|
||
@ExceptionHandler(Exception.class) | ||
public Mono<ResponseEntity<String>> handleGenericException(ServerWebExchange exchange, Exception ex) { | ||
return handleExceptionInternal(exchange, ex, HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
|
||
private Mono<ResponseEntity<String>> handleExceptionInternal(ServerWebExchange exchange, Exception ex, HttpStatus status) { | ||
String method = exchange.getRequest().getMethod().toString(); | ||
String userAgent = exchange.getRequest().getHeaders().getFirst("User-Agent"); | ||
String proxyIp = exchange.getRequest().getHeaders().getFirst("X-Forwarded-For"); | ||
InetSocketAddress address = exchange.getRequest().getRemoteAddress(); | ||
String originIp = proxyIp != null ? proxyIp : (address != null ? address.toString() : "UNKNOWN SOURCE"); | ||
String fullPath = exchange.getRequest().getURI().getPath() + | ||
(exchange.getRequest().getURI().getQuery() != null ? "?" + exchange.getRequest().getURI().getQuery() : ""); | ||
|
||
logger.error("Exception occurred: {} {} {} ERROR {} {}", method, fullPath, originIp, ex.getMessage(), userAgent); | ||
|
||
if (status == HttpStatus.INTERNAL_SERVER_ERROR) { | ||
return slackService.sendErrorNotification( | ||
method, | ||
fullPath, | ||
originIp, | ||
userAgent, | ||
ex.getMessage() | ||
).then(Mono.just(new ResponseEntity<>("Internal Server Error", HttpStatus.INTERNAL_SERVER_ERROR))); | ||
} | ||
|
||
return Mono.just(new ResponseEntity<>(status.getReasonPhrase(), status)); | ||
} | ||
} |
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