Created
July 1, 2022 11:34
-
-
Save safronman/df79fdac4cf5e4b4a159da460163cdbc to your computer and use it in GitHub Desktop.
axios catch TS error
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { setAppErrorAC, SetAppErrorActionType } from '../../app/app-reducer'; | |
| import { Dispatch } from 'redux'; | |
| import axios, { AxiosError } from 'axios'; | |
| import { AppThunk } from '../../app/store'; | |
| import { packsAPI } from '../../api/packs-api'; | |
| export const errorUtils = (e: Error | AxiosError<{error: string}>, dispatch: Dispatch<SetAppErrorActionType>) => { | |
| const err = e as Error | AxiosError<{ error: string }> | |
| if (axios.isAxiosError(err)) { | |
| const error = err.response?.data ? err.response.data.error : err.message | |
| dispatch(setAppErrorAC(error)) | |
| } else { | |
| dispatch(setAppErrorAC(`Native error ${err.message}`)) | |
| } | |
| } | |
| // https://github.com/axios/axios/issues/3612 | |
| // 1. then/catch | |
| // 1 ver | |
| const deletePackTC1 = (packId: string): AppThunk => dispatch => { | |
| packsAPI.deletePack(packId) | |
| .then(() => { | |
| // code | |
| }) | |
| .catch((err: AxiosError<{ error: string }>) => { | |
| const error = err.response | |
| ? err.response.data.error | |
| : err.message | |
| console.log('error: ', error) | |
| }) | |
| } | |
| // 2 ver | |
| const deletePackTC2 = (packId: string): AppThunk => dispatch => { | |
| packsAPI.deletePack(packId) | |
| .then(() => { | |
| // code | |
| }) | |
| .catch((err: AxiosError) => { | |
| const error = err.response | |
| ? (err.response.data as ({ error: string })).error | |
| : err.message | |
| console.log('error: ', error) | |
| }) | |
| } | |
| // 2. try/catch | |
| // 1 ver | |
| export const deletePackTC3 = (packId: string): AppThunk => async dispatch => { | |
| try { | |
| // code | |
| } catch (e) { | |
| const err = e as Error | AxiosError<{ error: string }> | |
| if (axios.isAxiosError(err)) { | |
| const error = err.response?.data ? err.response.data.error : err.message | |
| dispatch(setAppErrorAC(error)) | |
| } else { | |
| dispatch(setAppErrorAC(`Native error ${err.message}`)) | |
| } | |
| } | |
| } | |
| // 2 ver | |
| export const deletePackTC4 = (packId: string): AppThunk => async dispatch => { | |
| try { | |
| // code | |
| } catch (e) { | |
| const err = e as Error | AxiosError | |
| if (axios.isAxiosError(err)) { | |
| const error = err.response?.data ? (err.response.data as { error: string }).error : err.message | |
| dispatch(setAppErrorAC(error)) | |
| } else { | |
| dispatch(setAppErrorAC(`Native error ${err.message}`)) | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment