Skip to content

Parameterize HttpApi base errorSchema - #5654

Closed
Avaq wants to merge 2 commits into
Effect-TS:mainfrom
Avaq:avaq/override-decode-error-schema
Closed

Avaq wants to merge 2 commits into
Effect-TS:mainfrom
Avaq:avaq/override-decode-error-schema

Conversation

@Avaq

@Avaq Avaq commented Oct 23, 2025

Copy link
Copy Markdown
Contributor

Type

  • Refactor
  • Feature
  • Bug Fix
  • Optimization
  • Documentation Update

Description

This allows users to customise how HttpApiDecodeErrors are represented on the wire, enabling the use of HttpApi for wire protocols that don't follow the Tagged Error approach to encoding errors.

Related

@Avaq
Avaq requested a review from tim-smart as a code owner October 23, 2025 15:33
@changeset-bot

changeset-bot Bot commented Oct 23, 2025 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 3ad8816

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@gcanti

gcanti commented Nov 3, 2025

Copy link
Copy Markdown
Contributor

I'm not sure this will work as intended, in handlerToRoute within HttpApiBuilder.ts there's a HttpApiDecodeError.refailParseError, which makes me think the error logic is fixed

@Avaq

Avaq commented Nov 3, 2025

Copy link
Copy Markdown
Contributor Author

@gcanti yes -- it converts ParseError to HttpApiDecodeError. But at a later point, the (now overridable) schema is used to encode that HttpApiDecodeError for the wire. So no matter the origin of the HttpApiDecodeError, the schema is still going to work, as far as I understand it.

At least in my limited testing, it worked for both the OpenApi output and the encoding of Decode Errors.

@gcanti

gcanti commented Nov 5, 2025

Copy link
Copy Markdown
Contributor

I see, I'll let @tim-smart chime in then

@Avaq

Avaq commented Nov 5, 2025

Copy link
Copy Markdown
Contributor Author

I initially worked on a version where the user-supplied type of the encoded HttpApiDecodeError was threaded as a generic type through the HttpApp, but eventually found that this wasn't necessary.

And the user supplied schema doesn't even need to be lossless, it's fine to "encode" every HttpApiDecodeError to the value 42 and just have a decoder that always fails, as long as there's a schema for 42 added with addError. That way the HttpClient will be able to decode the weirdly encoded errors by just failing over to the schema that's able to decode it, as with any Union decoder.

@Avaq

Avaq commented Nov 30, 2025

Copy link
Copy Markdown
Contributor Author

@tim-smart could you please take a look at this? Most of the diff is just a new test fixture, the actual change is just ~10 loc.

Comment thread packages/platform/src/HttpApi.ts Outdated
export const make = <const Id extends string>(identifier: Id): HttpApi<Id, never, HttpApiDecodeError> =>
export const make = <const Id extends string>(
identifier: Id,
errorSchema: Schema.Schema<HttpApiDecodeError, any> = HttpApiDecodeError

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally this would be an options object, and instead of any you can use an discarded generic.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I try to use a discarded generic, TypeScript issues:

Type 'typeof HttpApiDecodeError' is not assignable to type 'Schema<HttpApiDecodeError, E, never>'.
Types of property 'Encoded' are incompatible.
Type '{ readonly _tag: "HttpApiDecodeError"; readonly message: string; readonly issues: readonly { readonly _tag: "Type" | "Pointer" | "Unexpected" | "Missing" | "Composite" | "Refinement" | "Transformation" | "Forbidden"; readonly message: string; readonly path: readonly (string | ... 1 more ... | { ...; })[]; }[]; }' is not assignable to type 'E'.
'E' could be instantiated with an arbitrary type which could be unrelated to '{ readonly _tag: "HttpApiDecodeError"; readonly message: string; readonly issues: readonly { readonly _tag: "Type" | "Pointer" | "Unexpected" | "Missing" | "Composite" | "Refinement" | "Transformation" | "Forbidden"; readonly message: string; readonly path: readonly (string | ... 1 more ... | { ...; })[]; }[]; }'. (ts 2322)

It's because allowing a user to supply E may invalidate the default parameter we're trying to assign.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to use an options object

@Avaq
Avaq force-pushed the avaq/override-decode-error-schema branch 2 times, most recently from 69e0599 to fc20fc2 Compare December 1, 2025 10:39
Avaq added 2 commits December 29, 2025 11:53
This allows users to customize how HttpApiDecodeErrors are represented
on the wire, enabling the use of HttpApi for wire protocols that don't
follow the Tagged Error approach to encoding errors.
The HttpRouter.toHttpApp returns an App.Default<E | RouteNotFound>,
but that type gets suppressed by the input type of Router.router, which
has `unknown` for the `E`, so the whole thing become `unknown`.

Additionally, the `apiMiddleware` has the type:

    <E, R>(self: App.Default<E, R>): App.Default<any, any>

So once again, the RouteNotFound is lost when the router is piped
through the middleware.

Both of these places where type information is lost could have led to
the following oversight:

The error is eventually captured by catchAllCause, and an attempt is
made to encode it. We don't know if the encoding succeeds, but by
default it will not succeed for RouteNotFound, causing it to be
re-failed to the end user. It is then likely caught by the
`withErrorHandling` part of `HttpApp.toHandled` which uses
`ServerError.causeResponse` to convert it to a Response.

However, in userland it's not clear why this is happening, because the
return type from `httpApp` states that the created HttpApp cannot fail.

This commit adds RouteNotFound to the error branch of the returned
HttpApp, making it clearer for users how RouteNotFound is propegated and
handled, and allowing users to add their own handlers.

------------------------------------------------------------------------

Note:

Technically, all errors that could occur according to the injected HttpApi
should be included in the output type of the httpApp. We never know for
sure that the encoder will succeed, and so we could always re-fail them.

The thing is, we can't know these in the `httpApp` constructor, because
the `HttpApi` is injected via the `HttpApi.Api` tag, which loses the `E`
type on the `HttpApi` it contains. So I left this out of scope for this
fix, because it would require a lot of refactoring.
@Avaq
Avaq force-pushed the avaq/override-decode-error-schema branch from fc20fc2 to 3ad8816 Compare December 29, 2025 11:41
@Avaq

Avaq commented Dec 29, 2025

Copy link
Copy Markdown
Contributor Author

@tim-smart I added a new commit here because I found another issue. If you want, I can extract it to a separate PR. I guess these pulls don't get a lot of attention because you're all focused on smol. I'm looking forward to the day I can upgrade, and focus my contributions there.

@fubhy

fubhy commented Jul 9, 2026

Copy link
Copy Markdown
Member

Closing as stale. There has not been activity for more than 6 months, the PR is not merge-ready, and it has drifted in scope with an unrelated second commit. If this is still needed, please open a fresh focused PR with a changeset and current tests.

@fubhy fubhy closed this Jul 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Customizing HttpApiDecodeError responses

4 participants