Tamás Soós
Tamás Soós
TtRPC
Created by cadams on 11/1/2023 in #❓-help
When error in tRPC route, the error message is vauge
Does that answer your questions?
12 replies
TtRPC
Created by cadams on 11/1/2023 in #❓-help
When error in tRPC route, the error message is vauge
Or send it to some other place automatically as described in the docs.
12 replies
TtRPC
Created by cadams on 11/1/2023 in #❓-help
When error in tRPC route, the error message is vauge
I can print the stack trace locally:
'use client';

import { trpc } from '@/util/trpc';

export default function MagicNumber() {
const { data, error, isError, isLoading } = trpc.magicNumber.useQuery();
console.log(error?.data?.stack);

if (isLoading) {
return <span>Loading...</span>;
}

if (isError) {
return <span>Error: {error.message}</span>;
}

return <span>Your magic number is {data}</span>;
}
'use client';

import { trpc } from '@/util/trpc';

export default function MagicNumber() {
const { data, error, isError, isLoading } = trpc.magicNumber.useQuery();
console.log(error?.data?.stack);

if (isLoading) {
return <span>Loading...</span>;
}

if (isError) {
return <span>Error: {error.message}</span>;
}

return <span>Your magic number is {data}</span>;
}
To get:
TRPCError: Magic number procedure error
at eval (webpack-internal:///(:3000/rsc)/./src/server/routers/app.ts:15:19)
at resolveMiddleware (webpack-internal:///(:3000/rsc)/./node_modules/@trpc/server/dist/index.mjs:431:36)
at callRecursive (webpack-internal:///(:3000/rsc)/./node_modules/@trpc/server/dist/index.mjs:467:38)
at resolve (webpack-internal:///(:3000/rsc)/./node_modules/@trpc/server/dist/index.mjs:497:30)
at callProcedure (webpack-internal:///(:3000/rsc)/./node_modules/@trpc/server/dist/config-4c0f8e88.mjs:189:12)
at inputToProcedureCall (webpack-internal:///(:3000/rsc)/./node_modules/@trpc/server/dist/resolveHTTPResponse-68c8befb.mjs:54:83)
at eval (webpack-internal:///(:3000/rsc)/./node_modules/@trpc/server/dist/resolveHTTPResponse-68c8befb.mjs:177:51)
at Array.map (<anonymous>)
at resolveHTTPResponse (webpack-internal:///(:3000/rsc)/./node_modules/@trpc/server/dist/resolveHTTPResponse-68c8befb.mjs:177:32)
TRPCError: Magic number procedure error
at eval (webpack-internal:///(:3000/rsc)/./src/server/routers/app.ts:15:19)
at resolveMiddleware (webpack-internal:///(:3000/rsc)/./node_modules/@trpc/server/dist/index.mjs:431:36)
at callRecursive (webpack-internal:///(:3000/rsc)/./node_modules/@trpc/server/dist/index.mjs:467:38)
at resolve (webpack-internal:///(:3000/rsc)/./node_modules/@trpc/server/dist/index.mjs:497:30)
at callProcedure (webpack-internal:///(:3000/rsc)/./node_modules/@trpc/server/dist/config-4c0f8e88.mjs:189:12)
at inputToProcedureCall (webpack-internal:///(:3000/rsc)/./node_modules/@trpc/server/dist/resolveHTTPResponse-68c8befb.mjs:54:83)
at eval (webpack-internal:///(:3000/rsc)/./node_modules/@trpc/server/dist/resolveHTTPResponse-68c8befb.mjs:177:51)
at Array.map (<anonymous>)
at resolveHTTPResponse (webpack-internal:///(:3000/rsc)/./node_modules/@trpc/server/dist/resolveHTTPResponse-68c8befb.mjs:177:32)
Which includes the line number.
12 replies
TtRPC
Created by cadams on 11/1/2023 in #❓-help
When error in tRPC route, the error message is vauge
That example with the console would just print undefined as it should, but if I do something like:
import { procedure, router } from '@/server/trpc';
import { TRPCError } from '@trpc/server';

export const appRouter = router({
magicNumber: procedure.query(() => {
try {
const arr: number[] = [];
console.log(arr[0].toFixed());
} catch (e) {
throw new TRPCError({
message: 'Magic number procedure error',
code: 'BAD_REQUEST',
});
}
return 42;
}),
});

export type AppRouter = typeof appRouter;
import { procedure, router } from '@/server/trpc';
import { TRPCError } from '@trpc/server';

export const appRouter = router({
magicNumber: procedure.query(() => {
try {
const arr: number[] = [];
console.log(arr[0].toFixed());
} catch (e) {
throw new TRPCError({
message: 'Magic number procedure error',
code: 'BAD_REQUEST',
});
}
return 42;
}),
});

export type AppRouter = typeof appRouter;
It gives me Magic number procedure error on the client side. I'm using this setup. It has no additional error formatting.
12 replies
TtRPC
Created by junior1 on 11/1/2023 in #❓-help
trpc error
Did it work?
20 replies
TtRPC
Created by junior1 on 11/1/2023 in #❓-help
trpc error
Files containing JSX should have a .jsx or .tsx extension.
20 replies
TtRPC
Created by junior1 on 11/1/2023 in #❓-help
trpc error
Oh actually, can you try saving it as a tsx file instead of a ts file?
20 replies
TtRPC
Created by junior1 on 11/1/2023 in #❓-help
trpc error
I'll check it out in about an hour or so
20 replies
TtRPC
Created by benjaminreid on 11/1/2023 in #❓-help
Would you recommend tRPC’s usage in this case?
I think there's nothing wrong with creating a reusable piece of logic, but you say make the client its own package. Why the client? Personally I'd create a private package with only the common logic in it as an async function. No reference to even trpc. Then I'd install this package on both applications, and invoke the library function in a procedure somewhere in a router. This way you can easily: - Transform the results further. - Have completely unique trpc configurations. - Have different trpc clients / links - Use the api call anywhere, even without trpc. - Extend the library with other API integrations in the future. - Have independent repos for the two apps and the library without any issues.
7 replies
TtRPC
Created by Zion on 11/1/2023 in #❓-help
Help with inferring output
Sorry, forgot to address the conditionals. I found a bug report here
9 replies
TtRPC
Created by Zion on 11/1/2023 in #❓-help
Help with inferring output
Does this help? Prisma has some tools to infer the types of select, include, etc..., I also had a similar use case.
9 replies
TtRPC
Created by junior1 on 11/1/2023 in #❓-help
trpc error
Could you share a repo so i can reproduce your issue?
20 replies
TtRPC
Created by cadams on 11/1/2023 in #❓-help
When error in tRPC route, the error message is vauge
Could you share repo for reproduction?
12 replies
TtRPC
Created by aryzing on 10/31/2023 in #❓-help
How to configure context for a standalone server?
Maybe you're looking for this and this
6 replies
TtRPC
Created by aryzing on 10/31/2023 in #❓-help
What's the negative code returned in error responses?
tRPC is compliant with JSON-RPC and has specific error codes. You can find them here; The docs have a great explanation on handling errors.
6 replies
TtRPC
Created by Tamás Soós on 10/27/2023 in #❓-help
tRPC over WebSocket with Next.js and NextAuth
Yes, that's a nice starter. The type issue in their context file I was able to solve by only defining the type there, and using getServerSession in the api route vs getSession in the ws server. Still it would be nice to use getServerSession in both places. I'll check out Lucia. The docs are nice. Thanks. Since in the end this is more of a NextAuth issue, I asked them directly on github. I'll close this post.
9 replies
TtRPC
Created by Tamás Soós on 10/27/2023 in #❓-help
tRPC over WebSocket with Next.js and NextAuth
Adding a try catch in there slows down the reconnect attempts to a few a seconds vs the original that reconnected as fast as it could. getServerSession throws
Error: Invariant: headers() expects to have requestAsyncStorage, none available.
at headers (/Users/tamas/Projects/oddp/node_modules/next/dist/client/components/headers.js:42:15)
at getServerSession (/Users/tamas/Projects/oddp/node_modules/next-auth/next/index.js:139:35)
at createContext (/Users/tamas/Projects/oddp/src/server/context.ts:22:21)
at WebSocketServer.<anonymous> (/Users/tamas/Projects/oddp/node_modules/@trpc/server/dist/adapters/ws.js:82:28)
at WebSocketServer.emit (node:events:526:35)
at WebSocketServer.emit (node:domain:488:12)
at WebSocketServer.completeUpgrade (/Users/tamas/Projects/oddp/node_modules/ws/lib/websocket-server.js:427:5)
at WebSocketServer.handleUpgrade (/Users/tamas/Projects/oddp/node_modules/ws/lib/websocket-server.js:336:10)
at Server.upgrade (/Users/tamas/Projects/oddp/node_modules/ws/lib/websocket-server.js:112:16)
at Server.emit (node:events:514:28)
Error: Invariant: headers() expects to have requestAsyncStorage, none available.
at headers (/Users/tamas/Projects/oddp/node_modules/next/dist/client/components/headers.js:42:15)
at getServerSession (/Users/tamas/Projects/oddp/node_modules/next-auth/next/index.js:139:35)
at createContext (/Users/tamas/Projects/oddp/src/server/context.ts:22:21)
at WebSocketServer.<anonymous> (/Users/tamas/Projects/oddp/node_modules/@trpc/server/dist/adapters/ws.js:82:28)
at WebSocketServer.emit (node:events:526:35)
at WebSocketServer.emit (node:domain:488:12)
at WebSocketServer.completeUpgrade (/Users/tamas/Projects/oddp/node_modules/ws/lib/websocket-server.js:427:5)
at WebSocketServer.handleUpgrade (/Users/tamas/Projects/oddp/node_modules/ws/lib/websocket-server.js:336:10)
at Server.upgrade (/Users/tamas/Projects/oddp/node_modules/ws/lib/websocket-server.js:112:16)
at Server.emit (node:events:514:28)
9 replies
TtRPC
Created by Tamás Soós on 10/27/2023 in #❓-help
tRPC over WebSocket with Next.js and NextAuth
My issue is with the createContext call. The opts type isn't quite right. getSession is giving me:
Type 'IncomingMessage | Request' is not assignable to type '(Partial<IncomingMessage> & { body?: any; }) | undefined'.
Type 'Request' is not assignable to type 'Partial<IncomingMessage> & { body?: any; }'.
Type 'Request' is not assignable to type 'Partial<IncomingMessage>'.
Types of property 'headers' are incompatible.
Type 'Headers' is not assignable to type 'IncomingHttpHeaders'.
Index signature for type 'string' is missing in type 'Headers'.ts(2322)
(property) CtxOrReq.req?: (Partial<IncomingMessage> & {
body?: any;
}) | undefined
Type 'IncomingMessage | Request' is not assignable to type '(Partial<IncomingMessage> & { body?: any; }) | undefined'.
Type 'Request' is not assignable to type 'Partial<IncomingMessage> & { body?: any; }'.
Type 'Request' is not assignable to type 'Partial<IncomingMessage>'.
Types of property 'headers' are incompatible.
Type 'Headers' is not assignable to type 'IncomingHttpHeaders'.
Index signature for type 'string' is missing in type 'Headers'.ts(2322)
(property) CtxOrReq.req?: (Partial<IncomingMessage> & {
body?: any;
}) | undefined
even though it happens to be working for now. Btw getSession creates infinite reconnects on the ws client with any other inputs and getServerSession does the same except I couldn't find any arguments that work at all.
9 replies
TtRPC
Created by Tamás Soós on 10/27/2023 in #❓-help
tRPC over WebSocket with Next.js and NextAuth
9 replies
TtRPC
Created by Tamás Soós on 10/27/2023 in #❓-help
tRPC over WebSocket with Next.js and NextAuth
and a provider component
9 replies