-
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 #202 from RandomJusicool/feature/201-exception-dis…
…cord-webhook 🔀 :: 예외 발생시 디스코드 알림
- Loading branch information
Showing
7 changed files
with
98 additions
and
13 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
11 changes: 11 additions & 0 deletions
11
src/main/java/com/juicycool/backend/global/filter/event/ErrorLoggingEvent.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,11 @@ | ||
package com.juicycool.backend.global.filter.event; | ||
|
||
import com.juicycool.backend.global.exception.ErrorCode; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class ErrorLoggingEvent { | ||
private ErrorCode errorCode; | ||
} |
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
69 changes: 69 additions & 0 deletions
69
src/main/java/com/juicycool/backend/global/thirdparty/discord/ErrorEventHandler.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,69 @@ | ||
package com.juicycool.backend.global.thirdparty.discord; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonPrimitive; | ||
import com.juicycool.backend.global.exception.ErrorResponse; | ||
import com.juicycool.backend.global.filter.event.ErrorLoggingEvent; | ||
import com.juicycool.backend.global.thirdparty.discord.properties.DiscordProperties; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.io.IOException; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class ErrorEventHandler { | ||
|
||
private final ObjectMapper objectMapper; | ||
private final DiscordProperties discordProperties; | ||
|
||
@EventListener | ||
public void exceptionLoggingHandler(ErrorLoggingEvent errorLoggingEvent) throws IOException { | ||
ErrorResponse errorResponse = new ErrorResponse(errorLoggingEvent.getErrorCode().getStatus(), errorLoggingEvent.getErrorCode().getMessage()); | ||
String responseString = objectMapper.writeValueAsString(errorResponse); | ||
|
||
JsonObject statusField = new JsonObject(); | ||
statusField.add("name", new JsonPrimitive("상태코드")); | ||
statusField.add("value", new JsonPrimitive(errorLoggingEvent.getErrorCode().getStatus())); | ||
|
||
JsonObject contentTypeField = new JsonObject(); | ||
contentTypeField.add("name", new JsonPrimitive("CONTENT-TYPE")); | ||
contentTypeField.add("value", new JsonPrimitive(MediaType.APPLICATION_JSON_VALUE)); | ||
|
||
JsonObject writerField = new JsonObject(); | ||
writerField.add("name", new JsonPrimitive("Exception")); | ||
writerField.add("value", new JsonPrimitive(responseString)); | ||
|
||
JsonArray fields = new JsonArray(); | ||
fields.add(statusField); | ||
fields.add(contentTypeField); | ||
fields.add(writerField); | ||
|
||
JsonObject jsonObject = new JsonObject(); | ||
jsonObject.add("content", new JsonPrimitive("")); | ||
|
||
JsonObject embed = new JsonObject(); | ||
embed.add("description", new JsonPrimitive("에러 로그")); | ||
embed.add("color", new JsonPrimitive(16711680)); | ||
embed.add("fields", fields); | ||
|
||
JsonArray embeds = new JsonArray(); | ||
embeds.add(embed); | ||
jsonObject.add("embeds", embeds); | ||
|
||
|
||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setContentType(MediaType.APPLICATION_JSON); | ||
|
||
RestTemplate restTemplate = new RestTemplate(); | ||
HttpEntity<String> entity = new HttpEntity<>(jsonObject.toString(), headers); | ||
restTemplate.postForObject(discordProperties.getErrorURL(), entity, String.class); | ||
} | ||
} |
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
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