-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: 기본 상점 상품 쿼리 수정 * chore: slack api client 의존성 추가 * feat: 예외 발생 시 슬랙 연동 구현 * chore: slack webhook url config 추가 * fix: build 오류 해결
- Loading branch information
Showing
13 changed files
with
176 additions
and
15 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
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
74 changes: 74 additions & 0 deletions
74
src/main/java/com/moabam/api/infrastructure/slack/SlackMessageFactory.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,74 @@ | ||
package com.moabam.api.infrastructure.slack; | ||
|
||
import static java.util.stream.Collectors.*; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.time.LocalDateTime; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
import net.gpedro.integrations.slack.SlackAttachment; | ||
import net.gpedro.integrations.slack.SlackField; | ||
import net.gpedro.integrations.slack.SlackMessage; | ||
|
||
import com.moabam.global.common.util.DateUtils; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
|
||
@Component | ||
public class SlackMessageFactory { | ||
|
||
private static final String ERROR_TITLE = "에러가 발생했습니다 🚨"; | ||
|
||
public SlackMessage generateErrorMessage(HttpServletRequest request, Exception exception) throws IOException { | ||
return new SlackMessage() | ||
.setAttachments(generateAttachments(request, exception)) | ||
.setText(ERROR_TITLE); | ||
} | ||
|
||
private List<SlackAttachment> generateAttachments(HttpServletRequest request, Exception exception) throws | ||
IOException { | ||
return List.of(new SlackAttachment() | ||
.setFallback("Error") | ||
.setColor("danger") | ||
.setTitleLink(request.getContextPath()) | ||
.setText(formatException(exception)) | ||
.setColor("danger") | ||
.setFields(generateFields(request))); | ||
} | ||
|
||
private String formatException(Exception exception) { | ||
return String.format("📍 Exception Class%n%s%n📍 Exception Message%n%s%n%s", | ||
exception.getClass().getName(), | ||
exception.getMessage(), | ||
Arrays.toString(exception.getStackTrace())); | ||
} | ||
|
||
private List<SlackField> generateFields(HttpServletRequest request) throws IOException { | ||
return List.of( | ||
new SlackField().setTitle("✅ Request Method").setValue(request.getMethod()), | ||
new SlackField().setTitle("✅ Request URL").setValue(request.getRequestURL().toString()), | ||
new SlackField().setTitle("✅ Request Time").setValue(DateUtils.format(LocalDateTime.now())), | ||
new SlackField().setTitle("✅ Request IP").setValue(request.getRemoteAddr()), | ||
new SlackField().setTitle("✅ Request Headers").setValue(request.toString()), | ||
new SlackField().setTitle("✅ Request Body").setValue(getRequestBody(request)) | ||
); | ||
} | ||
|
||
private String getRequestBody(HttpServletRequest request) throws IOException { | ||
String body; | ||
|
||
try ( | ||
InputStream inputStream = request.getInputStream(); | ||
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)) | ||
) { | ||
body = bufferedReader.lines().collect(joining(System.lineSeparator())); | ||
} | ||
return body; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/com/moabam/api/infrastructure/slack/SlackService.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,26 @@ | ||
package com.moabam.api.infrastructure.slack; | ||
|
||
import java.io.IOException; | ||
|
||
import org.springframework.core.task.TaskExecutor; | ||
import org.springframework.stereotype.Service; | ||
|
||
import net.gpedro.integrations.slack.SlackApi; | ||
import net.gpedro.integrations.slack.SlackMessage; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class SlackService { | ||
|
||
private final SlackApi slackApi; | ||
private final SlackMessageFactory slackMessageFactory; | ||
private final TaskExecutor taskExecutor; | ||
|
||
public void send(HttpServletRequest request, Exception exception) throws IOException { | ||
SlackMessage slackMessage = slackMessageFactory.generateErrorMessage(request, exception); | ||
taskExecutor.execute(() -> slackApi.call(slackMessage)); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.moabam.global.config; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import net.gpedro.integrations.slack.SlackApi; | ||
|
||
@Configuration | ||
public class SlackConfig { | ||
|
||
@Value("${webhook.slack.url}") | ||
private String webhookUrl; | ||
|
||
@Bean | ||
public SlackApi slackApi() { | ||
return new SlackApi(webhookUrl); | ||
} | ||
} |
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
24 changes: 24 additions & 0 deletions
24
src/main/java/com/moabam/global/error/handler/SlackExceptionHandler.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,24 @@ | ||
package com.moabam.global.error.handler; | ||
|
||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
import com.moabam.api.infrastructure.slack.SlackService; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@RestControllerAdvice | ||
@Profile({"dev", "prod"}) | ||
@RequiredArgsConstructor | ||
public class SlackExceptionHandler { | ||
|
||
private final SlackService slackService; | ||
|
||
@ExceptionHandler(Exception.class) | ||
void handleException(HttpServletRequest request, Exception exception) throws Exception { | ||
slackService.send(request, exception); | ||
throw exception; | ||
} | ||
} |
Submodule config
updated
from c9c58d to 296e5a
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