Skip to content
This repository has been archived by the owner on Aug 19, 2020. It is now read-only.
/ trysafe Public archive

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

License

Notifications You must be signed in to change notification settings

yarnaimo/trysafe

Repository files navigation

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

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published