3

GitHub - vitor-mariano/go-catch: Handle errors without try/catch, in the Go styl...

 3 years ago
source link: https://github.com/vitor-mariano/go-catch
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Go Catch

Handle errors without try/catch, in the Go style. Compatible with async and sync functions, preserving its types.

Installation

npm install --save go-catch

Import goCatch for async functions or goCatchWrap for regular sync functions.

import { goCatch, goCatchWrap } from 'go-catch';

Examples

Regular sync function

// Without Go Catch
const result = JSON.parse(value);

// With Go Catch
const [result, err] = goCatchWrap(JSON.parse)(value);
import { goCatchWrap } from 'go-catch';

interface Response {
  value: number;
}

// You can specify the return type of the function using generics
// or let TypeScript infer it automatically.
const [parsedValue, parseError] = goCatchWrap<Response>(JSON.parse)(input);

// You can handle errors checking their types or simply checking whether
// they are null
if (parseError instanceof SyntaxError) {
  console.error('Invalid response: ' + input);
  process.exit(1);
}

// After validated that there are no errors, you can use the
// non-null assertion operator to tell TypeScript that parsedValue
// should not be treated as null.
parsedValue!.a;

Async functions

// Without Go Catch
const result = await axios.get('/user', params);

// With Go Catch
const [result, err] = await goCatch(axios.get('/user', params));
import { goCatch } from 'go-catch';

async function getUser(...args): Promise<User> | null {
  // With async functions, you can call the function directly inside of the goCatch as an argument.
  const [response, err] = await goCatch(axios.get<User>('/user', params));

  if (err) {
    return console.error(err);
  }

  response!.data;

  // ...
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK