Last active
June 21, 2022 12:33
-
-
Save degitgitagitya/495e5613e89d9a660f747ce40e90ca1b to your computer and use it in GitHub Desktop.
Clean Error Handler For Axios (Typescript)
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 { AxiosError } from 'axios'; | |
/** | |
* @param {Promise<{data:T;}>} promise | |
* @param {(error:AxiosError<ErrorResolverBackEndDataType>)=>ErrorResolverBackEndType} resolveError | |
*/ | |
export const resolvePromise = async <T>( | |
promise: Promise<{ | |
data: T; | |
}>, | |
resolveError: ( | |
error: AxiosError<ErrorResolverBackEndDataType> | |
) => ErrorResolverBackEndType | |
) => { | |
try { | |
const { data } = await promise; | |
return [data, null] as const; | |
} catch (error) { | |
return [null, resolveError(error)] as const; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment