-
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.
Merge pull request #42 from KOA-TF/feature/slack-errorcheck
Feature/slack errorcheck
- Loading branch information
Showing
11 changed files
with
172 additions
and
0 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
20 changes: 20 additions & 0 deletions
20
...on-module/src/main/java/com/koa/commonmodule/presentation/CommonRestControllerAdvise.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,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); | ||
} | ||
} | ||
|
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 |
---|---|---|
|
@@ -2,3 +2,4 @@ rootProject.name = 'koaBE' | |
include 'api-module' | ||
include 'common-module' | ||
include 'core-module' | ||
include 'slack' |
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,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 not shown.
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,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 |
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,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; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
slack/src/main/java/com/koa/slack/config/SlackConfigProperties.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,9 @@ | ||
package com.koa.slack.config; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class SlackConfigProperties { | ||
|
||
private String serverWebhookUrl; | ||
} |
82 changes: 82 additions & 0 deletions
82
slack/src/main/java/com/koa/slack/service/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,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."); | ||
} | ||
} | ||
|
||
} |
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 @@ | ||
|
13 changes: 13 additions & 0 deletions
13
slack/src/test/java/com/koa/slack/SlackApplicationTests.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,13 @@ | ||
package com.koa.slack; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
@SpringBootTest | ||
class SlackApplicationTests { | ||
|
||
@Test | ||
void contextLoads() { | ||
} | ||
|
||
} |