Standardize the way you try/catch HTTP request
Are you reading this on npm
? Check out the full and updated documentation on GitHub.
npm i @openish-u/trycatchfy
Have you started a service and forgotten to treat some HTTP status errors? Do you know how to treat an HTTP status error? Or worst, you know how to treat an HTTP request error, but all time do you need to remember someone how to do that thought pull request?
Trycatchfy
is a way to have sure that a team is following a pattern.
import { trycatchfy } from './trycatchfy';
const getSomething = () => {
const expectedBehavior = async () => {
const response = await httpRequest()
console.log(response)
}
const onUnauthorizedError = (axiosResponse) => {
console.error('Sorry, looks like you do not have permission', axiosResponse)
}
const onEndCycle = () => {
console.log('Process stopped')
}
return trycatchfy({
expectedBehavior,
onUnauthorizedError,
onEndCycle,
// other errors
// onInternalServerError,
// onHttpExceptionError,
// onForbiddenError,
// onResourceError,
// onScriptError,
});
}
First, define your pattern by wrapping Trycatchfy
. See it as a config file. Do once.
import { initTrycatchfy } from './trycatchfy';
import { ITrycatchfyParams } from '../index';
interface IFakeAxios {
response: any;
status: number;
}
export const customHttpErrors = [
{ statusCode: 900, handleName: 'myCustomStatusCode' },
];
const trycatchfy = initTrycatchfy({
customHttpErrors, // optional custom handle
});
export const wrapperTrycatchfy = ({
expectedBehavior,
onForbiddenError,
onResourceError,
onScriptError,
onEndCycle,
onHttpExceptionError,
}: Omit<
ITrycatchfyParams<IFakeAxios>,
'onUnauthorizedError' | 'onInternalServerError'
>) => {
// handle Unauthorized Error once
const onUnauthorizedErrorDefault = () => {
console.log('logout the user');
};
// handle Internal Server Error once
const onInternalServerErrorDefault = () => {
console.log('server error - reload');
};
return trycatchfy<IFakeAxios>({
expectedBehavior,
onForbiddenError,
onResourceError,
onScriptError,
onHttpExceptionError,
onUnauthorizedError: onUnauthorizedErrorDefault,
onInternalServerError: onInternalServerErrorDefault,
onEndCycle,
});
};
Then, use it on your components
import { wrapperTrycatchfy } from './wrapperTrycatchfy'
const expectedBehavior = async () => {
const response = await httpRequest()
console.log(response)
}
wrapperTrycatchfy({
expectedBehavior,
// These follow handle are also required
// onEndCycle,
// onForbiddenError,
// onResourceError,
// onScriptError,
});