Skip to content

Commit

Permalink
6.6.3 사가 함수의 예외 처리
Browse files Browse the repository at this point in the history
  • Loading branch information
b-chae committed Oct 24, 2021
1 parent c4a32de commit 60a160d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
9 changes: 7 additions & 2 deletions src/common/api.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
export function callApiLike() {
return new Promise((resolve, reject) => {
console.log('1000')
setTimeout(resolve, 1000)
setTimeout(() => {
if (Math.random() * 10 < 5) {
resolve()
} else {
reject("callApiLike 실패")
}
}, 1000)
})
}
2 changes: 2 additions & 0 deletions src/timeline/container/TimelineMain.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default function TimelineMain() {
const dispatch = useDispatch()
const timelines = useSelector(state => state.timeline.timelines)
const isLoading = useSelector(state => state.timeline.isLoading)
const error = useSelector(state => state.timeline.error)

function onAdd() {
const timeline = getNextTimeline()
Expand All @@ -27,6 +28,7 @@ export default function TimelineMain() {
<button onClick={onAdd}>타임라인 추가</button>
<TimelineList timelines={timelines} onLike={onLike}/>
{isLoading && <p>전송 중 ...</p>}
{!!error && <p>에러 발생 ... {error}</p>}
</div>
)
}
7 changes: 5 additions & 2 deletions src/timeline/state/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export const types = {
INCREASE_NEXT_PAGE: 'timeline/INCREASE_NEXT_PAGE',
REQUEST_LIKE: 'timeline/REQUEST_LIKE',
ADD_LIKE: 'timeline/ADD_LIKE',
SET_LOADING: 'timeline/SET_LOADING'
SET_LOADING: 'timeline/SET_LOADING',
SET_ERROR: 'timeline/SET_ERROR'
}

export const actions = {
Expand All @@ -22,9 +23,10 @@ export const actions = {
type: types.SET_LOADING,
isLoading
}),
setError: error => ({ type: types.SET_ERROR, error })
}

const INITIAL_STSTE = { nextPage: 0, isLoading: false }
const INITIAL_STSTE = { nextPage: 0, isLoading: false, error: "" }
const reducer = createReducer(INITIAL_STSTE, {
[types.INCREASE_NEXT_PAGE]: (state, action) => {
state.nextPage += 1
Expand All @@ -40,6 +42,7 @@ const reducer = createReducer(INITIAL_STSTE, {
[types.SET_LOADING]: (state, action) => {
state.isLoading = action.isLoading
},
[types.SET_ERROR]: (state, action) => (state.error = action.error)
})

const reducers = [reducer, timelinesReducer]
Expand Down
8 changes: 7 additions & 1 deletion src/timeline/state/saga.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ export function* fetchData(action) {
const { timeline } = yield take(types.REQUEST_LIKE)
yield put(actions.setLoading(true))
yield put(actions.addLike(timeline.id, 1))
yield call(callApiLike)
yield put(actions.setError(''))
try {
yield call(callApiLike)
} catch (error) {
yield put(actions.setError(error))
yield put(actions.addLike(timeline.id, -1))
}
yield put(actions.setLoading(false))
}
}
Expand Down

0 comments on commit 60a160d

Please sign in to comment.