-
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.
Browse files
Browse the repository at this point in the history
[Feat/#4] 에러 발생 시 Discord 알림 전송 기능 구현
- Loading branch information
Showing
16 changed files
with
240 additions
and
102 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
6 changes: 3 additions & 3 deletions
6
src/main/java/corecord/dev/common/exception/GeneralException.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
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
38 changes: 38 additions & 0 deletions
38
src/main/java/corecord/dev/common/log/discord/DiscordAlarmSender.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,38 @@ | ||
package corecord.dev.common.log.discord; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.reactive.function.client.WebClient; | ||
|
||
import java.util.Arrays; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class DiscordAlarmSender { | ||
|
||
private final Environment environment; | ||
@Value("${logging.discord.web-hook-url}") | ||
private String webHookUrl; | ||
|
||
private final DiscordUtil discordUtil; | ||
private final WebClient webClient = WebClient.create(); | ||
|
||
public Void sendDiscordAlarm(Exception exception, HttpServletRequest httpServletRequest) { | ||
if (Arrays.asList(environment.getActiveProfiles()).contains("dev")) { | ||
return webClient.post() | ||
.uri(webHookUrl) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.bodyValue(discordUtil.createMessage(exception, httpServletRequest)) | ||
.retrieve() | ||
.bodyToMono(Void.class) | ||
.block(); | ||
} | ||
return null; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/main/java/corecord/dev/common/log/discord/DiscordLoggerAop.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,40 @@ | ||
package corecord.dev.common.log.discord; | ||
|
||
import corecord.dev.common.exception.GeneralException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.aspectj.lang.JoinPoint; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.aspectj.lang.annotation.Before; | ||
import org.aspectj.lang.annotation.Pointcut; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.context.request.RequestContextHolder; | ||
import org.springframework.web.context.request.ServletRequestAttributes; | ||
|
||
@Slf4j | ||
@Aspect | ||
@Component | ||
@RequiredArgsConstructor | ||
public class DiscordLoggerAop { | ||
|
||
private final DiscordAlarmSender discordAlarmSender; | ||
|
||
@Pointcut("execution(* corecord.dev.common.exception.GeneralExceptionAdvice..*(..))") | ||
public void generalExceptionErrorLoggerExecute() {} | ||
|
||
@Before("generalExceptionErrorLoggerExecute()") | ||
public void serverErrorLogging(JoinPoint joinpoint) { | ||
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest(); | ||
Object[] args = joinpoint.getArgs(); | ||
|
||
if (args[0] instanceof GeneralException exception) { | ||
if (exception.getErrorStatus().getHttpStatus() == HttpStatus.INTERNAL_SERVER_ERROR) | ||
discordAlarmSender.sendDiscordAlarm(exception, request); | ||
} else { | ||
Exception exception = (Exception) args[0]; | ||
discordAlarmSender.sendDiscordAlarm(exception, request); | ||
} | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/main/java/corecord/dev/common/log/discord/DiscordUtil.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,62 @@ | ||
package corecord.dev.common.log.discord; | ||
|
||
import corecord.dev.common.log.discord.dto.DiscordDto; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.io.PrintWriter; | ||
import java.io.StringWriter; | ||
import java.security.Principal; | ||
import java.time.ZoneId; | ||
import java.time.ZonedDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.List; | ||
|
||
|
||
@Component | ||
public class DiscordUtil { | ||
public DiscordDto.MessageDto createMessage(Exception exception, HttpServletRequest httpServletRequest) { | ||
return DiscordDto.MessageDto.builder() | ||
.content("# 🚨 서버 에러 발생 🚨") | ||
.embeds(List.of(DiscordDto.EmbedDto.builder() | ||
.title("에러 정보") | ||
.description("### 에러 발생 시간\n" | ||
+ ZonedDateTime.now(ZoneId.of("Asia/Seoul")).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH시 mm분 ss초")) | ||
+ "\n" | ||
+ "### 요청 엔드포인트\n" | ||
+ getEndPoint(httpServletRequest) | ||
+ "\n" | ||
+ "### 요청 클라이언트\n" | ||
+ getClient(httpServletRequest) | ||
+"\n" | ||
+ "### 에러 스택 트레이스\n" | ||
+ "```\n" | ||
+ getStackTrace(exception).substring(0, 1000) | ||
+ "\n```") | ||
.build() | ||
) | ||
).build(); | ||
} | ||
|
||
private String getClient(HttpServletRequest httpServletRequest) { | ||
String ip = httpServletRequest.getRemoteAddr(); | ||
|
||
Principal principal = httpServletRequest.getUserPrincipal(); | ||
if (principal != null) { | ||
return "[IP] : " + ip + " / [Id] : " + principal.getName(); | ||
} | ||
return "[IP] : " + ip; | ||
} | ||
|
||
private String getEndPoint(HttpServletRequest httpServletRequest) { | ||
String method = httpServletRequest.getMethod(); | ||
String url = httpServletRequest.getRequestURI(); | ||
return method + " " + url; | ||
} | ||
|
||
private String getStackTrace(Exception exception) { | ||
StringWriter stringWriter = new StringWriter(); | ||
exception.printStackTrace(new PrintWriter(stringWriter)); | ||
return stringWriter.toString(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/corecord/dev/common/log/discord/dto/DiscordDto.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,35 @@ | ||
package corecord.dev.common.log.discord.dto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.util.List; | ||
|
||
public class DiscordDto { | ||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Getter | ||
public static class MessageDto { | ||
@JsonProperty("content") | ||
private String content; | ||
|
||
@JsonProperty("embeds") | ||
private List<EmbedDto> embeds; | ||
} | ||
|
||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Getter | ||
public static class EmbedDto { | ||
@JsonProperty("title") | ||
private String title; | ||
|
||
@JsonProperty("description") | ||
private String description; | ||
} | ||
} |
13 changes: 7 additions & 6 deletions
13
src/main/java/corecord/dev/domain/ability/exception/AbilityException.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,17 +1,18 @@ | ||
package corecord.dev.domain.ability.exception; | ||
|
||
import corecord.dev.domain.ability.status.AbilityErrorStatus; | ||
import lombok.AllArgsConstructor; | ||
import corecord.dev.common.base.BaseErrorStatus; | ||
import corecord.dev.common.exception.GeneralException; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class AbilityException extends RuntimeException { | ||
public class AbilityException extends GeneralException { | ||
|
||
private final AbilityErrorStatus abilityErrorStatus; | ||
public AbilityException(BaseErrorStatus errorStatus) { | ||
super(errorStatus); | ||
} | ||
|
||
@Override | ||
public String getMessage() { | ||
return abilityErrorStatus.getMessage(); | ||
return errorStatus.getMessage(); | ||
} | ||
} |
14 changes: 8 additions & 6 deletions
14
src/main/java/corecord/dev/domain/analysis/exception/AnalysisException.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,16 +1,18 @@ | ||
package corecord.dev.domain.analysis.exception; | ||
|
||
import corecord.dev.domain.analysis.status.AnalysisErrorStatus; | ||
import lombok.AllArgsConstructor; | ||
import corecord.dev.common.base.BaseErrorStatus; | ||
import corecord.dev.common.exception.GeneralException; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class AnalysisException extends RuntimeException { | ||
private final AnalysisErrorStatus analysisErrorStatus; | ||
public class AnalysisException extends GeneralException { | ||
|
||
public AnalysisException(BaseErrorStatus errorStatus) { | ||
super(errorStatus); | ||
} | ||
|
||
@Override | ||
public String getMessage() { | ||
return analysisErrorStatus.getMessage(); | ||
return errorStatus.getMessage(); | ||
} | ||
} |
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
14 changes: 8 additions & 6 deletions
14
src/main/java/corecord/dev/domain/auth/exception/TokenException.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,16 +1,18 @@ | ||
package corecord.dev.domain.auth.exception; | ||
|
||
import corecord.dev.domain.auth.status.TokenErrorStatus; | ||
import corecord.dev.common.base.BaseErrorStatus; | ||
import corecord.dev.common.exception.GeneralException; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public class TokenException extends RuntimeException { | ||
private final TokenErrorStatus tokenErrorStatus; | ||
public class TokenException extends GeneralException { | ||
|
||
public TokenException(BaseErrorStatus errorStatus) { | ||
super(errorStatus); | ||
} | ||
|
||
@Override | ||
public String getMessage() { | ||
return tokenErrorStatus.getMessage(); | ||
return errorStatus.getMessage(); | ||
} | ||
} |
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: 8 additions & 4 deletions
12
src/main/java/corecord/dev/domain/chat/exception/ChatException.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,16 +1,20 @@ | ||
package corecord.dev.domain.chat.exception; | ||
|
||
import corecord.dev.common.base.BaseErrorStatus; | ||
import corecord.dev.common.exception.GeneralException; | ||
import corecord.dev.domain.chat.status.ChatErrorStatus; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class ChatException extends RuntimeException { | ||
private final ChatErrorStatus chatErrorStatus; | ||
public class ChatException extends GeneralException { | ||
|
||
public ChatException(BaseErrorStatus errorStatus) { | ||
super(errorStatus); | ||
} | ||
|
||
@Override | ||
public String getMessage() { | ||
return chatErrorStatus.getMessage(); | ||
return errorStatus.getMessage(); | ||
} | ||
} |
Oops, something went wrong.