Skip to content

kirusick/compose-catch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

compose-catch

Compose your catch handlers

npm i compose-catch

handlers.js

export function forbidenHandler(error) {
    if (error.status === 403) {
        /* Do something */;
    } else {
        throw error;
    }
}

export function notFoundHandler(error) {
    if (error.status === 404) {
        /* Do something */;
    } else {
        throw error;
    }
}

export function commonHandler(error) {
    /* Do something */
}
import composeCatch from 'compose-catch';
import { forbidenHandler, notFoundHandler, commonHandler } from './handlers';

try {
    const res = await fetch();
} catch (error) {
    return composeCatch(
        forbidenHandler,
        notFoundHandler,
        commonHandler,
    )(error);
}

You also can create composed handler and use it in all project

composedHandlers.js

import composeCatch from 'compose-catch';
import { forbidenHandler, notFoundHandler, commonHandler } from './handlers';

export const composedHandler = composeCatch(
    forbidenHandler,
    notFoundHandler,
    commonHandler,
);
import { composedHandler } from './composedHandlers';

try {
    const res = await fetch();
} catch (error) {
    return composedHandler(error);
}

About

npm module to compose you catch handlers

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published