-
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 #54 from YAPP-Github/MAFOO-38
[MAFOO-37] feat: ์ฌ์ง ์จ๋ฒ ๋๋ฉ์ธ(photo-service) ์ฌ๋ ์๋ฆผ, ์ค๋ฅ ๋ฉ์์ง ๋ณด๋ด๊ธฐ
- Loading branch information
Showing
5 changed files
with
170 additions
and
1 deletion.
There are no files selected for viewing
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
photo-service/src/main/java/kr/mafoo/photo/config/SlackApiConfig.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 kr.mafoo.photo.config; | ||
|
||
import com.slack.api.Slack; | ||
import com.slack.api.methods.MethodsClient; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class SlackApiConfig { | ||
|
||
@Value("${slack.webhook.token}") | ||
private String token; | ||
|
||
@Bean | ||
public MethodsClient getClient() { | ||
Slack slackClient = Slack.getInstance(); | ||
return slackClient.methods(token); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
photo-service/src/main/java/kr/mafoo/photo/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.photo.handler; | ||
|
||
import kr.mafoo.photo.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)); | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
photo-service/src/main/java/kr/mafoo/photo/service/SlackService.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,92 @@ | ||
package kr.mafoo.photo.service; | ||
|
||
import com.slack.api.methods.MethodsClient; | ||
import com.slack.api.methods.request.chat.ChatPostMessageRequest; | ||
import com.slack.api.model.block.Blocks; | ||
import com.slack.api.model.block.LayoutBlock; | ||
import com.slack.api.model.block.composition.MarkdownTextObject; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
import reactor.core.publisher.Mono; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static com.slack.api.model.block.Blocks.*; | ||
import static com.slack.api.model.block.composition.BlockCompositions.plainText; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class SlackService { | ||
|
||
@Value(value = "${slack.webhook.token}") | ||
private String token; | ||
|
||
@Value(value = "${slack.webhook.channel.error}") | ||
private String errorChannel; | ||
|
||
private final MethodsClient methodsClient; | ||
|
||
public Mono<Void> sendErrorNotification(String method, String uri, String originIp, String userAgent, String message) { | ||
return Mono.fromCallable(() -> { | ||
List<LayoutBlock> layoutBlocks = new ArrayList<>(); | ||
|
||
// Header ์ฝ์ | ||
layoutBlocks.add( | ||
Blocks.header( | ||
headerBlockBuilder -> | ||
headerBlockBuilder.text(plainText("๐จ ์์ํ์ง ๋ชปํ ์๋ฌ ๋ฐ์")) | ||
) | ||
); | ||
|
||
layoutBlocks.add(divider()); | ||
|
||
// Content ์ฝ์ | ||
MarkdownTextObject errorMethodMarkdown = | ||
MarkdownTextObject.builder().text("`METHOD`\n" + method).build(); | ||
|
||
MarkdownTextObject errorUriMarkdown = | ||
MarkdownTextObject.builder().text("`URI`\n" + uri).build(); | ||
|
||
layoutBlocks.add( | ||
section( | ||
section -> section.fields(List.of(errorMethodMarkdown, errorUriMarkdown)) | ||
) | ||
); | ||
|
||
MarkdownTextObject errorOriginIpMarkdown = | ||
MarkdownTextObject.builder().text("`์๋ฌ ๋ฐ์ IP`\n" + originIp).build(); | ||
|
||
MarkdownTextObject errorUserAgentMarkdown = | ||
MarkdownTextObject.builder().text("`์๋ฌ ๋ฐ์ ํ๊ฒฝ`\n" + userAgent).build(); | ||
|
||
layoutBlocks.add( | ||
section( | ||
section -> section.fields(List.of(errorOriginIpMarkdown, errorUserAgentMarkdown)) | ||
) | ||
); | ||
|
||
MarkdownTextObject errorMessageMarkdown = | ||
MarkdownTextObject.builder().text("`๋ฉ์ธ์ง`\n" + message).build(); | ||
|
||
layoutBlocks.add( | ||
section( | ||
section -> section.fields(List.of(errorMessageMarkdown)) | ||
) | ||
); | ||
|
||
ChatPostMessageRequest chatPostMessageRequest = | ||
ChatPostMessageRequest | ||
.builder() | ||
.text("์์ํ์ง ๋ชปํ ์๋ฌ ๋ฐ์ ์๋ฆผ") | ||
.channel(errorChannel) | ||
.blocks(layoutBlocks) | ||
.build(); | ||
|
||
return methodsClient.chatPostMessage(chatPostMessageRequest); | ||
|
||
}).then(); | ||
} | ||
|
||
} |
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