0.0.3 • Published 5 years ago

trysafe v0.0.3

Weekly downloads
26
License
MIT
Repository
github
Last release
5 years ago

⛵ 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'