Skip to content

chore: better error for mismatched content-type - #5654

Merged
Nick-Lucas merged 2 commits into
nextfrom
alt-approach
Apr 21, 2024
Merged

Nick-Lucas merged 2 commits into
nextfrom
alt-approach

Conversation

@juliusmarminge

@juliusmarminge juliusmarminge commented Apr 21, 2024 •

Copy link
Copy Markdown
Member

Closes #5653

  • improves error by providing the received content type to the error
  • uses Headers (Node18+) whose .get() is case-insensitive

@juliusmarminge
juliusmarminge requested a review from a team as a code owner April 21, 2024 09:00
@vercel

vercel Bot commented Apr 21, 2024 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
next-prisma-starter ✅ Ready (Inspect) Visit Preview Apr 21, 2024 9:18am
og-image ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 21, 2024 9:18am
www ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 21, 2024 9:18am


interface MinimalHandlerOpts {
req: {
headers: HTTPHeaders | Headers;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This could probably only take Headers and do the conversion higher up, but that's a bigger refactor for another day

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.

Yeah that's fine, limited number of these types right now so we can wrangle it here

THandler extends BaseContentTypeHandler<THandlerOpts>,
>(handlers: THandler[], opts: THandlerOpts) {
const handler = handlers.find((handler) => handler.isMatch(opts));
const contentType = new Headers(opts.req.headers as HeadersInit).get(

@juliusmarminge juliusmarminge Apr 21, 2024 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

this cast is fine, idk why it complains that you can't pass a Record<string, string[]> in ther...

CleanShot 2024-04-21 at 11 02 14@2x

THandler extends BaseContentTypeHandler<THandlerOpts>,
>(handlers: THandler[], opts: THandlerOpts) {
const handler = handlers.find((handler) => handler.isMatch(opts));
const contentType = new Headers(opts.req.headers as HeadersInit).get(

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.

Yeah this is way better

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.

Does Lambda Adapter definitely behave okay? That has mixed case headers

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

if it adheres to the current type (Either Record<string, string> or Record<string, string[]> it should work. Otherwise we have invalid types somewhere else

'content-type',
);

const error = new TRPCError({

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.

Maybe we should still only create this when we actually need it? Unnecessary to create it for every request

@github-actions

github-actions Bot commented Apr 21, 2024 •

Copy link
Copy Markdown
Contributor

Diagnostics Comparison

Details

Numbers

Metric PR next
Files 795 795 (➖ 0)
Lines of Library 40,640 40,640 (➖ 0)
Lines of Definitions 118,086 118,086 (➖ 0)
Lines of TypeScript 4,967 4,967 (➖ 0)
Lines of JavaScript 0 0 (➖ 0)
Lines of JSON 0 0 (➖ 0)
Lines of Other 0 0 (➖ 0)
Identifiers 174,931 174,931 (➖ 0)
Symbols 109,020 109,020 (➖ 0)
Types 89 89 (➖ 0)
Instantiations 0 0 (➖ 0)
Memory used 175,665 175,697 (🔽🟢 -32)
Assignability cache size 0 0 (➖ 0)
Identity cache size 0 0 (➖ 0)
Subtype cache size 0 0 (➖ 0)
Strict subtype cache size 0 0 (➖ 0)

Timings and averages

Metric PR next
max (s) 4.357 4.368 (🔽🟢 -0.01)
min (s) 4.357 4.368 (🔽🟢 -0.01)
avg (s) 4.357 4.368 (🔽🟢 -0.01)
median (s) 4.357 4.368 (🔽🟢 -0.01)
length 1 1 (➖ 0)
unstable timings

Unstable

Timings are not reliable in here

Metric PR next
I/O Read time 0.04 0.04 (➖ 0)
Parse time 0.71 0.73 (🔽🟢 -0.02)
ResolveTypeReference time 0.03 0.03 (➖ 0)
ResolveModule time 0.11 0.12 (🔽🟢 -0.01)
ResolveLibrary time 0.02 0.02 (➖ 0)
Program time 1.05 1.06 (🔽🟢 -0.01)
Bind time 0.43 0.43 (➖ 0)
Total time 1.48 1.49 (🔽🟢 -0.01)

code: 'UNSUPPORTED_MEDIA_TYPE',
message:
'Invalid Content-Type header. This request may not be supported by your tRPC Adapter, or possibly by tRPC at all',
'No Content-Type header detected on the incoming request. This request may not be supported by your tRPC Adapter, or possibly by tRPC at all',

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@github-actions

Copy link
Copy Markdown
Contributor

This pull request has been locked because we are very unlikely to see comments on closed issues. If you think, this PR is still necessary, create a new one with the same branch. Thank you.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Apr 22, 2024

This branch was successfully deployed

5 active deployments
Preview – www — d0bc28aa Deployed Apr 21, 2024 by vercel[bot]
Preview – next-prisma-starter — d0bc28aa Deployed Apr 21, 2024 by vercel[bot]
Preview – trpc-next-app-dir — d0bc28aa Deployed Apr 21, 2024 by vercel[bot]
Preview – todomvc — d0bc28aa Deployed Apr 21, 2024 by vercel[bot]
Preview – og-image — d0bc28aa Deployed Apr 21, 2024 by vercel[bot]
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants