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

fix: 리다이렉트 롤백 #64

Merged
merged 3 commits into from
Apr 2, 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
129 changes: 68 additions & 61 deletions api/src/main/java/com/mm/api/exception/GlobalExceptionHandler.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.mm.api.exception;

import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.MalformedJwtException;
import lombok.extern.slf4j.Slf4j;
import static com.mm.api.exception.ErrorCode.*;

import java.security.SignatureException;
import java.util.List;
import java.util.Objects;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.AccessDeniedException;
Expand All @@ -12,66 +15,70 @@
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.security.SignatureException;
import java.util.List;
import java.util.Objects;

import static com.mm.api.exception.ErrorCode.*;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.MalformedJwtException;
import io.sentry.Sentry;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(AccessDeniedException.class)
public ResponseEntity<ErrorResponse> handleAccessDeniedException() {
ErrorResponse errorResponse = new ErrorResponse(ACCESS_DENIED.getErrorCode(), ACCESS_DENIED.getMessage());
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(errorResponse);
}

@ExceptionHandler(SignatureException.class)
public ResponseEntity<ErrorResponse> handleSignatureException() {
ErrorResponse errorResponse = new ErrorResponse(ACCESS_TOKEN_MALFORMED.getErrorCode(), ACCESS_TOKEN_MALFORMED.getMessage());
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(errorResponse);
}

@ExceptionHandler(MalformedJwtException.class)
public ResponseEntity<ErrorResponse> handleMalformedJwtException() {
ErrorResponse errorResponse = new ErrorResponse(ACCESS_TOKEN_MALFORMED.getErrorCode(), ACCESS_TOKEN_MALFORMED.getMessage());
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(errorResponse);
}

@ExceptionHandler(ExpiredJwtException.class)
public ResponseEntity<ErrorResponse> handleExpiredJwtException() {
ErrorResponse errorResponse = new ErrorResponse(ACCESS_TOKEN_EXPIRED.getErrorCode(), ACCESS_TOKEN_EXPIRED.getMessage());
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(errorResponse);
}

@ExceptionHandler(CustomException.class)
public ResponseEntity<ErrorResponse> handleCustomException(CustomException e) {
log.info(">>>>> CustomException occurred!! {}", e);

ErrorResponse errorResponse = new ErrorResponse(e.getCode().getErrorCode(), e.getMessage());
return ResponseEntity.status(e.getCode().getStatus()).body(errorResponse);
}

@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ErrorResponse> handleMethodArgumentException(MethodArgumentNotValidException e) {
log.info(">>>>> MethodArgumentNotValidException occurred!! {}", e);

BindingResult bindingResult = e.getBindingResult();
String errorMessage = Objects.requireNonNull(bindingResult.getFieldError())
.getDefaultMessage();

List<FieldError> fieldErrors = bindingResult.getFieldErrors();
ErrorResponse errorResponse = new ErrorResponse(ErrorCode.VALIDATION_FAILED.getErrorCode(), errorMessage);
fieldErrors.forEach(error -> errorResponse.addValidation(error.getField(), error.getDefaultMessage()));
return ResponseEntity.status(e.getStatusCode()).body(errorResponse);
}

@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorResponse> handleException(Exception e) {
log.info(">>>>> Internal Server error occurred!! {}", e);

ErrorResponse errorResponse = new ErrorResponse("500/0001", e.getMessage());
return ResponseEntity.internalServerError().body(errorResponse);
}
@ExceptionHandler(AccessDeniedException.class)
public ResponseEntity<ErrorResponse> handleAccessDeniedException() {
ErrorResponse errorResponse = new ErrorResponse(ACCESS_DENIED.getErrorCode(), ACCESS_DENIED.getMessage());
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(errorResponse);
}

@ExceptionHandler(SignatureException.class)
public ResponseEntity<ErrorResponse> handleSignatureException() {
ErrorResponse errorResponse = new ErrorResponse(ACCESS_TOKEN_MALFORMED.getErrorCode(),
ACCESS_TOKEN_MALFORMED.getMessage());
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(errorResponse);
}

@ExceptionHandler(MalformedJwtException.class)
public ResponseEntity<ErrorResponse> handleMalformedJwtException() {
ErrorResponse errorResponse = new ErrorResponse(ACCESS_TOKEN_MALFORMED.getErrorCode(),
ACCESS_TOKEN_MALFORMED.getMessage());
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(errorResponse);
}

@ExceptionHandler(ExpiredJwtException.class)
public ResponseEntity<ErrorResponse> handleExpiredJwtException() {
ErrorResponse errorResponse = new ErrorResponse(ACCESS_TOKEN_EXPIRED.getErrorCode(),
ACCESS_TOKEN_EXPIRED.getMessage());
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(errorResponse);
}

@ExceptionHandler(CustomException.class)
public ResponseEntity<ErrorResponse> handleCustomException(CustomException e) {
log.info(">>>>> CustomException occurred!! {}", e);

ErrorResponse errorResponse = new ErrorResponse(e.getCode().getErrorCode(), e.getMessage());
return ResponseEntity.status(e.getCode().getStatus()).body(errorResponse);
}

@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ErrorResponse> handleMethodArgumentException(MethodArgumentNotValidException e) {
log.info(">>>>> MethodArgumentNotValidException occurred!! {}", e);

BindingResult bindingResult = e.getBindingResult();
String errorMessage = Objects.requireNonNull(bindingResult.getFieldError())
.getDefaultMessage();

List<FieldError> fieldErrors = bindingResult.getFieldErrors();
ErrorResponse errorResponse = new ErrorResponse(ErrorCode.VALIDATION_FAILED.getErrorCode(), errorMessage);
fieldErrors.forEach(error -> errorResponse.addValidation(error.getField(), error.getDefaultMessage()));
return ResponseEntity.status(e.getStatusCode()).body(errorResponse);
}

@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorResponse> handleException(Exception e) {
log.info(">>>>> Internal Server error occurred!! {}", e);

Sentry.captureException(e);

ErrorResponse errorResponse = new ErrorResponse("500/0001", e.getMessage());
return ResponseEntity.internalServerError().body(errorResponse);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public Item getZigZagItemByCrawler(String redirectUrl) {
Item item = Item.builder()
.price(response.finalPrice())
.title(response.name())
.redirectUrl(response.pageUrl())
.redirectUrl(redirectUrl)
.categoryType(categoryType)
.refund(getRefundPrice(response.finalPrice(), categoryType.getRefundPercent()))
.thumbnailUrl(response.thumbnailUrl())
Expand Down
Loading