-
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.
#578 [feat] 메신저 이관으로 메시지 전송 모듈 구현
- Loading branch information
Showing
5 changed files
with
76 additions
and
19 deletions.
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
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
59 changes: 59 additions & 0 deletions
59
module-external/src/main/java/com/mile/slack/module/SendErrorModule.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,59 @@ | ||
package com.mile.slack.module; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.slf4j.MDC; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.lang.NonNull; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.reactive.function.client.WebClient; | ||
|
||
@Slf4j | ||
@Component | ||
public class SendErrorModule extends SendWebhookMessage { | ||
private final StringBuilder sb = new StringBuilder(); | ||
|
||
@Value("${webhook.url-for-error}") | ||
private String webHookUri; | ||
|
||
@Value("${spring.profiles.active}") | ||
private String profile; | ||
|
||
@Override | ||
public void sendError(@NonNull final Exception exception) { | ||
WebClient webClient = WebClient.builder() | ||
.baseUrl(webHookUri).build(); | ||
|
||
|
||
webClient.post() | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.bodyValue(generateMessage(exception)) | ||
.accept(MediaType.APPLICATION_JSON) | ||
.retrieve() | ||
.bodyToMono(String.class) | ||
.doOnError(e -> log.error("WEB HOOK 전송 중 에러 발생 -> {}", e.getMessage())) | ||
.subscribe(); | ||
} | ||
|
||
|
||
private String readRootStackTrace(Exception error) { | ||
return error.getStackTrace()[0].toString(); | ||
} | ||
|
||
private Message generateMessage(final Exception exception) { | ||
sb.append("🚨 ERROR🚨").append("\n").append(exception.toString()).append("\n").append("\n"); | ||
sb.append("🏃🏻PROFILE🏃🏻").append("\n").append(profile).append("\n").append("\n"); | ||
sb.append("🆔REQUEST ID🆔").append("\n").append(MDC.get("request_id")).append("\n").append("\n"); | ||
sb.append("️✏️DETAILS✏️").append("\n").append(readRootStackTrace(exception)).append("\n"); | ||
|
||
return new Message(sb.toString()); | ||
} | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
private class Message { | ||
private String text; | ||
} | ||
} |
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
12 changes: 7 additions & 5 deletions
12
module-external/src/main/java/com/mile/slack/module/SendWebhookMessage.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,8 +1,10 @@ | ||
package com.mile.slack.module; | ||
|
||
import org.springframework.lang.NonNull; | ||
|
||
public interface SendWebhookMessage { | ||
void sendMessage(@NonNull final String message); | ||
|
||
public abstract class SendWebhookMessage { | ||
void sendMessage(String message) { | ||
// TODO() - 상속받아 구현하기 | ||
} | ||
void sendError(Exception exception){ | ||
// TODO() - 상속받아 구현하기 | ||
} | ||
} |