Skip to content

Commit

Permalink
feat: 토스페이먼츠 예외 처리 (#204)
Browse files Browse the repository at this point in the history
* feat: TossPaymentException 생성 및 적용

* fix: 불필요한 로직 제거

* docs: 결제->예매로 수정

* refactor: TossPaymentException status code 입력받도록 수정

---------

Co-authored-by: Hanna Lee <[email protected]>
  • Loading branch information
byulcode and annahxxl authored Jan 10, 2024
1 parent be031be commit c6979c0
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
- api-event
공연 도메인
- api-booking
결제 도메인
예매 도메인
#### batch
스프링 배치 모듈
#### core
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object bo
protected ResponseEntity<Object> handleHttpMessageNotReadable(HttpMessageNotReadableException ex,
HttpHeaders headers, HttpStatusCode status, WebRequest request) {
ErrorResponse response = BookingErrorCode.INVALID_INPUT_VALUE.getErrorResponse();
log.warn("HttpMessageNotReadableException Occurred : {}", response.getErrorMessage());
return ResponseEntity.badRequest().body(response);
}

Expand All @@ -64,6 +65,13 @@ protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotV
return ResponseEntity.status(status).body(response);
}

@ExceptionHandler(TossPaymentException.class)
protected ResponseEntity<ErrorResponse> handleTossPaymentException(TossPaymentException ex) {
ErrorResponse response = new ErrorResponse(ex.getCode(), ex.getMessage());
log.warn("TossPaymentException Occurred : {}", response.getErrorMessage());
return ResponseEntity.status(ex.getStatus()).body(response);
}

@ExceptionHandler(BookingException.class)
protected ResponseEntity<ErrorResponse> handleBookingException(BookingException ex) {
ErrorResponse response = ex.getErrorCode().getErrorResponse();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.pgms.apibooking.common.exception;

import org.springframework.http.HttpStatus;

import com.pgms.coredomain.response.ErrorResponse;

import lombok.Getter;

@Getter
public class TossPaymentException extends RuntimeException {
private final String code;
private final HttpStatus status;

public TossPaymentException(ErrorResponse errorResponse, HttpStatus status) {
super(errorResponse.getErrorMessage());
this.code = errorResponse.getErrorCode();
this.status = status;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,25 @@

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.pgms.apibooking.common.exception.BookingException;
import com.pgms.apibooking.common.exception.TossPaymentException;
import com.pgms.apibooking.config.TossPaymentConfig;
import com.pgms.apibooking.domain.payment.dto.request.PaymentCancelRequest;
import com.pgms.apibooking.domain.payment.dto.request.PaymentConfirmRequest;
import com.pgms.apibooking.domain.payment.dto.response.PaymentCancelResponse;
import com.pgms.apibooking.domain.payment.dto.response.PaymentSuccessResponse;
import com.pgms.coredomain.domain.common.BookingErrorCode;
import com.pgms.apibooking.common.exception.BookingException;
import com.pgms.coredomain.response.ErrorResponse;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -41,7 +47,9 @@ public PaymentSuccessResponse requestTossPaymentConfirmation(PaymentConfirmReque
TossPaymentConfig.TOSS_CONFIRM_URL, new HttpEntity<>(request, headers), PaymentSuccessResponse.class);
} catch (HttpClientErrorException e) {
log.warn("HttpClientErrorException: {}", e.getMessage());
throw new BookingException(BookingErrorCode.TOSS_PAYMENTS_ERROR);
ErrorResponse errorResponse = handleHttpClientErrorException(e.getResponseBodyAsString());
HttpStatus status = (HttpStatus)e.getStatusCode();
throw new TossPaymentException(errorResponse, status);
} catch (Exception e) {
log.error("Exception: {}", e.getMessage(), e);
throw new BookingException(BookingErrorCode.INTERNAL_SERVER_ERROR);
Expand All @@ -55,6 +63,10 @@ public PaymentCancelResponse requestTossPaymentCancellation(String paymentKey, P
try {
return restTemplate.postForObject(
uri, new HttpEntity<>(request, headers), PaymentCancelResponse.class);
} catch (HttpClientErrorException e) {
ErrorResponse errorResponse = handleHttpClientErrorException(e.getResponseBodyAsString());
HttpStatus status = (HttpStatus)e.getStatusCode();
throw new TossPaymentException(errorResponse, status);
} catch (Exception e) {
log.error("Exception: {}", e.getMessage(), e);
throw new BookingException(BookingErrorCode.INTERNAL_SERVER_ERROR);
Expand All @@ -71,4 +83,19 @@ private HttpHeaders buildTossApiHeaders() {
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
return headers;
}

private ErrorResponse handleHttpClientErrorException(String body) {
if (body == null | body.isBlank()) {
throw new BookingException(BookingErrorCode.INTERNAL_SERVER_ERROR);
}
try {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(body);
String code = jsonNode.get("code").asText();
String message = jsonNode.get("message").asText();
return new ErrorResponse(code, message);
} catch (JsonProcessingException e) {
throw new BookingException(BookingErrorCode.INTERNAL_SERVER_ERROR);
}
}
}

0 comments on commit c6979c0

Please sign in to comment.