Skip to content

Commit

Permalink
Merge pull request #202 from RandomJusicool/feature/201-exception-dis…
Browse files Browse the repository at this point in the history
…cord-webhook

🔀 :: 예외 발생시 디스코드 알림
  • Loading branch information
Umjiseung authored Jul 25, 2024
2 parents ab70b96 + 85fcfce commit acc9202
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@
import com.juicycool.backend.global.exception.ErrorCode;
import com.juicycool.backend.global.exception.ErrorResponse;
import com.juicycool.backend.global.exception.GlobalException;
import com.juicycool.backend.global.filter.event.ErrorLoggingEvent;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.http.MediaType;
import org.springframework.web.filter.OncePerRequestFilter;

import java.io.IOException;

@AllArgsConstructor
public class ExceptionFilter extends OncePerRequestFilter {

private final ObjectMapper objectMapper;
private final ApplicationEventPublisher applicationEventPublisher;

public ExceptionFilter(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}

@Override
public void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws IOException, ServletException {
try {
filterChain.doFilter(request, response);
Expand All @@ -41,5 +41,7 @@ private void sendError(HttpServletResponse response, ErrorCode errorCode) throws
response.setStatus(errorCode.getStatus());
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
response.getWriter().write(responseString);

applicationEventPublisher.publishEvent(new ErrorLoggingEvent(errorCode));
}
}
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.juicycool.backend.global.security.handler.JwtAuthenticationEntryPoint;
import com.juicycool.backend.global.security.jwt.JwtProvider;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
Expand All @@ -29,6 +30,7 @@ public class SecurityConfig {
private final JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint;
private final JwtAccessDeniedHandler jwtAccessDeniedHandler;
private final ObjectMapper objectMapper;
private final ApplicationEventPublisher applicationEventPublisher;

@Bean
public PasswordEncoder passwordEncoder() {
Expand Down Expand Up @@ -110,7 +112,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
)

.addFilterBefore(new RequestLogFilter(), UsernamePasswordAuthenticationFilter.class)
.addFilterBefore(new ExceptionFilter(objectMapper), UsernamePasswordAuthenticationFilter.class)
.addFilterBefore(new ExceptionFilter(objectMapper, applicationEventPublisher), UsernamePasswordAuthenticationFilter.class)
.addFilterBefore(new JwtFilter(jwtProvider), UsernamePasswordAuthenticationFilter.class);


Expand Down
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,22 @@
import com.juicycool.backend.domain.auth.event.SignUpLoggingEvent;
import com.juicycool.backend.global.thirdparty.discord.properties.DiscordProperties;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;
import org.springframework.web.client.RestTemplate;


@Component
@RequiredArgsConstructor
@Slf4j
public class DiscordUtil {
public class SignUpEventHandler {

private final DiscordProperties discordProperties;

@EventListener
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
public void signupLoggingHandler(SignUpLoggingEvent signupLoggingEvent) {
JsonObject emailField = new JsonObject();
emailField.add("name", new JsonPrimitive("이메일"));
Expand All @@ -43,6 +42,7 @@ public void signupLoggingHandler(SignUpLoggingEvent signupLoggingEvent) {

JsonObject jsonObject = new JsonObject();
jsonObject.add("content", new JsonPrimitive("## 회원가입 알림"));

JsonArray embeds = new JsonArray();
embeds.add(embed);
jsonObject.add("embeds", embeds);
Expand All @@ -52,7 +52,6 @@ public void signupLoggingHandler(SignUpLoggingEvent signupLoggingEvent) {

RestTemplate restTemplate = new RestTemplate();
HttpEntity<String> entity = new HttpEntity<>(jsonObject.toString(), headers);
log.info(discordProperties.getSignUpURL());
restTemplate.postForObject(discordProperties.getSignUpURL(), entity, String.class);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
@ConfigurationProperties("discord")
public class DiscordProperties {
private final String signUpURL;
private final String errorURL;
}
3 changes: 2 additions & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ jwt:
refreshSecret: ${JWT_REFRESH_SECRET}

discord:
sign-up-url: ${DISCORD_SIGN_UP_URL}
sign-up-url: ${DISCORD_SIGN_UP_URL}
error-url: ${DISCORD_ERROR_URL}

0 comments on commit acc9202

Please sign in to comment.