You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Customizing HttpApi Errors (For RFC 9457 Problem Details) #5367
As a developer, I want my Public API to have a standardized error format following RFC 9457, that does not use Effect's proprietary _tag for differentiation.
Currently, HttpApi doesn't support Schema.transform and HttpApiDecodeError is hardcoded for every API route (even if it doesn't have any kind of input. #4751)
What is the feature you are proposing to solve the problem?
import{HttpMiddleware,HttpServerResponse}from'@effect/platform'import{HttpApiDecodeError}from'@effect/platform/HttpApiError'import{Effect,Schema}from'effect'import{ValidationError}from'./errors'/** * Transform from HttpApiDecodeError to ValidationError Problem Details format */constHttpApiDecodeErrorToValidationError=Schema.transform(HttpApiDecodeError,ValidationError,{strict: false,decode: (httpError)=>({type: 'urn:piqy:errors:validation'asconst,title: 'Validation Error'asconst,status: 400asconst,detail: httpError.message,issues: httpError.issues,}),encode: (validationError)=>newHttpApiDecodeError({message: validationError.detail,// biome-ignore lint/suspicious/noExplicitAny: no idea whyissues: validationError.issuesasany,}),})/** * Middleware that transforms HttpApiDecodeError responses to Problem Details format */exportconstProblemDetailsDecodeErrorMiddleware=HttpMiddleware.make((app)=>Effect.gen(function*(){constresponse=yield*appif(response.body._tag==='Uint8Array'){constdecoder=newTextDecoder()consttext=decoder.decode(response.body.body)if(text.includes('HttpApiDecodeError')){constvalidationError=yield*Schema.decode(HttpApiDecodeErrorToValidationError)(JSON.parse(text)).pipe(Effect.catchTag('ParseError',()=>Effect.succeed(response)),)returnyield*HttpServerResponse.json(validationError,{status: 400,headers: {'content-type': 'application/problem+json',},})}}returnresponse}),)
Also, it's a duplicate of #4751, which is the main reason I closed this
And yikes 🙈 having to decode the already byte-encoded, ready for flight, response body 😳
And that's not to mention that you'd also need an additional hack of the OpenAPI specs generated from the underlying HttpApi, because it doesn't know about your middleware.
@tobimori I opened #5479, which may solve part of the problem, if accepted.
With it, you can just do .addError(HttpApiDecodeErrorToValidationError) and it'll take precedence over the built-in HttpApiDecodeError schema.
The remaining part of the problem is that the HttpApiDecodeError would still be listed in JsonSchema responses, but in practice it wouldn't actually come out of your API implementation.
Activity
Avaq commentedon Sep 4, 2025
Hey I found this, and I think it may be a duplicate of my own feature request: #5465
@tobimori since you closed this, has it been solved?
tobimori commentedon Sep 4, 2025
@Avaq
I used a middleware:
Also, it's a duplicate of #4751, which is the main reason I closed this
Avaq commentedon Sep 4, 2025
Right, thanks!!
And yikes 🙈 having to decode the already byte-encoded, ready for flight, response body 😳
And that's not to mention that you'd also need an additional hack of the OpenAPI specs generated from the underlying HttpApi, because it doesn't know about your middleware.
I hope a better solution will come along soon!
tobimori commentedon Sep 4, 2025
yes, I have a similar thing for my OpenApi. but it's the only way to male it possible right now.
Avaq commentedon Sep 8, 2025
@tobimori I opened #5479, which may solve part of the problem, if accepted.
With it, you can just do
.addError(HttpApiDecodeErrorToValidationError)and it'll take precedence over the built-inHttpApiDecodeErrorschema.The remaining part of the problem is that the
HttpApiDecodeErrorwould still be listed in JsonSchema responses, but in practice it wouldn't actually come out of your API implementation.linrium commentedon Sep 13, 2025
Do can I use it with HttpBuilderApi. I tried but it didn't work
janstuemmel commentedon Sep 17, 2025
Hey @tobimori
i found a solution to do this a bit prettier
yan you provide a example on how you did get rid of the error in the open api spec?