Skip to content

Instantly share code, notes, and snippets.

@safronman
Created July 1, 2022 11:34
Show Gist options
  • Save safronman/df79fdac4cf5e4b4a159da460163cdbc to your computer and use it in GitHub Desktop.
Save safronman/df79fdac4cf5e4b4a159da460163cdbc to your computer and use it in GitHub Desktop.

Revisions

  1. safronman created this gist Jul 1, 2022.
    79 changes: 79 additions & 0 deletions error-utils.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,79 @@
    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}`))
    }
    }
    }