Effect CommunityEC
Effect Community3y ago
10 replies
firefox

Wrapping Function X to Handle Errors

Let's say I have function X that can throw an error at any given time

const X = async (id: unknown) => {
  if (typeof id !== 'number') throw // this could throw an error
  const item = await getItemViaId(id) // this could throw an error
  return JSON.parse(item) // this could throw an error
}

I want to wrap this function with some utils so that if an error is thrown I will return { success: false, error: string}, otherwise, return { success: true, data }. I'd assume Effect can help here, what would be the correct pattern?
Was this page helpful?