Go like error handling in TypeScript

In this article we’ll learn about how we can handle our errors like Go with TypeScript.

Note: In TypeScript this is probably not a “best practice”, or even a good practice at all but let’s have fun experimenting nonetheless!

Let’s take the following …


This content originally appeared on DEV Community and was authored by Karan Pratap Singh

In this article we'll learn about how we can handle our errors like Go with TypeScript.

Note: In TypeScript this is probably not a "best practice", or even a good practice at all but let's have fun experimenting nonetheless!

Let's take the following as an example.

import * as fs from 'fs/promises';

async function main(): Promise<void> {
  try {
    const result: Buffer = await fs.readFile('./package.json');
    // Do something with result
  } catch (error) {
    // Handle error
  }
}

main();

In Go, this should look as below.

package main

import "io/ioutil"

func main() {
    data, err := ioutil.ReadFile("./package.json")

    if err != nil {
        // Handle error
    }

    // Do something with data
}

Let's write our async handler helper function! This function basically returns a Tuple of result and error as TypeScript doesn't have multiple returns.

type Maybe<T> = T | null;

type AsyncResult = any;
type AsyncError = any;
type AsyncReturn<R, E> = [Maybe<R>, Maybe<E>];
type AsyncFn = Promise<AsyncResult>;

async function async<R = AsyncResult, E = AsyncError>(
  fn: AsyncFn
): Promise<AsyncReturn<R, E>> {
  try {
    const data: R = await fn;
    return [data, null];
  } catch (error) {
    return [null, error];
  }
}

We can use our async utility as below. We can pass our Result and Error generics types.

import * as fs from 'fs/promises';

async function main(): Promise<void> {
  const fn: Promise<Buffer> = fs.readFile('./package.json');
  const [data, error] = await async<Buffer, NodeJS.ErrnoException>(fn);

  if (error) {
    // Handle error
  }

  // Do something with data
}

main();

Perfect! We now have isolated our try/catch with Go like error handling pattern.


This content originally appeared on DEV Community and was authored by Karan Pratap Singh


Print Share Comment Cite Upload Translate Updates
APA

Karan Pratap Singh | Sciencx (2021-08-20T10:59:30+00:00) Go like error handling in TypeScript. Retrieved from https://www.scien.cx/2021/08/20/go-like-error-handling-in-typescript/

MLA
" » Go like error handling in TypeScript." Karan Pratap Singh | Sciencx - Friday August 20, 2021, https://www.scien.cx/2021/08/20/go-like-error-handling-in-typescript/
HARVARD
Karan Pratap Singh | Sciencx Friday August 20, 2021 » Go like error handling in TypeScript., viewed ,<https://www.scien.cx/2021/08/20/go-like-error-handling-in-typescript/>
VANCOUVER
Karan Pratap Singh | Sciencx - » Go like error handling in TypeScript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/20/go-like-error-handling-in-typescript/
CHICAGO
" » Go like error handling in TypeScript." Karan Pratap Singh | Sciencx - Accessed . https://www.scien.cx/2021/08/20/go-like-error-handling-in-typescript/
IEEE
" » Go like error handling in TypeScript." Karan Pratap Singh | Sciencx [Online]. Available: https://www.scien.cx/2021/08/20/go-like-error-handling-in-typescript/. [Accessed: ]
rf:citation
» Go like error handling in TypeScript | Karan Pratap Singh | Sciencx | https://www.scien.cx/2021/08/20/go-like-error-handling-in-typescript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.