0.0.2 • Published 6 years ago

compose-catch v0.0.2

Weekly downloads
3
License
ISC
Repository
github
Last release
6 years ago

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);
}