Skip to content

Commit

Permalink
Change value returned when fail to get weather data to Error from str…
Browse files Browse the repository at this point in the history
…ing and Modify error in toolkit.ts
  • Loading branch information
BadaHertz52 committed Sep 14, 2023
1 parent f33364d commit 1fd1fb4
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 56 deletions.
7 changes: 4 additions & 3 deletions src/modules/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1378,7 +1378,7 @@ export const getWeatherData = async (
sfGrid: SFGridItem,
longitude: string,
latitude: string
): Promise<string | WeatherState> => {
): Promise<Error | WeatherState> => {
const userAreaCode = sfGrid.areaCode;
const { nX, nY } = changeNType(sfGrid);
const stationName: string[] =
Expand Down Expand Up @@ -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,
Expand All @@ -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;
}
};
2 changes: 1 addition & 1 deletion src/modules/position/toolkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
29 changes: 15 additions & 14 deletions src/modules/weather/saga.ts
Original file line number Diff line number Diff line change
@@ -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<typeof request>){
const {longitude, latitude,sfGrid} =action.payload ;
function* getWeatherSaga(action: ReturnType<typeof request>) {
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)
};
export function* weatherSaga() {
yield takeEvery(request, getWeatherSaga);
}
35 changes: 18 additions & 17 deletions src/modules/weather/thunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void,WeatherState, unknown,WeatherAction>=>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<void, WeatherState, unknown, WeatherAction> =>
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));
}

};

};
39 changes: 18 additions & 21 deletions src/modules/weather/toolkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
)
);

0 comments on commit 1fd1fb4

Please sign in to comment.