Skip to content

Commit

Permalink
Feat/#141 error logging to slack (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
yummygyudon authored Nov 16, 2023
2 parents fe4db68 + 4b6c460 commit 958d633
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 3 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ dependencies {

// test
testImplementation 'org.springframework.boot:spring-boot-starter-test'

// slack
implementation 'com.slack.api:slack-api-client:1.30.0'
}

tasks.named('test') {
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/org/sopt/app/application/slack/SlackService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.sopt.app.application.slack;

import com.slack.api.Slack;
import com.slack.api.model.Attachment;
import com.slack.api.webhook.Payload;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class SlackService {

@Value("${webhook.slack.url}")
private String SLACK_WEBHOOK_URL;

private final Slack slackClient = Slack.getInstance();

public void sendSlackMessage(String title, String message) {
try{
slackClient.send(SLACK_WEBHOOK_URL, Payload.builder()
.text(title)
.attachments(List.of(
Attachment.builder()
.color("#FF0000")
.text(message)
.build()
))
.build());
} catch (Exception e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,57 @@
package org.sopt.app.common.response;

import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.val;
import org.jetbrains.annotations.NotNull;
import org.sopt.app.application.slack.SlackService;
import org.sopt.app.common.exception.BaseException;
import org.sopt.app.domain.entity.User;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
@RequiredArgsConstructor
public class CommonControllerAdvice {

private final SlackService slackService;

@ExceptionHandler(value = BaseException.class)
public ResponseEntity onKnownException(BaseException baseException) {
return new ResponseEntity<>(CommonResponse.onFailure(baseException.getStatusCode(),
baseException.getResponseMessage()), null, baseException.getStatusCode());
public ResponseEntity onKnownException(HttpServletRequest req, BaseException baseException) {
final Long userId = getUserId();
val requestUri = req.getRequestURI();
val baseExceptionMessage = baseException.getResponseMessage();
val baseExceptionStatusCode = baseException.getStatusCode();
final String message = getMessage(
baseException, userId, requestUri, baseExceptionMessage, baseExceptionStatusCode);

slackService.sendSlackMessage("Error", message);

return new ResponseEntity<>(CommonResponse.onFailure(baseExceptionStatusCode,
baseExceptionMessage), null, baseExceptionStatusCode);
}

@NotNull
private String getMessage(
BaseException baseException, Long userId, String requestUri, String baseExceptionMessage,
HttpStatus baseExceptionStatusCode
) {
return "유저 아이디: " + userId + "\n" +
"요청 URI: " + requestUri + "\n" +
"오류 메시지: " + baseExceptionMessage + "\n" +
"오류 코드: " + baseExceptionStatusCode + "\n" +
"StackTrace" + Arrays.toString(baseException.getStackTrace());
}

private Long getUserId() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
val user = (User) authentication.getPrincipal();
return user.getId();
}

}

0 comments on commit 958d633

Please sign in to comment.