doc
doc2y ago

TRPCClientError: Unexpected end of JSON input

Hey I am running into this issue when calling a mutateAsync in production in Vercel (the error does not appear in localhost dev mode).
TRPCClientError: Unexpected end of JSON input
My code base is so large that I am not sure where do I start to debug. Are there any best practices that I should follow to avoid this error? It is similar to this issue: https://github.com/trpc/trpc/discussions/1847#discussioncomment-4244990
GitHub
Error "TRPCClientError: request failed" when building a Next.js app...
When trying to build my Next.js project, I'm getting the following error: TRPCClientError: request to http://localhost:3000/api/trpc/post.test?batch=1&input=%7B%220%22%3A%7B%22json%...
13 Replies
Nick
Nick2y ago
If you inspect the failed request in the network debugger what does it return? I mostly see errors like this where some HTTP service is returning an error in HTML format where it's expected to be a JSON response on the happy path. Could also be some encoding issue though. tRPC itself wouldn't return HTML but perhaps it's just forwarding some error from inside your API?
doc
doc2y ago
Here is the header response. The weird part is that if I implement a "retry" on the call, it always succeeds the second time. And on very rare occasions, it succeeds the first time as well. But I prefer if it just does not error out.
doc
doc2y ago
^
Nick
Nick2y ago
What's the body response though?
doc
doc2y ago
payload? Im not sure what you are referring to the "network>response" tab is empty
Nick
Nick2y ago
Got it 500 implies the error originated inside the API itself, not on the client side. Which means it's a likely problem with something you're doing in your own application You should check the logs on the API and add more logging if they're not helping
doc
doc2y ago
let me give it a try thanks
doc
doc2y ago
I try to add console.log and ran it in prod. For some reason console.log does not appear if the error occurs. The error shown in image.
doc
doc2y ago
Even when I run a dummy function, it fails (only in Vercel prod).
testFunction: t.procedure.mutation(({ctx})=>{
return {testValue: true: testValue2: 0}
})
testFunction: t.procedure.mutation(({ctx})=>{
return {testValue: true: testValue2: 0}
})
doc
doc2y ago
Nick
Nick2y ago
This definitely looks like a debugging piece within your app. The stacktrace looks quite informative
doc
doc2y ago
So even with an empty function, since the error still occurs, so a trpc setup issue? Seems odd as I just ran T3 app.
Nick
Nick2y ago
You’re hosting within lambda right? I would look at that setup if it’s working locally. Can be in your codebase or in your infrastructure