JSPM

@moeenn/recover

2.0.0
    • ESM via JSPM
    • ES Module Entrypoint
    • Export Map
    • Keywords
    • License
    • Repository URL
    • TypeScript Types
    • README
    • Created
    • Published
    • Downloads 65
    • Score
      100M100P100Q63748F
    • License ISC

    Easy type-safe error handling without try-catch

    Package Exports

    • @moeenn/recover
    • @moeenn/recover/dist/index.js
    • @moeenn/recover/dist/index.mjs

    This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@moeenn/recover) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

    Readme

    @moeenn/recover

    I dislike try-catch. The syntax is clunky and inconvenient. This library provides a somewhat similar approach to error handling as Golang. I believe error handling should be as convenient as possible so we as developers are more inclined to use it in more places in our code.

    Installation

    $ npm i @moeenn/recover

    Usage

    import { recover } from "@moeenn/recover"
    
    /**
     * Example action
     * @returns {number}
     */
    function syncAction() {
      return 42
    }
    
    /** @returns {void} */
    function main() {
      const result = recover(() => syncAction())
      if (result.error) {
        /** in case of error, res.error will be of type Error (instead of unknown) */
        console.error("Error: ", result.error.message)
        return
      }
    
      /** type is automatically inferred as number */
      console.log(result.ok)
    }
    
    main()
    import { recoverAsync } from "@moeenn/recover"
    
    /** @returns {Promise<void>} */
    async function main() {
      const url = "https://jsonplaceholder.typicode.com/todos/1"
      const res = await recoverAsync(() => fetch(url))
      if (res.error) {
        console.error("Error: failed to fetch", res.error.message)
        return
      }
    
      const body = await recoverAsync(() => res.ok.json())
      if (body.error) {
        console.error("Error: failed to parse response as JSON", body.error.message)
        return
      }
    
      console.log(body.ok)
    }
    
    main().catch(console.error)
    import { recover, Ok, Err } from "@moeenn/recover"
    
    // manually handle errors of unknown type
    const result = recover(() => action(), (error: unknown): Err => {
      // convert `unknown` error into Error instance
      const err = new Error("TODO")
      return { ok: null, error: err }
    })

    Running test

    $ npm test