Skip to content

Commit

Permalink
♻️ refactor: 에러 핸들러 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
seheonnn committed May 9, 2024
1 parent a960c00 commit 5459133
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.waither.weatherservice.controller;

public class WeatherController {
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.waither.weatherservice.dto.AccuweatherTestRequest;
import com.waither.weatherservice.dto.AirTestRequest;
import com.waither.weatherservice.dto.ForeCastTestRequest;
import com.waither.weatherservice.dto.MsgTestRequest;
import com.waither.weatherservice.dto.request.AccuweatherTestRequest;
import com.waither.weatherservice.dto.request.AirTestRequest;
import com.waither.weatherservice.dto.request.ForeCastTestRequest;
import com.waither.weatherservice.dto.request.MsgTestRequest;
import com.waither.weatherservice.service.WeatherService;

import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.waither.weatherservice.dto;
package com.waither.weatherservice.dto.request;

public record AccuweatherTestRequest(
double latitude,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.waither.weatherservice.dto;
package com.waither.weatherservice.dto.request;

public record AirTestRequest(
String searchDate
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.waither.weatherservice.dto;
package com.waither.weatherservice.dto.request;

public record ForeCastTestRequest(
int nx,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.waither.weatherservice.dto;
package com.waither.weatherservice.dto.request;

public record MsgTestRequest(
String location
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.waither.weatherservice.dto.request;

import lombok.Builder;

@Builder
public record WeatherRequest() {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.waither.weatherservice.dto.response;

import lombok.Builder;

@Builder
public record WeatherResponse() {
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.waither.weatherservice.exception.WeatherExceptionHandler;
import com.waither.weatherservice.response.WeatherErrorCode;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -69,14 +71,14 @@ public List<ForeCastOpenApiResponse.Item> callForeCastApi(
.accept(MediaType.APPLICATION_JSON)
.retrieve().bodyToMono(ForeCastOpenApiResponse.class)
.onErrorResume(throwable -> {
throw new OpenApiException(RESPONSE_EXCEPTION_MSG);
throw new WeatherExceptionHandler(WeatherErrorCode.WEATHER_OPENAPI_ERROR);
})
.block().getResponse();

if (response.getHeader().getResultCode().equals("00")) {
return response.getBody().getItems().getItem();
} else {
throw new OpenApiException(response.getHeader().getResultMsg());
throw new WeatherExceptionHandler(WeatherErrorCode.WEATHER_OPENAPI_ERROR);
}
}

Expand Down Expand Up @@ -124,7 +126,7 @@ public List<MsgOpenApiResponse.RowData> callDisasterMsgApi(String location) thro
})
.retrieve().bodyToMono(String.class)
.onErrorResume(throwable -> {
throw new OpenApiException(RESPONSE_EXCEPTION_MSG);
throw new WeatherExceptionHandler(WeatherErrorCode.WEATHER_OPENAPI_ERROR);
})
.block();

Expand All @@ -135,7 +137,8 @@ public List<MsgOpenApiResponse.RowData> callDisasterMsgApi(String location) thro
return response.getDisasterMsg().get(1).getRow();
} else {
String resultMsg = response.getDisasterMsg().get(0).getHead().get(2).getResultData().getResultMsg();
throw new OpenApiException(resultMsg);
log.info("[*] OpenApi Error : {}", resultMsg);
throw new WeatherExceptionHandler(WeatherErrorCode.WEATHER_OPENAPI_ERROR);
}
}

Expand All @@ -161,8 +164,11 @@ public List<AirKoreaOpenApiResponse.Items> callAirKorea(String searchDate) throw
AirKoreaOpenApiResponse.Response response = webClient.get()
.uri(uri)
.accept(MediaType.APPLICATION_JSON)
.retrieve().bodyToMono(AirKoreaOpenApiResponse.class)
.blockOptional().orElseThrow(() -> new OpenApiException(RESPONSE_EXCEPTION_MSG)).getResponse();
.retrieve()
.bodyToMono(AirKoreaOpenApiResponse.class)
.blockOptional()
.orElseThrow(() -> new WeatherExceptionHandler(WeatherErrorCode.WEATHER_OPENAPI_ERROR))
.getResponse();

if (response.getHeader().getResultCode().equals("00")) {

Expand All @@ -178,7 +184,8 @@ public List<AirKoreaOpenApiResponse.Items> callAirKorea(String searchDate) throw

return items;
} else {
throw new OpenApiException(response.getHeader().getResultMsg());
log.info("[*] OpenApi Error : {}", response.getHeader().getResultMsg());
throw new WeatherExceptionHandler(WeatherErrorCode.WEATHER_OPENAPI_ERROR);
}
}

Expand Down Expand Up @@ -215,7 +222,7 @@ public String callAccuweatherLocationApi(double latitude, double longitude) thro
.accept(MediaType.APPLICATION_JSON)
.retrieve().bodyToMono(String.class)
.onErrorResume(throwable -> {
throw new OpenApiException(RESPONSE_EXCEPTION_MSG);
throw new WeatherExceptionHandler(WeatherErrorCode.WEATHER_OPENAPI_ERROR);
})
.block();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
@AllArgsConstructor
public enum WeatherErrorCode implements BaseErrorCode {

WEATHER_ERROR_EXAMPLE(HttpStatus.BAD_REQUEST, "WEAT4000", "날씨 에러입니다.");
WEATHER_EXAMPLE_ERROR(HttpStatus.BAD_REQUEST, "WEAT4000", "날씨 에러입니다."),
WEATHER_OPENAPI_ERROR(HttpStatus.BAD_REQUEST, "WEAT4001", "OpenApi 관련 오류입니다.");

private final HttpStatus httpStatus;
private final String code;
Expand Down

0 comments on commit 5459133

Please sign in to comment.