Effect CommunityEC
Effect Community2mo ago
3 replies
kristo

Handling ParseErrors in Effect Typescript HttpApi without per-handler wrapping?

Hi! I'm building an HttpApi where virtually every endpoint calls Schema.decode internally to validate database results before returning domain models. This means repository methods can fail with
ParseError
.

The issue:
ParseError
isn't a schema and can't be serialized by the api. There is an existing pattern to map them into schemas like in HttpApiDecodeError.fromParseError, but using this requires me to wrap very endpoint in an error mapper:

  export const mapParseError = <A, E, R>(
    effect: Effect.Effect<A, E | ParseResult.ParseError, R>
  ): Effect.Effect<A, E | HttpApiDecodeError, R> =>
    effect.pipe(
      Effect.catchIf(
        ParseResult.isParseError,
        HttpApiDecodeError.refailParseError
      )
    );

  // Usage in handlers
  handlers.handle("endpoint", ({ path }) =>
    someEffect(path).pipe(mapParseError)
  )


This works but requires wrapping every repository call with mapParseError.

Question: Is there a way to handle ParseErrors globally in the HttpApi without per-handler wrapping? Is there any other pattern y'all are using for this?

Should I make a new Schema.decode that fails with my own ParseErrorSchema and use that everywhere?
Was this page helpful?