New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

@minimal-effort/safe-try

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@minimal-effort/safe-try

Safe-try is a Typescript type-safe error handling library for both sync and async operations

latest
Source
npmnpm
Version
1.2.0
Version published
Maintainers
1
Created
Source

Safe-try

TypeScript type safe error handling libary for both sync and async operations


License

Basic Usage

import { Try } from "@minimal-effort/safe-try"

// Synchronous operations
const { data, error } = Try.catch(() => JSON.parse('{"name": "Asep"}'))
if (error) {
    console.error("Parse failed:", error)

    if (error instanceof Error) {
        // handle error
    }
}

console.log("Parsed data:", data) // { name: "Asep" }

// Asynchronous operations
const fetchUser = async () => {
    const result = await fetch('/api')
    if (!result.ok) throw new Error('Api Error')
    return result
}

const { data, error } = await Try.catch(async () => fetchUser())
if (error) {
    console.error("failed:", error)
    // Handle error
}

const { data, error } = await Try.catch(fetchUser)
if (error) {
    console.error("failed:", error)
    // Handle error
}

Advanced Configuration

// Resilient API call with timeout, retry, and exponential backoff
const result = await Try.catch(fetchUser)
    .withTimeout(5000)                          // 5 second timeout
    .withRetry(3)                              // Retry up to 3 times
    .withBackoff({                             // Exponential backoff
        baseDelayMs: 1000,                       // Start with 1 second
        exponential: true,                       // Double delay each retry
        maxDelayMs: 10000,                       // Cap at 10 seconds
        jitter: true                             // Add randomness
    })

if (result.error) {
    console.error("Operation failed after all retries:", result.error)
    // handle error
}

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

Try.sleep(ms)

Utility method for creating delays.

await Try.sleep(1000) // Wait 1 second

File Operations

import { readFile } from 'fs/promises'

const result = await Try.catch(async () => readFile('config.json', 'utf-8'))
    .withRetry(2)

const config = result.error
    ? getDefaultConfig()
    : JSON.parse(result.data)

Parsing Operations

function parseJsonSafely(jsonString: string) {
    const result = Try.catch(() => JSON.parse(jsonString))

    return result.error
        ? { valid: false, error: result.error.message }
        : { valid: true, data: result.data }
}

Advanced Patterns

Chaining Operations

async function processUserData(userId: string) {
  // Fetch user
    const userResult = await Try.catch(async () => fetchUser(userId))
        .withTimeout(5000)
        .withRetry(2)

  if (userResult.error) return userResult

  // Process user data
  const processResult = Try.catch(() => processUser(userResult.data))
  if (processResult.error) return processResult

  // Save processed data
  const saveResult = await Try.catch(async () => saveUser(processResult.data))
        .withRetry(3)

  return saveResult
}

Custom Error Types

class ApiError extends Error {
    constructor(public status: number, message: string) {
        super(message)
    }
}

const result = await Try.catch<User, ApiError>(fetchUser)
if (result.error) {
    console.error(`API Error ${result.error.status}: ${result.error.message}`)
    // handle error
}

Star ⭐ this repo if you find it useful!

Keywords

error-handling

FAQs

Package last updated on 11 Aug 2025

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