Skip to content

Commit

Permalink
Merge pull request #42 from KOA-TF/feature/slack-errorcheck
Browse files Browse the repository at this point in the history
Feature/slack errorcheck
  • Loading branch information
Jeongh00 authored Nov 17, 2023
2 parents 07f6865 + d9b8f66 commit c496187
Show file tree
Hide file tree
Showing 11 changed files with 172 additions and 0 deletions.
2 changes: 2 additions & 0 deletions common-module/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ plugins {
}

dependencies {
implementation project(':slack')

implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.springframework.boot:spring-boot-starter-security'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.koa.commonmodule.presentation;

import com.koa.commonmodule.exception.BusinessException;
import com.koa.slack.service.SlackService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@RequiredArgsConstructor
public class CommonRestControllerAdvise {

private final SlackService slackService;

@ExceptionHandler({RuntimeException.class, BusinessException.class, Exception.class})
public void commonExceptionHandler(Exception e) {
slackService.sendingExceptionMessage(e);
}
}

1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ rootProject.name = 'koaBE'
include 'api-module'
include 'common-module'
include 'core-module'
include 'slack'
17 changes: 17 additions & 0 deletions slack/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'

implementation("com.slack.api:bolt:1.18.0")
implementation("com.slack.api:bolt-servlet:1.18.0")
implementation("com.slack.api:bolt-jetty:1.18.0")
}

bootJar {
enabled = false
}
jar {
enabled = true
}

tasks.register("prepareKotlinBuildScriptModel"){}
Binary file added slack/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
7 changes: 7 additions & 0 deletions slack/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
20 changes: 20 additions & 0 deletions slack/src/main/java/com/koa/slack/config/SlackConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.koa.slack.config;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@RequiredArgsConstructor
public class SlackConfig {

@Bean
public SlackConfigProperties logConfigProperties(
@Value("https://hooks.slack.com/services/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") String serverWebhookUrl
) {
SlackConfigProperties slackConfigProperties = new SlackConfigProperties();
slackConfigProperties.setServerWebhookUrl(serverWebhookUrl);
return slackConfigProperties;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.koa.slack.config;

import lombok.Data;

@Data
public class SlackConfigProperties {

private String serverWebhookUrl;
}
82 changes: 82 additions & 0 deletions slack/src/main/java/com/koa/slack/service/SlackService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.koa.slack.service;

import com.koa.slack.config.SlackConfigProperties;
import com.slack.api.Slack;
import com.slack.api.webhook.WebhookResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;

import static com.slack.api.model.Attachments.asAttachments;
import static com.slack.api.model.Attachments.attachment;
import static com.slack.api.model.block.Blocks.asBlocks;
import static com.slack.api.model.block.Blocks.section;
import static com.slack.api.model.block.composition.BlockCompositions.markdownText;
import static com.slack.api.webhook.WebhookPayloads.payload;

@Slf4j
@Service
public class SlackService {

private final SlackConfigProperties properties;

public SlackService(
@Qualifier("logConfigProperties") SlackConfigProperties properties
) {
this.properties = properties;
}

public void sendingExceptionMessage(Exception exception) {
// Slack 메세지 보내기
try {
StackTraceElement[] stackTrace = exception.getStackTrace();
String className = stackTrace[0].getClassName();
String methodName = stackTrace[0].getMethodName();
int lineNumber = stackTrace[0].getLineNumber();

String title = exception.getClass().getName();
String errorMessage = exception.getLocalizedMessage();

StringWriter sw = new StringWriter();
exception.printStackTrace(new PrintWriter(sw));

String stackTraceString = sw.toString();

String formattedStackTrace = stackTraceString.length() > 2500
? stackTraceString.substring(0, 2500)
: stackTraceString;

log.info("Stack Trace: {}", formattedStackTrace);

WebhookResponse response = Slack.getInstance().send(properties.getServerWebhookUrl(), payload(p -> p
.text("Exception Occurred: " + className)
.attachments(asAttachments(
attachment(a -> a
.color("#ff0000")
.blocks(asBlocks(
section(section -> section.text(markdownText("*Exception Occurred:* " + className))),
section(section -> section.text(markdownText("*Method:* " + methodName))),
section(section -> section.text(markdownText("*Line:* " + lineNumber))),
section(section -> section.text(markdownText("*Exception:* " + title))),
section(section -> section.text(markdownText("*Exception Message:*"))),
section(section -> section.text(markdownText("```" + errorMessage + "```"))),
section(section -> section.text(markdownText("*Stack Trace:*"))),
section(section -> section.text(markdownText("```" + formattedStackTrace + "```")))
))
)
))
));

log.info("Slack Response: {}", response);

} catch (IOException e) {
throw new RuntimeException(HttpStatus.INTERNAL_SERVER_ERROR.value() + "Can't send Slack Message.");
}
}

}
1 change: 1 addition & 0 deletions slack/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

13 changes: 13 additions & 0 deletions slack/src/test/java/com/koa/slack/SlackApplicationTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.koa.slack;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SlackApplicationTests {

@Test
void contextLoads() {
}

}

0 comments on commit c496187

Please sign in to comment.