-
Notifications
You must be signed in to change notification settings - Fork 0
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 #161 from Gongjakso/feat/discord
feat: 디스코드 웹훅 연동
- Loading branch information
Showing
2 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
11 changes: 10 additions & 1 deletion
11
src/main/java/com/gongjakso/server/global/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
64 changes: 64 additions & 0 deletions
64
src/main/java/com/gongjakso/server/global/util/discord/DiscordClient.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,64 @@ | ||
package com.gongjakso.server.global.util.discord; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.reactive.function.client.WebClient; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@Component | ||
public class DiscordClient { | ||
|
||
@Value("${discord.environment}") | ||
private String environment; | ||
|
||
@Value("${discord.webhook-url}") | ||
private String webhookUrl; | ||
|
||
public void sendErrorMessage(Integer code, String message, String stackTrace) { | ||
if(!environment.equals("prod")) { | ||
return; | ||
} | ||
|
||
WebClient webClient = WebClient.create(); | ||
|
||
//요청 본문 | ||
Map<String, Object> embedData = new HashMap<>(); | ||
|
||
embedData.put("title", "공작소 서버 에러 발생"); | ||
|
||
Map<String, String> field1 = new HashMap<>(); | ||
field1.put("name", "발생시각"); | ||
field1.put("value", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss"))); | ||
|
||
Map<String, String> field2 = new HashMap<>(); | ||
field2.put("name", "에러 코드"); | ||
field2.put("value", code.toString()); | ||
|
||
Map<String, String> field3 = new HashMap<>(); | ||
field3.put("name", "에러 명"); | ||
field3.put("value", message); | ||
|
||
Map<String, String> field4 = new HashMap<>(); | ||
field4.put("name", "스택 트레이스"); | ||
field4.put("value", stackTrace); | ||
|
||
embedData.put("fields", List.of(field1, field2, field3, field4)); | ||
|
||
Map<String, Object> payload = new HashMap<>(); | ||
payload.put("embeds", new Object[]{embedData}); | ||
|
||
webClient.post() | ||
.uri(webhookUrl) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.bodyValue(payload) | ||
.retrieve() | ||
.bodyToMono(Void.class) | ||
.block(); | ||
} | ||
} |