Skip to content

Commit

Permalink
#499 [feat] sentry exception monitoring 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
sohyundoh committed Aug 20, 2024
1 parent 4efa6d2 commit 7ca9836
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
2 changes: 2 additions & 0 deletions module-common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ jar { enabled = true }
dependencies {
//Aop
implementation 'org.springframework.boot:spring-boot-starter-aop'
//Sentry
implementation 'io.sentry:sentry-spring-boot-starter-jakarta:7.9.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import com.mile.exception.model.NotFoundException;
import com.mile.exception.model.TooManyRequestException;
import com.mile.exception.model.UnauthorizedException;
import jakarta.servlet.http.HttpServletRequest;
import io.sentry.Sentry;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -33,31 +33,37 @@ public class GlobalExceptionHandler {

@ExceptionHandler(HttpMessageNotReadableException.class)
public ResponseEntity<ErrorResponse> handleHttpMessageNotReadableException(final HttpMessageNotReadableException e) {
Sentry.captureException(e);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ErrorResponse.of(ErrorMessage.ENUM_VALUE_BAD_REQUEST));
}

@ExceptionHandler(HandlerMethodValidationException.class)
public ResponseEntity<ErrorResponse> handleHandlerMethodValidationException(final HandlerMethodValidationException e) {
Sentry.captureException(e);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ErrorResponse.of(HttpStatus.BAD_REQUEST.value(), Objects.requireNonNull(e.getAllValidationResults().get(INDEX_ZERO).getResolvableErrors().get(INDEX_ZERO).getDefaultMessage())));
}

@ExceptionHandler(MethodArgumentTypeMismatchException.class)
public ResponseEntity<ErrorResponse> handleMethodArgumentTypeMismatchException(final MethodArgumentTypeMismatchException e) {
Sentry.captureException(e);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ErrorResponse.of(ErrorMessage.REQUEST_URL_WRONG_ERROR));
}

@ExceptionHandler(BadRequestException.class)
public ResponseEntity<ErrorResponse> handleBadRequestException(final BadRequestException e) {
Sentry.captureException(e);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ErrorResponse.of(e.getErrorMessage()));
}

@ExceptionHandler(UnauthorizedException.class)
public ResponseEntity<ErrorResponse> handleUnauthorizedException(final UnauthorizedException e) {
Sentry.captureException(e);
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(ErrorResponse.of(e.getErrorMessage()));
}

@ExceptionHandler(JwtValidationException.class)
public ResponseEntity<ErrorResponse> handleJwtValidationException(final JwtValidationException e) {
Sentry.captureException(e);
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(ErrorResponse.of(e.getErrorMessage()));
}

Expand All @@ -68,11 +74,11 @@ public ResponseEntity<ErrorResponse> handleHttpRequestMethodNotSupportedExceptio

@ExceptionHandler(MethodArgumentNotValidException.class)
protected ResponseEntity<ErrorResponse> handleMethodArgumentNotValidException(final MethodArgumentNotValidException e) {
Sentry.captureException(e);
FieldError fieldError = e.getBindingResult().getFieldError();
if (fieldError == null)
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ErrorResponse.of(ErrorMessage.VALIDATION_REQUEST_MISSING_EXCEPTION));

return switch (fieldError.getCode()) {
return switch (Objects.requireNonNull(fieldError.getCode())) {
case "NotNull", "NotBlank" ->
ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ErrorResponse.of(ErrorMessage.VALIDATION_REQUEST_NULL_OR_BLANK_EXCEPTION));
case "Size" ->
Expand All @@ -84,31 +90,37 @@ protected ResponseEntity<ErrorResponse> handleMethodArgumentNotValidException(fi

@ExceptionHandler(ForbiddenException.class)
public ResponseEntity<ErrorResponse> handleForbiddenException(final ForbiddenException e) {
Sentry.captureException(e);
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(ErrorResponse.of(e.getErrorMessage()));
}

@ExceptionHandler(NotFoundException.class)
public ResponseEntity<ErrorResponse> handleNotFoundException(final NotFoundException e) {
Sentry.captureException(e);
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ErrorResponse.of(e.getErrorMessage()));
}

@ExceptionHandler(ConflictException.class)
public ResponseEntity<ErrorResponse> handleConflictException(final ConflictException e) {
Sentry.captureException(e);
return ResponseEntity.status(HttpStatus.CONFLICT).body(ErrorResponse.of(e.getErrorMessage()));
}

@ExceptionHandler(TooManyRequestException.class)
public ResponseEntity<ErrorResponse> handleTooManyRequestException(final TooManyRequestException e) {
Sentry.captureException(e);
return ResponseEntity.status(HttpStatus.TOO_MANY_REQUESTS).body(ErrorResponse.of(e.getErrorMessage()));
}

@ExceptionHandler(NoHandlerFoundException.class)
public ResponseEntity<ErrorResponse> handleNoHandlerFoundException(final NoHandlerFoundException e) {
Sentry.captureException(e);
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ErrorResponse.of(ErrorMessage.HANDLER_NOT_FOUND));
}

@ExceptionHandler(Exception.class)
protected ResponseEntity<ErrorResponse> handleException(final Exception error, final HttpServletRequest request) {
protected ResponseEntity<ErrorResponse> handleException(final Exception error) {
Sentry.captureException(error);
log.error("================================================NEW===============================================");
log.error(error.getMessage(), error);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ErrorResponse.of(ErrorMessage.INTERNAL_SERVER_ERROR));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public CachedBodyServletInputStream(byte[] cachedBody) {

@Override
public boolean isFinished() {
// 현재 읽을 수 있는 Byte 수가 0이라면 True를 반환
try {
return cachedBodyInputStream.available() == 0;
} catch (IOException e) {
Expand Down

0 comments on commit 7ca9836

Please sign in to comment.