Effect CommunityEC
Effect Community3y ago
1 reply
joystick

Error Handling and Data Unmarshalling in TypeScript

Hello, how are you doing?

I need some help with Typescript:

class UnmarshallError extends Error {
  readonly _tag = "UnmarshallError";
}

class NoSubscriptionsFound {
  readonly _tag = "NoSubscribersFoundError";
}

const ensureItems = (data: ScanCommandOutput): Effect.Effect<
  never,
  NoSubscriptionsFound,
  Record<string, AttributeValue>[]
  > => data.Items && data.Items.length > 0
    ? Effect.succeed(data.Items)
    : Effect.fail(new NoSubscriptionsFound())

const unmarshallData = (data: ScanCommandOutput) => pipe(
  ensureItems(data),
  Effect.flatMap((items) =>
    Effect.all(items.map(item => Effect.try({
      try: () => unmarshall(item as { [key: string]: AttributeValue }),
      catch: (e: Error) => new UnmarshallError(e.message)
    })))
  )
)

Typescript complains on Effect.try:
typescript: No overload matches this call. Overload 1 of 2, '(options: { readonly try: LazyArg<Record<string, any>>; readonly catch: (error: unknown) => UnmarshallError; }): Effect<never, UnmarshallError, Record<...>>', gave the following error. Type '(e: Error) => UnmarshallError' is not assignable to type '(error: unknown) => UnmarshallError'. Types of parameters 'e' and 'error' are incompatible. Type 'unknown' is not assignable to type 'Error'. Overload 2 of 2, '(evaluate: LazyArg<unknown>): Effect<never, unknown, unknown>', gave the following error. Argument of type '{ try: () => Record<string, any>; catch: (e: Error) => UnmarshallError; }' is not assignable to parameter of type 'LazyArg<unknown>'. Object literal may only specify known properties, and 'try' does not exist in type 'LazyArg<unknown>'. [2769]
Was this page helpful?