From 1fd1fb443bfa21c33f39f6f5e8b7b8ecfbdab240 Mon Sep 17 00:00:00 2001 From: badahertz52 Date: Thu, 14 Sep 2023 17:31:44 +0900 Subject: [PATCH] Change value returned when fail to get weather data to Error from string and Modify error in toolkit.ts --- src/modules/api.ts | 7 +++--- src/modules/position/toolkit.ts | 2 +- src/modules/weather/saga.ts | 29 ++++++++++++------------ src/modules/weather/thunk.ts | 35 +++++++++++++++-------------- src/modules/weather/toolkit.ts | 39 +++++++++++++++------------------ 5 files changed, 56 insertions(+), 56 deletions(-) diff --git a/src/modules/api.ts b/src/modules/api.ts index 64340b9..9a1359e 100644 --- a/src/modules/api.ts +++ b/src/modules/api.ts @@ -1378,7 +1378,7 @@ export const getWeatherData = async ( sfGrid: SFGridItem, longitude: string, latitude: string -): Promise => { +): Promise => { const userAreaCode = sfGrid.areaCode; const { nX, nY } = changeNType(sfGrid); const stationName: string[] = @@ -1564,7 +1564,7 @@ export const getWeatherData = async ( }; return weather; } else { - const error = { + const errorData = { skyCode: skyCode instanceof Error, sVFcst: sVFcst instanceof Error, uSNcst: uSNcst instanceof Error, @@ -1573,7 +1573,8 @@ export const getWeatherData = async ( tomorrowApGrade: tomorrowApGrade instanceof Error, sunInform: sunInformHasError, }; + const error = new Error(`[Error : weather data]:${errorData}`); console.error(error); - return JSON.stringify(error); + return error; } }; diff --git a/src/modules/position/toolkit.ts b/src/modules/position/toolkit.ts index 2313cec..dd460df 100644 --- a/src/modules/position/toolkit.ts +++ b/src/modules/position/toolkit.ts @@ -25,7 +25,7 @@ export const toolkitPosition = createAsyncThunk( }; return position; } catch (err) { - const error = err as Error; + const error = new Error("Fail to get area data"); return thunkAPI.rejectWithValue(error.message); } } diff --git a/src/modules/weather/saga.ts b/src/modules/weather/saga.ts index 7a9d1e8..b7e7969 100644 --- a/src/modules/weather/saga.ts +++ b/src/modules/weather/saga.ts @@ -1,24 +1,25 @@ -import {call, put, takeEvery} from 'redux-saga/effects'; +import { call, put, takeEvery } from "redux-saga/effects"; import { getWeatherData } from "../api"; import { WeatherState } from "./types"; import { request, success, failure } from "./reducer"; -function* getWeatherSaga(action:ReturnType){ - const {longitude, latitude,sfGrid} =action.payload ; +function* getWeatherSaga(action: ReturnType) { + const { longitude, latitude, sfGrid } = action.payload; try { - const data : string|WeatherState = yield call(()=>(getWeatherData(sfGrid, longitude, latitude))); - if(typeof data === "string"){ - const error =new Error(data); - yield put(failure(error)); - }else{ - yield put(success(data)) - }; + const data: Error | WeatherState = yield call(() => + getWeatherData(sfGrid, longitude, latitude) + ); + if (data instanceof Error) { + yield put(failure(data)); + } else { + yield put(success(data)); + } } catch (error) { const e = new Error(`Can't get weather data ${error}`); yield put(failure(e)); } -}; +} -export function* weatherSaga( ){ - yield takeEvery(request, getWeatherSaga) -}; \ No newline at end of file +export function* weatherSaga() { + yield takeEvery(request, getWeatherSaga); +} diff --git a/src/modules/weather/thunk.ts b/src/modules/weather/thunk.ts index 21e1e65..951d8be 100644 --- a/src/modules/weather/thunk.ts +++ b/src/modules/weather/thunk.ts @@ -4,22 +4,23 @@ import { PositionSuccessData } from "../position/types"; import { WeatherAction, WeatherState } from "./types"; import { request, success, failure } from "./reducer"; -export const getWeatherThunk =(positionSuccessData:PositionSuccessData):ThunkAction=>async(dispatch)=>{ - const {longitude, latitude, sfGrid}=positionSuccessData; - dispatch(request(positionSuccessData)); - try{ - // weatherState 이거나 error message 를 담은 string - const data = await getWeatherData(sfGrid,longitude,latitude); - if(typeof data === "string"){ - const error = new Error (`[Error : weather data]:${data}`); +export const getWeatherThunk = + ( + positionSuccessData: PositionSuccessData + ): ThunkAction => + async (dispatch) => { + const { longitude, latitude, sfGrid } = positionSuccessData; + dispatch(request(positionSuccessData)); + try { + // weatherState 이거나 error message 를 담은 string + const data = await getWeatherData(sfGrid, longitude, latitude); + if (data instanceof Error) { + dispatch(failure(data)); + } else { + dispatch(success(data)); + } + } catch (e) { + const error = new Error("Fail get weather data"); dispatch(failure(error)); - }else{ - dispatch(success(data)); } - }catch(e){ - const error =new Error ("Fail get weather data"); - dispatch(failure(error)); - } - -}; - + }; diff --git a/src/modules/weather/toolkit.ts b/src/modules/weather/toolkit.ts index d3649ab..b84f2ab 100644 --- a/src/modules/weather/toolkit.ts +++ b/src/modules/weather/toolkit.ts @@ -4,29 +4,26 @@ import { PositionSuccessData } from "../position"; import { noneState_weather } from "./reducer"; import { WeatherState } from "./types"; - -export const toolkitWeather = createAsyncThunk ( - 'weather/toolkitWeather', - async ( positionSuccessData:PositionSuccessData,thunkAPI) => { - const {longitude, latitude, sfGrid} = positionSuccessData; +export const toolkitWeather = createAsyncThunk( + "weather/toolkitWeather", + async (positionSuccessData: PositionSuccessData, thunkAPI) => { + const { longitude, latitude, sfGrid } = positionSuccessData; try { - const data = await getWeatherData(sfGrid,longitude,latitude); - const error = new Error (`[Error]:${data}`); - const weatherState : WeatherState = - (typeof data === "string")? - { - ...noneState_weather, - state:"failure", - error:error, - } - :{ - ...data - } - return weatherState - + const data = await getWeatherData(sfGrid, longitude, latitude); + const weatherState: WeatherState = + data instanceof Error + ? { + ...noneState_weather, + state: "failure", + error: data, + } + : { + ...data, + }; + return weatherState; } catch (err) { - const error = err as Error ; + const error = new Error("Fail get weather data"); return thunkAPI.rejectWithValue(error.message); } } -) \ No newline at end of file +);