ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋ฌธ์ œ

์„œ๋ฒ„์™€์˜ API ํ†ต์‹  ๋„์ค‘ ๋น„๋™๊ธฐ์  ์ฒ˜๋ฆฌ์˜ ์ธ์‹๊ณผ ์ œ์–ด๋ฅผ ํšจ์œจ์ ์œผ๋กœ ํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด redux-saga ๋ฏธ๋“ค์›จ์–ด๋ฅผ ์ด์šฉํ•œ ์œ ํ‹ธ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค.

export const createRequestActionTypes = type => {
  const SUCCESS = `${type}_SUCCESS`;
  const FAILURE = `${type}_FAILURE`;
  return [type, SUCCESS, FAILURE];
};

export default function createRequestSaga(type, request) {
  const SUCCESS = `${type}_SUCCESS`;
  const FAILURE = `${type}_FAILURE`;

  return function* (action) {
    yield put(startLoading(type));

    try {
      const response = yield call(request, action.payload);
      yield put({
        type: SUCCESS,
        payload: response.data,
      });
    } catch (error) {
      yield put({
        type: FAILURE,
        payload: error,
        error: true,
      });
    }
    yield put(finishLoading(type));
  };
}

 

์ œ๋„ˆ๋ ˆ์ดํ„ฐ ํ•จ์ˆ˜ ๋‚ด์—์„œ call() ํ•จ์ˆ˜๊ฐ€ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒฐ๊ณผ ๊ฐ’์— ๋”ฐ๋ผ์„œ ๊ฐ๊ธฐ ๋‹ค๋ฅธ ํ›„์† ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋Š” ์œ ํ‹ธ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜์˜€๋Š”๋ฐ, ์—๋Ÿฌ ์ฒ˜๋ฆฌ์— ๋Œ€ํ•œ ์•ก์…˜์„ ํŠธ๋ฆฌ๊ฑฐํ•˜์˜€์„ ๋•Œ ์ œ๋Œ€๋กœ ๋œ ์—๋Ÿฌ ์ฒ˜๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์—†๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€๋‹ค.

 

 

๋ฌธ์ œํ•ด๊ฒฐ

ํ•ด๋‹น ๋ฌธ์ œ๋Š” redux-sage ๋ฏธ๋“ค์›จ์–ด์—์„œ ๋ฐœ์ƒํ•œ ๋ฌธ์ œ๊ฐ€ ์•„๋‹Œ axios ์—๋Ÿฌ ์ฒ˜๋ฆฌ์— ๋Œ€ํ•ด์„œ ๋ฏธํกํ•˜๊ฒŒ ์•Œ๊ณ  ์žˆ์—ˆ๋˜ ๊ฒƒ์—์„œ ๋ฐœ์ƒํ•˜์˜€๋‹ค.

 

๋ฆฌ๋“€์„œ ๋‚ด์—์„œ API ๋„์ค‘ ์—๋Ÿฌ ์ฒ˜๋ฆฌ๋ฅผ ๋‹ด๋‹นํ•˜๋Š” ์•ก์…˜์˜ ๊ฒฝ์šฐ๋ฅผ ์ฝ”๋“œ๋กœ ์‚ดํŽด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

const auth = handleActions(
  {
  
    ( ... )
    
    [DUPLICATIONCHECK_FAILURE]: (state, { payload: error }) => ({
      ...state,
      authError: response.data.message,
    })
  },
  initialState
);

 

์„œ๋ฒ„์— ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ์œ ์ €์ผ ๊ฒฝ์šฐ, ์ค‘๋ณต ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒ๋˜๊ณ  ํ•ด๋‹น ์—๋Ÿฌ์— ๋Œ€ํ•œ ๋‚ด์šฉ์„ [DUPLICATIONCHECK_FAILURE] ์•ก์…˜์—์„œ ์ฒ˜๋ฆฌํ•ด ์ฃผ๋„๋ก ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์˜€๋‹ค.

 

ํ•˜์ง€๋งŒ error์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ put() ์ธ์ž์— ๋‹ด์•„ ๋ฆฌ๋“€์„œ ๋‚ด์—์„œ ๋””์ŠคํŠธ๋Ÿญ์ณ๋ง์„ ํ†ตํ•ด error ํ‚ค ๊ฐ’์„ state์— ๋ฐ”๋กœ ์ €์žฅํ•˜๋Š” ์ฒ˜๋ฆฌ ๋‹จ๊ณ„์—์„œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋˜ ๊ฒƒ์ด๋‹ค.

 

์„œ๋ฒ„์—์„œ ๋ฐ˜ํ™˜ํ•œ ์—๋Ÿฌ๋Š” ์—ฌ๋Ÿฌ ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ๊ฐ์ฒด์˜ ํ˜•ํƒœ์ด๋‹ค.

https://yamoo9.github.io/axios/guide/error-handling.html

 

์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ | Axios ๋Ÿฌ๋‹ ๊ฐ€์ด๋“œ

์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ axios ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. axios.get('/user/12345') .catch(function (error) { if (error.response) { console.log(error.response.data); console.log(error.response.status); console.log(error.response.he

yamoo9.github.io

 

ํ˜„์žฌ ํ•ด๋‹น ๋กœ์ง์—์„œ ํ•„์š”ํ•œ ๊ฐ’์€ ํšŒ์› ๊ฐ€์ž…์„ ์ง„ํ–‰ํ•˜์˜€์„ ๋•Œ ์ด๋ฉ”์ผ์„ ํ† ๋Œ€๋กœ ํ•ด๋‹น ์œ ์ €๊ฐ€ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ์— 'DUPLICATION ERROR' message๋ฅผ ๋ฐ›๋Š” ๊ฒƒ์ธ๋ฐ, ํ•ด๋‹น ๊ฐ’์€ ์ „๋‹ฌ ๋ฐ›์€ error์— ๋ฐ”๋กœ ๋ฐ”์ธ๋”ฉ๋œ ๊ฒƒ์ด ์•„๋‹Œ ๋‚ด๋ถ€ ํ‚ค ๊ฐ’์œผ๋กœ ์กด์žฌํ•˜์˜€๋‹ค.

 

๊ทธ๋ž˜์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์—๋Ÿฌ์— ๋Œ€ํ•œ ๊ฐ ์ •๋ณด๋ฅผ ์ฝ˜์†”๋กœ ํ™•์ธํ•ด๋ณด๋‹ˆ ํ˜„์žฌ ํ•„์š”ํ•œ ์ •๋ณด๊ฐ€ error.response.data ๋‚ด์— message์— ์กด์žฌํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

return client
    .post(`${API.baseUrl}users/${currentCheckInputAPI}`, {
      [currentCheckKey]: currentCheckInputValue,
    })
    .then(res => console.log(res.message))
    .catch(error =>
      console.log(
        error.response.data,
        error.response.status, 
        error.response.headers
      )
    );

 

์ด์— ๋ฆฌ๋“€์„œ ๋‚ด์—์„œ ํ•ด๋‹น ๊ฐ’์„ store์— ์ €์žฅํ•ด์ฃผ๊ธฐ ์œ„ํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜์˜€๋‹ค.

const auth = handleActions(
  {
  
	( ... )
    
    [DUPLICATIONCHECK_FAILURE]: (state, { payload: { response } }) => ({
      ...state,
      authError: response.data.message,
    })
  },
  initialState
);
๋Œ“๊ธ€
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
TAG
more
ยซ   2024/10   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
๊ธ€ ๋ณด๊ด€ํ•จ