0.4.0 • Published 5 months ago

except-js v0.4.0

Weekly downloads
1
License
MIT
Repository
github
Last release
5 months ago

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

0.4.0

5 months ago

0.3.0

3 years ago

0.2.0

3 years ago

0.1.1

4 years ago

0.1.0

4 years ago