-
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.
Showing
3 changed files
with
81 additions
and
3 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
35 changes: 35 additions & 0 deletions
35
src/main/java/org/sopt/app/application/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,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(); | ||
} | ||
} | ||
} |
46 changes: 43 additions & 3 deletions
46
src/main/java/org/sopt/app/common/response/CommonControllerAdvice.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 |
---|---|---|
@@ -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(); | ||
} | ||
|
||
} |