except-js

0.4.0 • Public • Published

except-js

except-js provides the necessary abstractions for handling exceptions of different types within multiple catch statements. This is pretty standard on strongly typed languages like Java.

Usage

Polished Syntax

const { create, except } = require('except-js')

const Exception1Test = create('Exception1Test')
const Exception2Test = create('Exception2Test')

function helloWorld(){
    throw new Exception1Test('ExceptionTest here!')
}

try {
    helloWorld()
}catch(exception){
    except(exception)
        .on(Exception1Test)
        .do(() => {
            console.log('[Exp1] Message:', exception.message)
        })
        .on(Exception2Test)
        .do(() => {
            console.log('[Exp2] Message:', exception.message)
        })
        .done()
}

Traditional Syntax

const { create, handle } = require('except-js')

const ExceptionTest = create('ExceptionTest')

function helloWorld(){
    throw new ExceptionTest('ExceptionTest here!')
}

try {
    helloWorld()
}catch(exception){
    handle(exception, [
        {
            exception: ExceptionTest,
            handler: () => {
                console.log('Message:', exception.message)
            }
        }
    ])
}

Note: If no matching handler is passed, the exception is thrown again. This is a design decision in order to preserve the decoupling of the sender from the receiver of the exception (exception propagation across the stack).

License

MIT

/except-js/

    Package Sidebar

    Install

    npm i except-js

    Weekly Downloads

    5

    Version

    0.4.0

    License

    MIT

    Unpacked Size

    8.34 kB

    Total Files

    5

    Last publish

    Collaborators

    • hadichahine