Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hotfix: nginx 조작을 위한 슬랙 알림 api 부활 #507

Open
wants to merge 1 commit into
base: BE/release
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
package com.yigongil.backend.ui.exceptionhandler;

import static com.slack.api.webhook.WebhookPayloads.payload;

import com.slack.api.Slack;
import com.slack.api.model.Attachment;
import com.slack.api.model.Field;
import com.yigongil.backend.exception.HttpException;
import io.jsonwebtoken.ExpiredJwtException;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
Expand All @@ -14,37 +24,81 @@
@RestControllerAdvice
public class ApiExceptionHandler {

@Value("${slack.webhook-url}")
private String webhookUrl;
private final InternalServerErrorMessageConverter messageConverter;
private final Slack slackClient = Slack.getInstance();

public ApiExceptionHandler(InternalServerErrorMessageConverter messageConverter) {
this.messageConverter = messageConverter;
}

@ExceptionHandler
public ResponseEntity<String> handleHttpException(HttpException e, HttpServletRequest request) {
sendSlackAlertErrorLog(e, request);
log.error("예외 발생: ", e);
return ResponseEntity.status(e.getHttpStatus())
.body(e.getMessageWithInput());
}

@ExceptionHandler
public ResponseEntity<String> handleExpiredJwt(ExpiredJwtException e, HttpServletRequest request) {
sendSlackAlertErrorLog(e, request);
log.info("토큰 만료: ", e);
return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
.body("토큰이 만료되었습니다");
}

@ExceptionHandler
public ResponseEntity<String> handleMethodArgumentNotValid(MethodArgumentNotValidException e, HttpServletRequest request) {
sendSlackAlertErrorLog(e, request);
log.error(e.getMessage());
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(e.getMessage());
}

@ExceptionHandler
public ResponseEntity<String> handleException(Exception e, HttpServletRequest request) {
sendSlackAlertErrorLog(e, request);
log.error("예상치 못한 예외 발생: ", e);
return ResponseEntity.internalServerError()
.body(messageConverter.convert(e));
}


private void sendSlackAlertErrorLog(Exception e, HttpServletRequest request) {
try {
slackClient.send(webhookUrl, payload(p -> p
.text("서버 에러 발생")
.attachments(
List.of(generateSlackAttachment(e, request))
)
));
} catch (IOException error) {
log.info("Slack 통신 예외 발생");
}
}

private Attachment generateSlackAttachment(Exception e, HttpServletRequest request) {
String requestTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS").format(LocalDateTime.now());
String xffHeader = request.getHeader("X-FORWARDED-FOR");
return Attachment.builder()
.color("ff0000")
.title(requestTime + " 발생 에러 로그")
.fields(List.of(
generateSlackField("Request IP", xffHeader == null ? request.getRemoteAddr() : xffHeader),
generateSlackField("Request URL", request.getRequestURL() + " " + request.getMethod()),
generateSlackField("Error Message", e.getMessage())
)
)
.build();
}

private Field generateSlackField(String title, String value) {
return Field.builder()
.title(title)
.value(value)
.valueShortEnough(false)
.build();
}
}
3 changes: 3 additions & 0 deletions backend/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,6 @@ jwt:

swagger:
server-url: "https://localhost:8080/v1"

slack:
webhook-url: http:locallocalclocal
3 changes: 3 additions & 0 deletions backend/src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@ oauth2:
jwt:
expiration: 5
key: qkljwnqdjnqjkdnqdwkdkm31i4j1dasksmdasdsadsasdafef113

slack:
webhook-url: http://ignore