Noisy Pillaging Monster

    trysafe
    TypeScript icon, indicating that this package has built-in type declarations

    0.0.3 • Public • Published

    TrySafe

    Safe and simple error handling for TypeScript, inspired by Golang and Scala

    Install

    yarn add trysafe

    Usage

    Try and TryAsync returns an Either type defined in fp-ts.

    import { Try, TryAsync } from 'trysafe'
    
    // sync
    const result = Try(() => {
        if (condition) {
            throw new Error('failed')
            // or
            throw 'failed'
        }
    
        return 'succeeded'
    })
    
    // async
    const result = await TryAsync(async () => {
        if (condition) {
            throw new Error('failed')
            // or
            throw 'failed'
        }
    
        return 'succeeded'
    })
    
    // result: Either<Error, string>
    
    if (result.isLeft()) {
        // result.value: Error
    
        console.error(result.value.message) // -> 'failed'
        return
    }
    
    // result.value: string
    
    console.log(result.value) // -> 'succeeded'

    Install

    npm i trysafe

    DownloadsWeekly Downloads

    2

    Version

    0.0.3

    License

    MIT

    Unpacked Size

    13.3 kB

    Total Files

    22

    Last publish

    Collaborators

    • yarnaimo