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

Feature/#50 #66

Merged
merged 3 commits into from
May 8, 2024
Merged
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,12 +1,15 @@
package b1nd.dodamcore.common.exception.handler;
package b1nd.dodamapi.common.exception;

import b1nd.dodamcore.common.exception.ErrorResponseEntity;
import b1nd.dodamcore.common.exception.GlobalExceptionCode;
import b1nd.dodamcore.common.exception.custom.CustomException;
import b1nd.dodamcore.common.exception.CustomException;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
Expand All @@ -15,42 +18,52 @@
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

@Slf4j
import java.util.List;

@RestControllerAdvice
@Slf4j
@RequiredArgsConstructor
public class CustomExceptionHandler {

private final ErrorNoticeSender errorNoticeSender;

@ExceptionHandler(CustomException.class)
protected ResponseEntity handleCustomException(CustomException e){
protected ResponseEntity<ErrorResponseEntity> handleCustomException(CustomException e){
log.error("CustomException Status : {}", e.getExceptionCode().getHttpStatus());
log.error("CustomException Message : {}", e.getExceptionCode().getMessage());

return ErrorResponseEntity.responseEntity(e.getExceptionCode());
}

@ExceptionHandler(MethodArgumentNotValidException.class)
protected ResponseEntity handleValidException(MethodArgumentNotValidException e) {
StringBuilder message = new StringBuilder();
e.getBindingResult().getAllErrors().forEach(
error -> message
.append(((FieldError) error).getField()).append(" ")
.append(error.getDefaultMessage()).append(", ")
);
protected ResponseEntity<ErrorResponseEntity> handleValidException(MethodArgumentNotValidException e) {
String message = getValidExceptionMessages(e.getBindingResult().getAllErrors());

log.error("Valid Fail Object : {}", e.getObjectName());
log.error("Valid Fail Message : \"{}\"", message.substring(0, message.length() - 2));
log.error("Valid Fail Message : \"{}\"", message);

return ResponseEntity
.status(e.getStatusCode())
.body(ErrorResponseEntity.builder()
.status(e.getStatusCode().value())
.code(GlobalExceptionCode.PARAMETER_NOT_VALID.name())
.message(message.substring(0, message.length() - 2))
.message(message)
.build()
);
}

private String getValidExceptionMessages(List<ObjectError> errors) {
StringBuilder message = new StringBuilder();
errors.forEach(
error -> message
.append(((FieldError) error).getField()).append(" ")
.append(error.getDefaultMessage()).append(", ")
);
return message.substring(0, message.length() - 2);
}

@ExceptionHandler(MissingServletRequestParameterException.class)
protected ResponseEntity handleMissingServletRequestParameterException(MissingServletRequestParameterException e) {
protected ResponseEntity<ErrorResponseEntity> handleMissingServletRequestParameterException(MissingServletRequestParameterException e) {
return ResponseEntity
.status(400)
.body(ErrorResponseEntity.builder()
Expand All @@ -61,7 +74,7 @@ protected ResponseEntity handleMissingServletRequestParameterException(MissingSe
}

@ExceptionHandler(HttpMessageNotReadableException.class)
protected ResponseEntity handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
protected ResponseEntity<ErrorResponseEntity> handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
return ResponseEntity
.status(400)
.body(ErrorResponseEntity.builder()
Expand All @@ -72,7 +85,7 @@ protected ResponseEntity handleHttpMessageNotReadableException(HttpMessageNotRea
}

@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
protected ResponseEntity handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
protected ResponseEntity<ErrorResponseEntity> handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
return ResponseEntity
.status(400)
.body(ErrorResponseEntity.builder()
Expand All @@ -83,7 +96,7 @@ protected ResponseEntity handleHttpRequestMethodNotSupportedException(HttpReques
}

@ExceptionHandler(HttpMediaTypeNotSupportedException.class)
protected ResponseEntity handleHttpMediaTypeNotSupportedException() {
protected ResponseEntity<ErrorResponseEntity> handleHttpMediaTypeNotSupportedException() {
return ResponseEntity
.status(400)
.body(ErrorResponseEntity.builder()
Expand All @@ -94,7 +107,7 @@ protected ResponseEntity handleHttpMediaTypeNotSupportedException() {
}

@ExceptionHandler(MethodArgumentTypeMismatchException.class)
protected ResponseEntity handleMethodArgumentTypeMismatchException() {
protected ResponseEntity<ErrorResponseEntity> handleMethodArgumentTypeMismatchException() {
return ResponseEntity
.status(400)
.body(ErrorResponseEntity.builder()
Expand All @@ -105,8 +118,8 @@ protected ResponseEntity handleMethodArgumentTypeMismatchException() {
}

@ExceptionHandler(Exception.class)
protected ResponseEntity handleException(Exception e){
log.error(e.toString());
protected ResponseEntity<ErrorResponseEntity> handleException(Exception e, HttpServletRequest request){
errorNoticeSender.send(e, request);

return ResponseEntity
.status(500)
Expand All @@ -116,4 +129,5 @@ protected ResponseEntity handleException(Exception e){
.message(GlobalExceptionCode.INTERNAL_SERVER.getMessage())
.build());
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package b1nd.dodamapi.common.exception;

import b1nd.dodamcore.notice.NoticeClient;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.time.LocalDateTime;

@Component
@RequiredArgsConstructor
public class ErrorNoticeSender {

private final NoticeClient client;

@Async
public void send(Exception e, HttpServletRequest request) {
LocalDateTime now = LocalDateTime.now();
String requestPath = getRequestPath(request);
String title = "🚨 OMG";
String description = "### 🕖 발생 시간\n"
+ now
+ "\n"
+ "### 🔗 요청 URL\n"
+ requestPath
+ "\n"
+ "### 📄 Stack Trace\n"
+ "```\n"
+ getStackTrace(e)
+ "\n```";

client.notice("", title, description);
}

private String getRequestPath(HttpServletRequest request) {
String path = request.getMethod() + " " + request.getRequestURL();

String queryString = request.getQueryString();
if (queryString != null) {
path += "?" + queryString;
}

return path;
}

private String getStackTrace(Exception e) {
StringWriter stringWriter = new StringWriter();
e.printStackTrace(new PrintWriter(stringWriter));

return stringWriter.toString().substring(0, 1000);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import b1nd.dodamcore.point.domain.event.PointCanceledEvent;
import b1nd.dodamcore.point.domain.event.PointIssuedEvent;

final class PointSMSEventHelper {
final class PointMessageMaker {

public static PointIssuedEvent createIssuedEvent(Member member, PointReason reason) {
return new PointIssuedEvent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private void saveScores(List<Student> students, PointReason reason) {

private void publishPointIssuedEvents(List<Student> students, PointReason reason) {
students.forEach(s -> eventPublisher.publishEvent(
PointSMSEventHelper.createIssuedEvent(s.getMember(), reason)
PointMessageMaker.createIssuedEvent(s.getMember(), reason)
));
}

Expand All @@ -84,12 +84,11 @@ public Response cancel(Integer id) {
}

private void publishCanceledEvent(PointReason reason, Member member) {
eventPublisher.publishEvent(PointSMSEventHelper.createCanceledEvent(member, reason));
eventPublisher.publishEvent(PointMessageMaker.createCanceledEvent(member, reason));
}

public ResponseData<List<PointRes>> getMyPoints(PointType type) {
Student student = memberService.getStudentFromSession();

return ResponseData.ok("내 상벌점 조회 성공", getPointsBy(student, type));
}

Expand Down
4 changes: 3 additions & 1 deletion dodam-api/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ spring:
ddl-auto: none
properties:
hibernate:
show_sql: true
show_sql: false
format_sql: true
data:
redis.repositories.enabled: false
Expand Down Expand Up @@ -72,6 +72,8 @@ app:
url: ${CODENARY_URL}
melon:
url: ${MELON_URL}
discord:
url: ${DISCORD_URL}

cloud:
aws:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package b1nd.dodamcore.banner.domain.exception;

import b1nd.dodamcore.common.exception.custom.CustomException;
import b1nd.dodamcore.common.exception.CustomException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package b1nd.dodamcore.bus.domain.exception;

import b1nd.dodamcore.common.exception.custom.CustomException;
import b1nd.dodamcore.common.exception.CustomException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package b1nd.dodamcore.bus.domain.exception;

import b1nd.dodamcore.common.exception.custom.CustomException;
import b1nd.dodamcore.common.exception.CustomException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package b1nd.dodamcore.bus.domain.exception;

import b1nd.dodamcore.common.exception.custom.CustomException;
import b1nd.dodamcore.common.exception.CustomException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package b1nd.dodamcore.bus.domain.exception;

import b1nd.dodamcore.common.exception.custom.CustomException;
import b1nd.dodamcore.common.exception.CustomException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package b1nd.dodamcore.bus.domain.exception;

import b1nd.dodamcore.common.exception.custom.CustomException;
import b1nd.dodamcore.common.exception.CustomException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package b1nd.dodamcore.common.enums;

import b1nd.dodamcore.common.exception.ExceptionCode;
import b1nd.dodamcore.common.exception.custom.CustomException;
import b1nd.dodamcore.common.exception.CustomException;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package b1nd.dodamcore.common.exception.custom;
package b1nd.dodamcore.common.exception;

import b1nd.dodamcore.common.exception.ExceptionCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package b1nd.dodamcore.common.exception.custom;
package b1nd.dodamcore.common.exception;

import b1nd.dodamcore.common.exception.GlobalExceptionCode;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package b1nd.dodamcore.member.domain.exception;

import b1nd.dodamcore.common.exception.custom.CustomException;
import b1nd.dodamcore.common.exception.CustomException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package b1nd.dodamcore.member.domain.exception;

import b1nd.dodamcore.common.exception.custom.CustomException;
import b1nd.dodamcore.common.exception.CustomException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package b1nd.dodamcore.member.domain.exception;

import b1nd.dodamcore.common.exception.custom.CustomException;
import b1nd.dodamcore.common.exception.CustomException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package b1nd.dodamcore.member.domain.exception;

import b1nd.dodamcore.common.exception.custom.CustomException;
import b1nd.dodamcore.common.exception.CustomException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package b1nd.dodamcore.member.domain.exception;

import b1nd.dodamcore.common.exception.custom.CustomException;
import b1nd.dodamcore.common.exception.CustomException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

Expand Down
Loading
Loading