Alt description missing in image

Function Utils

import * as functionUtils from '@green-stack/utils/functionUtils'

Collection of utility functions for handling asynchronous operations and error handling:

        • functionUtils.ts

Error Handling

tryCatch()

Attempts to execute a promise, wraps it with try/catch, and returns either the data or error in a structured way.

// Successful promise
const successPromise = Promise.resolve({ data: 'success' })
const result = await tryCatch(successPromise)
// => { data: { data: 'success' } }
 
// Failed promise with Error
const errorPromise = Promise.reject(new Error('Something went wrong'))
const errorResult = await tryCatch(errorPromise)
// => { error: Error('Something went wrong') }
 
// Failed promise with non-Error
const nonErrorPromise = Promise.reject('String error')
const nonErrorResult = await tryCatch(nonErrorPromise)
// => { error: Error('String error') }

Common use cases:

// API call with error handling
const fetchData = async () => {
    const { data, error } = await tryCatch(fetch('/api/data'))
    
    if (error) {
        console.error('Failed to fetch data:', error)
        return null
    }
    
    return data
}
 
// Database operation
const createUser = async (userData) => {
    const { data, error } = await tryCatch(db.users.create(userData))
    
    if (error) {
        // Handle specific error cases
        if (error.message.includes('duplicate')) {
            throw new Error('User already exists')
        }
        throw error
    }
    
    return data
}

This is particularly useful when you want to:

  • Handle errors in a consistent way
  • Avoid try/catch blocks in your business logic
  • Ensure all errors are properly converted to Error objects
  • Keep error handling separate from the main logic flow