Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@nickfthedev/gotry

Package Overview
Dependencies
Maintainers
0
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@nickfthedev/gotry

Go-style error handling with try-catch wrapper

  • 1.0.0
  • latest
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

gotry

TypeScript/JavaScript error handling inspired by Go's error pattern.

Why?

  • ✨ Simple and clean error handling pattern
  • 🔄 Supports both sync and async functions
  • 📦 Zero dependencies
  • 🛡️ Written in TypeScript with full type safety
  • 🎯 Reduces error handling boilerplate
  • 🔍 Inspired by Go's error handling pattern

Installation

npm install @nickfthedev/gotry

Usage

The package provides two functions: gotry for synchronous operations and gotryAsync for asynchronous operations. Both return a tuple of [result, error].

Synchronous Usage

import { gotry } from "gotry";

const [result, error] = gotry(() => {
  return someRiskyOperation();
});

if (error) {
  console.error("Operation failed:", error);
  return;
}

console.log("Success:", result);

Asynchronous Usage

import { gotryAsync } from "gotry";

const [result, error] = await gotryAsync(async () => {
  return await fetchSomeData();
});

if (error) {
  console.error("Fetch failed:", error);
  return;
}

console.log("Data:", result);

API Reference

gotry(fn: () => T): [T | null, Error | null]

Executes a synchronous function and returns a tuple containing the result and error (if any).

import { gotry } from "gotry";

const [result, error] = gotry(() => {
  if (Math.random() > 0.5) throw new Error("Bad luck!");
  return "Success!";
});

gotryAsync(fn: () => Promise): Promise<[T | null, Error | null]>

Executes an asynchronous function and returns a promise of a tuple containing the result and error (if any).

import { gotryAsync } from "gotry";

const [result, error] = await gotryAsync(async () => {
  const response = await fetch("https://api.example.com/data");
  return response.json();
});

Error Handling Pattern

Both functions follow Go's error handling pattern:

  • On success: Returns [result, null]
  • On error: Returns [null, error]

All errors are guaranteed to be instances of Error. If a non-Error is thrown, it will be converted to an Error instance.

License

MIT

Keywords

FAQs

Package last updated on 04 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc