Skip to content

Commit

Permalink
Merge pull request #161 from Gongjakso/feat/discord
Browse files Browse the repository at this point in the history
feat: 디스코드 웹훅 연동
  • Loading branch information
dl-00-e8 authored Jun 4, 2024
2 parents 37055fa + 11a954b commit 2dbf397
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
package com.gongjakso.server.global.exception;

import com.gongjakso.server.global.util.discord.DiscordClient;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.Arrays;

@Slf4j
@RestControllerAdvice
@RequiredArgsConstructor
public class GlobalExceptionHandler {

private final DiscordClient discordClient;

@ExceptionHandler(ApplicationException.class)
protected ResponseEntity<ErrorResponse> handleApplicationException(ApplicationException e){
log.error(e + " " + e.getErrorCode().toString());
log.error("{} {}", e, e.getErrorCode().toString());
discordClient.sendErrorMessage(e.getErrorCode().getCode(), e.getErrorCode().getMessage(), Arrays.toString(e.getStackTrace()));
return ResponseEntity.status(e.getErrorCode().getHttpStatus())
.body(new ErrorResponse(e.getErrorCode()));
}

@ExceptionHandler(RuntimeException.class)
protected ResponseEntity<ErrorResponse> handleRuntimeException(RuntimeException e) {
log.error(e.getMessage());
discordClient.sendErrorMessage(ErrorCode.INTERNAL_SERVER_EXCEPTION.getCode(), e.getMessage(), Arrays.toString(e.getStackTrace()));
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(new ErrorResponse(e.getMessage()));
}
Expand Down
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();
}
}

0 comments on commit 2dbf397

Please sign in to comment.