Skip to content
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
Expand Up @@ -2,6 +2,7 @@

import com.jootalkpia.stock_server.stocks.advice.exception.BadRequestException;
import com.jootalkpia.stock_server.stocks.advice.exception.ErrorResponse;
import com.jootalkpia.stock_server.stocks.advice.exception.InvalidMinutePriceFromApiException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
Expand All @@ -11,6 +12,11 @@ public class ControllerAdvice {

@ExceptionHandler(BadRequestException.class)
public ResponseEntity<ErrorResponse> handleBadRequestException(BadRequestException e) {
return ResponseEntity.badRequest().body(new ErrorResponse(e.getMessage()));
return ResponseEntity.badRequest().body(new ErrorResponse(e.getErrorCode(), e.getMessage()));
}

@ExceptionHandler(InvalidMinutePriceFromApiException.class)
public ResponseEntity<ErrorResponse> handleRuntimeException(InvalidMinutePriceFromApiException e) {
return ResponseEntity.internalServerError().body(new ErrorResponse(e.getErrorCode(), e.getMessage()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public class BadRequestException extends BusinessException {

public BadRequestException(String message) {
super(message);
public BadRequestException(String errorCode, String message) {
super(errorCode, message);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package com.jootalkpia.stock_server.stocks.advice.exception;

public class BusinessException extends RuntimeException {
private final String errorCode;

public BusinessException(String message) {
public BusinessException(String errorCode, String message) {
super(message);
this.errorCode = errorCode;
}

public String getErrorCode() {
return errorCode;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package com.jootalkpia.stock_server.stocks.advice.exception;

public record ErrorResponse(String message) {
public record ErrorResponse(
String code,
String message) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.jootalkpia.stock_server.stocks.advice.exception;

public class InvalidMinutePriceFromApiException extends RuntimeException {

private static final String ERROR_CODE = "S50001";
private static final String MESSAGE = "한국 투자 API로부터 응답 받은 데이터가 없습니다. 1분 후에 다시 시도하세요.";

private final String errorCode;

public InvalidMinutePriceFromApiException() {
super(MESSAGE);
this.errorCode = ERROR_CODE;
}

public String getErrorCode() {
return errorCode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

public class InvalidObjectIdFormatException extends BadRequestException {

private static final String ERROR_CODE = "S40002";

private static final String MESSAGE = "ObjectId는 24자리의 16진수 문자열이어야 합니다: ";

public InvalidObjectIdFormatException(String cursorId) {
super(MESSAGE + cursorId);
super(ERROR_CODE, MESSAGE + cursorId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

public class NoSuchMinutePriceException extends BadRequestException {

private static final String ERROR_CODE = "S40001";

private static final String MESSAGE = "조회된 분봉 데이터가 없습니다.";

public NoSuchMinutePriceException() {
super(MESSAGE);
super(ERROR_CODE, MESSAGE);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.jootalkpia.stock_server.stocks.advice.util;

import com.jootalkpia.stock_server.stocks.advice.exception.InvalidMinutePriceFromApiException;
import com.jootalkpia.stock_server.stocks.advice.exception.InvalidObjectIdFormatException;
import com.jootalkpia.stock_server.stocks.advice.exception.NoSuchMinutePriceException;
import com.jootalkpia.stock_server.stocks.dto.MinutePrice;
import com.jootalkpia.stock_server.stocks.dto.response.MinutePriceDetailedResponse;

import java.util.List;
import java.util.NoSuchElementException;

public class StockValidationUtils {
private StockValidationUtils() {
Expand All @@ -31,4 +32,10 @@ private static boolean isValidObjectId(String cursorId) {

return cursorId.matches("[0-9a-fA-F]{24}");
}

public static void validateMinutePriceOutput(MinutePriceDetailedResponse minutePriceDetailedResponse) {
if (minutePriceDetailedResponse.output2().size() < 2) {
throw new InvalidMinutePriceFromApiException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.jootalkpia.stock_server.stocks.dto.MinutePrice;

import static com.jootalkpia.stock_server.stocks.advice.util.StockValidationUtils.validateMinutePriceOutput;

public record MinutePriceSimpleResponse(
String code,
String htsKorIsnm,
Expand All @@ -16,6 +18,8 @@ public record MinutePriceSimpleResponse(
) {

public static MinutePriceSimpleResponse from(MinutePriceDetailedResponse minutePriceDto, String code) {
validateMinutePriceOutput(minutePriceDto);

return new MinutePriceSimpleResponse(
code,
minutePriceDto.output1().htsKorIsnm(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ spring:
uri: ${MONGO_URI}
jackson:
property-naming-strategy: LOWER_CAMEL_CASE
time-zone: Asia/Seoul

logging:
level:
Expand Down
Loading